68.6k views
5 votes
Method generateSales Does not take any parameters. Returns an array of integers. Using the random number generator, generate 10 numbers between 1 and 100. Store the numbers in an integer array. Print the array to the console. Method writeToFile Takes an integer array. Writes the array of integers to a file called numbers.txt Returns the filename. Catches any Exception that might be thrown. Method readNumbers Takes a string (the filename) Reads the file contents into an array list (integer). Catch a file not found exception. Returns the array list. Method printResults Takes the ArrayList of Integers Reads the elements and prints them on the command line. Don't return anything.

Main method
Call generate sales method.
Call write to file method and pass the array of sales data.
Call read numbers method and pass the file name.
Call print results and pass list of numbers
Catch any Exception that might be thrown.

User Michelann
by
5.6k points

1 Answer

1 vote

Answer:

See Explaination

Step-by-step explanation:

// for generating random numbers import java.util.Random; // read and write to a file and handling exceptions import java.io.*; // reading data from a file import java.util.Scanner; // storing data into ArrayList import java.util.ArrayList; class Sample { int[] arr; public int[] generateSales() { // creating an object for Random class Random r = new Random(); // creating an array of size 10 arr = new int[10]; for(int i=0;i<arr.length;i++) { // generating random numbers and storing into an array arr[i] = r.nextInt(100); } for(int i=0;i<arr.length;i++) { // printing the array data in console System.out.println(arr[i]); } // returning integer array return arr; } // Writing to a file public String writeToFile(int[] tarr) { // fName for storing file name String fName = null; // try-catch for catching any unwanted exception try { File file = new File("numbers.txt"); // file.createNewFile() creates the file if(file.createNewFile()) { System.out.println("File created Successfully"); } // if file exists else block will be executed else { System.out.println("File already exists'"); } // storing the filename fName = file.getName(); FileWriter myWriter = new FileWriter(fName); for(int i=0; i<tarr.length;i++) { // writing to a file myWriter.write(arr[i]+"\\"); } // closing the file myWriter.close(); System.out.println("Successfully wrote to the file."); } catch (IOException e) { System.out.println("An error occurred."); e.printStackTrace(); } // returning the file name return fName; } // reading data from a file public ArrayList<Integer> readNumbers(String fileName) { // ArrayList to store integer type data ArrayList<Integer> list = new ArrayList<Integer>(); // try-catch to handle FileNotFoundException try { File f = new File(fileName); // reading data from a file Scanner sc = new Scanner(f); while (sc.hasNextInt()) { // storing data into a list list.add(sc.nextInt()); } // closing the file sc.close(); } catch (FileNotFoundException e) { System.out.println("An error occurred."); e.printStackTrace(); } // returning ArrayList return list; } // Printing the data of ArrayList to console public void printResults(ArrayList<Integer> intList) { for(int i=0;i<intList.size();i++) { System.out.println(intList.get(i)); } } // main method public static void main(String[] args) { ArrayList<Integer> listOInt = new ArrayList<Integer>(); // creating an object for Sample class Sample s = new Sample(); int[] rarr; // calling generateSales method and storing the returned int array rarr = s.generateSales(); // calling writeToFile method and storing the returned fileName String fileName = s.writeToFile(rarr); // calling readNumbers method and storing the returned ArrayList listOInt = s.readNumbers(fileName); // calling printResults to display the data of listOInt s.printResults(listOInt); } }

User Aravindh Gopi
by
4.9k points