you will be creating a driver class only for this assignment, however, you need
ID: 3700713 • Letter: Y
Question
you will be creating a driver class only for this assignment, however, you need to submit the driver class and both the input and output files.
Be sure to include a message to the user explaining the purpose of the program before any other printing to the screen.
Within the main method, please complete the following tasks:
Use a loop to create a file named “input.txt” that contains 10 randomly generated numbers between 0 and 99, one per line. Be sure to close the file when done and print a message to the screen indicating the input file has been created.
Create an output file named “output.txt”. Create a header row similar to the following in the file:
Number 1x 2x 3x 4x 5x 6x 7x 8x 9x 10x
Using a second loop, read in the numbers from the input file one at a time (use the hasNext() method). After reading in each number, calculate and write/print the first 10 multiples of the value read in separated by a tab to the output file.
Close file output file when all the values in the input file have been processed.
Print a message to the user indicating that the output files has been created.
Include clear comments throughout the program that explain the functionality the various lines/small sections of code are completing as related to the execution of the program.
Explanation / Answer
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Random;
/**
* The java program that write 10 random numbers in the
* file input.txt and then open input file and then write
* the 1 to 10 times of each number to output.txt file
* */
//Multiples.java
import java.util.Scanner;
public class Multiples {
public static void main(String[] args) {
String inputFile="input.txt";
Random rand=new Random();
PrintWriter writer=null;
try {
writer=new PrintWriter(new File(inputFile));
//write 10 random numbers
for (int i = 0; i < 10; i++) {
int num=rand.nextInt(99);
writer.printf("%-5d", num);
}
//close the writer object
writer.close();
System.out.println("input.txt file has been created");
} catch (Exception e) {
System.out.println(e.getMessage());
}
String outputFile="output.txt";
Scanner filescanner=null;
try {
//open file for reading
filescanner=new Scanner(new File(inputFile));
//open file for writing
writer=new PrintWriter(new File(outputFile));
//read until end of file
while(filescanner.hasNext()) {
int num=filescanner.nextInt();
//write 1 to 10 times of num to file output.txt
writer.printf("%-5d%-5d%-5d%-5d%-5d%-5d%-5d%-5d%-5d%-5d%-5d ",
num,
1*num,
2*num,
3*num,
4*num,
5*num,
6*num,
7*num,
8*num,
9*num,
10*num);
}
writer.close();
filescanner.close();
System.out.println("output.txt file has been created");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
------------------------------------------------------------------------------------------
Sample Output:
input.txt file has been created
output.txt file has been created
input.txt
44 8 16 81 43 42 83 81 63 11
output.txt
44 44 88 132 176 220 264 308 352 396 440
8 8 16 24 32 40 48 56 64 72 80
16 16 32 48 64 80 96 112 128 144 160
81 81 162 243 324 405 486 567 648 729 810
43 43 86 129 172 215 258 301 344 387 430
42 42 84 126 168 210 252 294 336 378 420
83 83 166 249 332 415 498 581 664 747 830
81 81 162 243 324 405 486 567 648 729 810
63 63 126 189 252 315 378 441 504 567 630
11 11 22 33 44 55 66 77 88 99 110
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.