Question: Purpose Learn how to use Java input/output. Java Topics Java I/O, while loop, summing for a total and average. References Textbook use the index for
Purpose Learn how to use Java input/output.
Java Topics Java I/O, while loop, summing for a total and average.
References Textbook use the index for relevant topics.
Specification
This program uses files for input and output and is based on the template file, Main_04_Template.java, discussed in class and included in FLIP_FILES.zip.
The names of the input and output files are, respectively:
Main_04_Input.txt
Main_04_Output.txt
The input file, uploaded to ZyLabs, has this information in order of gross pay, savings percentage rate and IRA investment rate:
100.00 10.0 5.0
1000.00 11 6
1000.00 11 -6
1234.56 10.5 6.0
6543.21 10 5.0
10000 12 10
-1000 10.00 5.00
4444.22 10.2 4.9
2222.44 11.9 10
3141.59 12 5.0
Do not change the numbers. Include the usual comments in the header area at the start of your program.
Insert code that calculates the savings amount and the IRA investment amount from the numbers for valid lines that are read from the input file. You will also accumulate sums to calculate the averages of the gross pay, savings amount and investment amount for all input lines that are valid. Valid input lines have all three input numbers greater than 0. To calculate the averages after you finish reading the input file, youll need to count the number of valid input lines and keep running totals of gross pay, savings amount and IRA amount. Output the numbers read and the calculated amounts to the output file and to the console in tabular form as shown below.
If one or more input values is <= 0, output just the input values and not the calculated amounts.
Here is an example using the numbers above. Note that dollar amounts should be formatted with DecimalFormat to two decimal places and percentage rates to one decimal place. Use two spaces between each field. If one lines data is invalid, use 12 spaces between the savings rate and the IRA rate. Use the underscore character, _, for underlining the heading. When calculating the averages, ensure that the denominator is not zero. How do you check for a zero denominator?
Here is how the beginning of the output should look:
Gross Pay Savings Rate Savings Amount IRA Rate IRA Amount
_________ ____________ ______________ ________ __________
100.00 10.0 10.00 5.0 5.00
500.00 -5.0 8.0
1000.00 11.0 110.00 6.0 60.00
etc.
Well learn soon how to line up the decimals so the numbers arent ragged.
After reading all the numbers and outputting the relevant data for each output line, output to the output file and to the console these nine data values:
1. The total number of input lines read.
2. The total number of valid input lines read.
3. The sums of the gross pay, savings amount and IRA amount for valid lines only.
4. The averages of the gross pay, savings amount and IRA amount, where the average is calculated only for valid input lines.
5. The total of the savings amounts and IRA amounts, where the total is calculated only for valid input lines.
The summary data output should look like this:
Number of data lines read: 10
Number of valid data lines: 8
Totals Averages
_________ ________
Gross Pay 28,686.02 3,585.75
Savings 3,198.72 399.84
IRA 2,063.32 257.92
Savings+IRA 5,262.05
/**
File: Main_04_Template.java - a starting point for Assignment 4
Read a file of numbers, calculate sums and averages
Input File Main_04_Input.txt has numbers of type double for gross pay,
savings rate and IRA investment rate, one set per line
Process Read the data values, calculate savings and IRA investment amounts,
and sum them
Output A file with the original data values, one line for each input line as
shown in the assignment specification. After the detail lines are
displayed, display the summary information as listed in the spec.
The output file is named Main_04_Output.txt
Note Without your added code, the program will display the number
of numbers in the input file.
*/
import java.util.Scanner; // Access the Scanner class
import java.io.*; // Access PrintWriter and related classes
//### Rename your class and file name to Main_04
public class Main_04_Template {
public static void main(String[] args) throws IOException {
// Declare variables
// Define your file names on the next two lines as needed.
final String INPUT_FILE = "Main_04_Input.txt";
final String OUTPUT_FILE = "Main_04_Output.txt";
int numInputLines = 0; // Number of lines in the input file
int numValidLines = 0; // Number of valid lines in the input file
double grossPay = 0.0; // Input file's gross pay
double savingsRate = 0.0; // Input file's savings rate
double iraRate = 0.0; // Input file's IRA investment rate
double sumGrossPay = 0.0; // Sum of all valid gross pay amounts
double sumSavings = 0.0; // Sum of all valid savings amounts
double sumIra = 0.0; // Sum of all valid IRA investment amounts
String line = ""; // Output one line to two or more output areas
//### Add variables to calculate the averages
// Access the input/output files
File inputDataFile = new File(INPUT_FILE);
Scanner inputFile = new Scanner(inputDataFile);
FileWriter outputDataFile = new FileWriter(OUTPUT_FILE);
PrintWriter outputFile = new PrintWriter(outputDataFile);
// ***** Begin program execution *****
// Read the input file and sum the numbers.
while (inputFile.hasNext()) {
numInputLines++;
grossPay = inputFile.nextDouble();
savingsRate = inputFile.nextDouble();
iraRate = inputFile.nextDouble();
//### Add code here to:
// 1. Determine whether the input data is valid
// 2. If so:
// (a) Calculate savings and IRA investment amounts
// (b) Add those amounts and gross pay to running totals
// (c) Write the spec'd information to the output file
// (d) Write the same information to the console
// (use System.out.println). This is called "Echoing the input"
// 3. If not:
// (a) Write just the three input values in the correct columns
// (b) Write the same information to the console
} // End while
/*
//### Here, the while loop has ended, meaning we've read the entire file
Add code here to output in the format shown in the specification:
1. The total number of input lines read (included in this template)
2. The total number of valid input lines
3. The sums of the gross pay, savings amount and IRA amount for valid
lines only
4. The averages of the gross pay, savings amount and IRA amount for
valid lines only
5. Close the input file
6. Close the output file
*/
line = "The number of input lines is " + numInputLines
+ " Note: Remove these print statements and replace them"
+ " with the required otuput statements.";
outputFile.println(line);
System.out.println(line);
outputFile.close();
System.exit(0);
} // End main
} // End class
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
