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
5. Create a variety of test cases focusing on the sorting algorithm, such as the final element is the smallest, the entire set i
andrew11 [14]

Answer:

// C code

// This code will compute the values of the sales ticket sales for concerts

#include <stdio.h>

#define MAXN 100 // max characters in a group/concert name

#define MAXG 50 // max concerts/groups

#define MAXC 3 //max categories

char group [MAXG][MAXN];

int fans [MAXG][MAXC];

float prices [MAXC];

float sales [MAXG];

int count = 0;

void printArray () {

printf ("%15s%5s%5s%5s%10s\n",

"Concert", "s1", "s2", "s3","Sales");

for (int i = 0; i < count; i++) {

printf ("%15s", group [i]);

for (int j = 0; j < MAXC; j++) {

printf ("%5d", fans[i][j]);

} // end for each category

printf ("%10.2f\n", sales [i]);

} // end for each group

} // end function printArray

void computeSales () {

for (int i = 0; i < count; i++) {

sales [i] = 0;

for (int j = 0; j < MAXC; j++) {

sales [i] += prices [j] * fans [i][j];

} // end for each category

} // end for each group

} // end function computeSales

// ***** totalSales ****

void totalSales()

{

float tsales;

for(int i = 0; i <= MAXC; i++){

tsales += sales [i];

}

printf("\n\t\tThe total sales are: $%.2f\n",tsales);

}

// ***********************************

void switchRows (int m, int n) {

char tc;

int ti;

float v;

// printf ("Switching %d with %d\n", m, n);

for (int i = 0; i < MAXN; i++) {

tc = group [m][i];

group [m][i] = group [n][i];

group [n][i] = tc;

} // end for each character in a group name

for (int i = 0; i < MAXC; i++) {

ti = fans [m][i];

fans [m][i] = fans [n][i];

fans [n][i] = ti;

} // end for each fan category

v = sales [m];

sales [m] = sales [n];

sales [n] = v;

} // end switch

int findMinSales (int m) {

float min = sales [m];

int target = m;

for (int i = m+1; i < count; i++)

if (sales [i] < min) {

min = sales [i];

target = i;

} // end new max found

return target;

} // end function findMinSales

void sortBySales () {

int target;

for (int i = 0; i < count; i++) {

target = findMinSales (i);

if (target > i)

switchRows (i, target);

} // for each concert

} // end function sortBySales

//**********sort by fans*************

int minFans (int b) {

float min = fans [b][0];

int target = b;

for (int i = b; i < count; i++)

if (fans [i][0] < min) {

min = fans [i][0];

target = i;

} // end new max found

return target;

}

void sortByFans () {

int target = 0;

//int result;

 

for (int i = 0; i < count; i++) {

target = minFans(i);

if (target > i)

//result = target;    

switchRows (i,target);

 

}

}

//********************************

void getData () {

// for (int i = 0; i < MAXG; i++) sales [i] = 0;

printf ("\nEnter ticket prices in each of %d cateogories: ", MAXC);

for (int i = 0; i < MAXC; i++)

scanf ("%f", &prices [i]);

printf ("-- Enter group and fans in %d categories\n", MAXC);

printf (" . to finish entries:\n");

for (int i = 0; i < MAXG; i++) {

scanf ("%s", group[i]);

if (group [i][0] == '.')

break;

count++;

for (int j = 0; j < MAXC; j++)

scanf ("%d", &fans[i][j]);

} // end for each group

} // end function getData

int main(void) {

getData ();

computeSales ();

printArray ();

printf ("\n --- Sorted sales ---\n");

sortBySales ();

printArray ();

totalSales ();

printf("--- sorted by fans ---\n");

sortByFans ();

printArray ();

totalSales ();

printf("\t\t ... Bye ...\n");

return 0;

}

7 0
4 years ago
Give 3 reasons why it is believed that smart phones precent us from communicating face to face.give three reasons why it is beli
gregori [183]

Answer:

yes

Explanation:

because the people will be lazy to go and talk to that person instead They will call each other and discuss

7 0
2 years ago
What Windows utility can enable you to shut down an unresponsive application?
boyakko [2]

Answer:

Task manager

Explanation:

Allows you to force quit applications.

4 0
3 years ago
Read 2 more answers
Money left over after the bills are paid is referred to as
sveta [45]
Change is what is left over 

ex. the cashier tells you "here is your leftover change sir/ma'am "<span />
3 0
4 years ago
Read 2 more answers
A circular copper disk of diameter D=10 cm rotates at frequency =1800 rev/min about an axis through its center and at right angl
MAXImum [283]

Answer:

Induced current is 0.226 A

Explanation:

To calculate potential difference we have

E =1/2Bomega*r2 =1/2*1*1800*3.14*0.05=141.3

putting values we get

the p.d is 141.3

7 0
3 years ago
Other questions:
  • You are given a network of 10.50.24.0/21, which contains 2,048 addresses. what subnet mask should you use to divide this into fo
    7·1 answer
  • Which piece of personal information do websites often require users to enter?
    6·2 answers
  • Do Violent Video Games Make People More violent in Real Life ?
    14·1 answer
  • Anna is making a presentation on the solar system. She wants to emphasize the planet names as they appear one by one on the pres
    11·1 answer
  • You resurrected an old worksheet. It appears to contain most of the information that you need, but not all of it. Which step sho
    5·1 answer
  • What do these terms have in common? google, yahoo!, bing they are important books. they are internet search engines. they are wo
    10·1 answer
  • Write an if/else statement that compares the double variable pH with 7.0 and makes the following assignments to the bool variabl
    6·1 answer
  • Which of the four control methods is best suited for transfer of large blocks of data?
    8·1 answer
  • Favorite color should it be stored why?or why not?<br>​
    6·1 answer
  • Technology __________ guides how frequently technical systems are updated, and how technical updates are approved and funded.
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!