1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
bearhunter [10]
4 years ago
5

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.
Computers and Technology
1 answer:
liraira [26]4 years ago
7 0

Answer:

See Explaination

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]+"\n"); } // 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); } }

You might be interested in
<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="225547404f435156475062404e434c4947560c414d4f">[email&#1
Basile [38]
The answer would be D. An email address
3 0
4 years ago
Read 2 more answers
Can you show me how to code this is GDBonline? explaining each statement with notes//
MAVERICK [17]

Answer:

Create two classes, PlayingCard and Main, as depicted in the attachments.

Explanation:

The getters/setters are not actually used in this code, but creating them is pretty standard. Although, you could argue that a PlayingCard object should never change value/suit, and is what it is as long as it exists. Such objects would be called "immutable".

7 0
2 years ago
Technological improvements have allowed people to inhabit a wider variety of places more easily. Please select the best answer f
muminat

The statement ‘Technological improvements have allowed people to inhabit a wider variety of places more easily’ is true. Technology has made our lives easier and faster. You can just buy a land or house from the developer to obtain a place you want.

9 0
3 years ago
Read 2 more answers
In what country have regulators demanded that facebook stop collecting biometric data on its users?
Helga [31]
America or Australia
6 0
4 years ago
Which of the following is one way a corporation can raise money?
Vladimir [108]
Selling stock on the stock market
4 0
4 years ago
Other questions:
  • Find Multiplication of Binary Numbers: 1100112 x 10012
    9·1 answer
  • Who defined the term crowdsourcing in a Wired magazine article?
    15·1 answer
  • Jackson is teaching the decimal number system. He wants his students to know how to expand numbers by powers of 10. Which is the
    13·2 answers
  • The software program you need to have to read PDF file, such as the PDF files you download from your My Courses page, is
    9·2 answers
  • Design state machines to control the minutes and hours of a standard 24 hour clock. Your clock should include an AM/PM indicator
    11·1 answer
  • What is the purpose of a Macro in Word?: *
    13·1 answer
  • I’ll mark brainliest if correct
    10·1 answer
  • Given three packets of size 2,500 Bytes (caution this is Bytes not bits) each are ready inside computer 1 to be transmitted onto
    14·1 answer
  • The temperature at 8 AM was 44°F and at noon it was 64°F. what was the percent of change from 8 AM to noon?
    11·1 answer
  • Which would you use to get the number of elements in a dictionary?
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!