Write a program that creates an output file called “proj7.txt” containing n rand
ID: 3677713 • Letter: W
Question
Write a program that creates an output file called “proj7.txt” containing n random numbers between 10 and 100 inclusive. The program prompts the user for the value of n, generates the random numbers, and writes them to the file three on each line. You must close the file output stream when you are done writing to the file. Otherwise, the output file may not have all the n numbers in it. File I/O are usually buffered by the operating systems, and these buffers are flushed when you close the streams. Seed the random number generator with the system’s clock so that it generates a different sequence of numbers each time you run the program. Compile and run the program and make sure it works (check the output file to verify that it contains n numbers between 10 and 100 and that there are three numbers on each line).
*********************** The code should be in Eclipse MAC USER********************
Now you are going to read the numbers from the file you just created “proj7.txt” and display them on the screen 3 on each line. In addition you are going to find the largest, the smallest, and the average of all the numbers in the file and display them neatly on the screen (see below). You will do all these tasks by reading the input file “proj7.txt” just once.
Include a loop in the program that lets the utser repeat the above process for as many times as she/he wishes to do so.
Sample Input and Output:
Programmer: Name of the programmer (your name)
Course:
Lab#:
Due Date:
How many numbers you want in “proj7.txt”: 10
File “proj7.txt” contains:
21 90 12
30 20 50
62 71 99
86
The largest number in “proj7.txt” is: 99
The smallest number in “proj7.txt” is: 12
The average of all the numbers in “proj7.txt” is: 54.1
Do it again? Y or y (for yes), and N or n (for no): N
Explanation / Answer
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Random;
import java.util.Scanner;
/**
* @author srinu
*
*/
public class RandomNumbersFile {
/**
* method to write randoms to the file
*
* @param n
*/
public static void writeRandoms(int n) {
try {
File file = new File("proj7.txt");
Random r = new Random();
int Low = 10;
int High = 100;
file.createNewFile();
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
for (int i = 1; i <= n; i++) {
int randomNum = r.nextInt(High - Low) + Low;
bw.write(randomNum + " ");
}
bw.close();
fw.close();
} catch (Exception e) {
// TODO: handle exception
}
}
/**
* method to read randoms from file, print the values and calculate min, max
* and average
*
* @param n
*/
public static void readRandoms(int n) {
try {
File file = new File("proj7.txt");
Scanner scanner = new Scanner(file);
int min = 0, max = 0, sum = 0;
System.out.println("File "proj7.txt" contains:");
for (int i = 1; i <= n; i++) {
int value = scanner.nextInt();
if (i % 3 == 0)
System.out.print(value + " ");
else
System.out.print(value + " ");
if (i == 1) {
max = min = value;
sum += value;
} else {
sum += value;
if (max < value)
max = value;
if (min > value)
min = value;
}
}
System.out.println(" The largest number in "proj7.txt" is:"
+ max);
System.out
.println("The smallest number in "proj7.txt" is:" + min);
double average = (double) sum / (double) n;
System.out
.println("The average of all the numbers in "proj7.txt" is:"
+ average);
scanner.close();
} catch (Exception e) {
// TODO: handle exception
}
}
/**
* method to display heading
*/
public static void displayHeading() {
System.out.println("Sample Input and Output:");
System.out.println("Programmer: Name of the programmer (your name)");
System.out.println("Course: ");
System.out.println("Lab#: ");
System.out.println("Due Date: ");
}
/**
* @param args
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
displayHeading();
do {
System.out.print("How many numbers you want in "proj7.txt":");
int n = scanner.nextInt();
writeRandoms(n);
readRandoms(n);
System.out
.print("Do it again? Y or y (for yes), and N or n (for no): ");
String choice = scanner.next();
if (choice.equalsIgnoreCase("n"))
break;
} while (true);
scanner.close();
}
}
OUTPUT:
Sample Input and Output:
Programmer: Name of the programmer (your name)
Course:
Lab#:
Due Date:
How many numbers you want in "proj7.txt":10
File "proj7.txt" contains:
87 73 57
47 56 41
10 66 41
48
The largest number in "proj7.txt" is:87
The smallest number in "proj7.txt" is:10
The average of all the numbers in "proj7.txt" is:52.6
Do it again? Y or y (for yes), and N or n (for no): y
How many numbers you want in "proj7.txt":15
File "proj7.txt" contains:
42 56 96
73 14 97
13 40 33
21 22 78
17 69 48
The largest number in "proj7.txt" is:97
The smallest number in "proj7.txt" is:13
The average of all the numbers in "proj7.txt" is:47.93333333333333
Do it again? Y or y (for yes), and N or n (for no): y
How many numbers you want in "proj7.txt":8
File "proj7.txt" contains:
39 94 60
31 42 71
75 21
The largest number in "proj7.txt" is:94
The smallest number in "proj7.txt" is:21
The average of all the numbers in "proj7.txt" is:54.125
Do it again? Y or y (for yes), and N or n (for no): y
How many numbers you want in "proj7.txt":5
File "proj7.txt" contains:
57 37 25
42 59
The largest number in "proj7.txt" is:59
The smallest number in "proj7.txt" is:25
The average of all the numbers in "proj7.txt" is:44.0
Do it again? Y or y (for yes), and N or n (for no): y
How many numbers you want in "proj7.txt":17
File "proj7.txt" contains:
33 50 33
68 90 20
93 77 31
19 60 82
80 31 96
43 65
The largest number in "proj7.txt" is:96
The smallest number in "proj7.txt" is:19
The average of all the numbers in "proj7.txt" is:57.11764705882353
Do it again? Y or y (for yes), and N or n (for no): n
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.