Purpose Learn how to use Java input/output. Java Topics Java I/O, while loop, su
ID: 3588685 • Letter: P
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 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, you’ll 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 line’s 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.
We’ll learn soon how to line up the decimals so the numbers aren’t “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
Explanation / Answer
Given below is the completed code for the question. When the code is copied here in Chegg editor, the multiple spaces are all replaced by single space character . So you may have to format the output by including double space and 12 spaces where needed. You will find the comment line //12 spaces where you need 12 spaces to be appended. for rest of the places where you find " ", please use double space inside quotes.
Make sure you have the input file Main_04_Input.txt in your project folder (NOT inside src folder if using eclipse). Otherwise the file will not be found and you may get exception.
In case of any issues, post a comment. Hope the answer helped. If it did, please do rate the answer. Thank you.
/**
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
import java.text.DecimalFormat;
//### Rename your class and file name to Main_04
public class Main_04{
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);
DecimalFormat moneyFormat = new DecimalFormat(".00");
DecimalFormat rateFormat = new DecimalFormat(".0");
// ***** Begin program execution *****
// Read the input file and sum the numbers.
line = "Gross Pay Savings Rate Savings Amount IRA Rate IRA Amount" + " ";
line += "____________________________________________________________" ;
outputFile.println(line);
System.out.println(line);
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
boolean valid = true;
if(grossPay <= 0 || savingsRate <= 0 || iraRate <= 0)
{
valid = false;
}
// 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"
if(valid)
{
numValidLines++;
double savingsAmt = savingsRate * grossPay / 100;
double iraAmt = iraRate * grossPay / 100;
sumGrossPay += grossPay;
sumIra += iraAmt;
sumSavings += savingsAmt;
line = moneyFormat.format(grossPay) +" ";
line += rateFormat.format(savingsRate) + " ";
line += moneyFormat.format(savingsAmt) + " ";
line += rateFormat.format(iraRate) + " " ;
line += moneyFormat.format(iraAmt) ;
}
// 3. If not:
// (a) Write just the three input values in the correct columns
// (b) Write the same information to the console
else
{
line = moneyFormat.format(grossPay) +" ";
line += rateFormat.format(savingsRate) + " " ;
line += " " ; //12 spaces
line += rateFormat.format(iraRate) + " " ;
line += " " ; //12 spaces
}
outputFile.println(line);
System.out.println(line);
} // 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
*/
double avgGrossPay = 0, avgSavings = 0, avgIRA = 0;
if(numValidLines != 0)
{
avgGrossPay = sumGrossPay / numValidLines;
avgSavings = sumSavings / numValidLines;
avgIRA = sumIra /numValidLines;
}
moneyFormat = new DecimalFormat("###,###.00");
double total = sumSavings + sumIra;
line = "Number of data lines read: " + numInputLines + " ";
line += "Number of valid lines read: " + numValidLines + " ";
line += " Totals Averages" + " ";
line +=" ______ ________" + " ";
line += "Gross Pay " + moneyFormat.format(sumGrossPay) + " " + moneyFormat.format(avgGrossPay) + " ";
line += "Savings " + moneyFormat.format(sumSavings) + " " + moneyFormat.format(avgSavings) + " ";
line += "IRA " + moneyFormat.format(sumIra) + " " + moneyFormat.format(avgIRA) + " ";
line += "Savings + IRA " + moneyFormat.format(total) + " ";
outputFile.println(line);
System.out.println(line);
inputFile.close();
outputFile.close();
System.exit(0);
} // End main
} // End class
output
Gross Pay Savings Rate Savings Amount IRA Rate IRA Amount
____________________________________________________________
100.00 10.0 10.00 5.0 5.00
1000.00 11.0 110.00 6.0 60.00
1000.00 11.0 -6.0
1234.56 10.5 129.63 6.0 74.07
6543.21 10.0 654.32 5.0 327.16
10000.00 12.0 1200.00 10.0 1000.00
-1000.00 10.0 5.0
4444.22 10.2 453.31 4.9 217.77
2222.44 11.9 264.47 10.0 222.24
3141.59 12.0 376.99 5.0 157.08
Number of data lines read: 10
Number of valid lines read: 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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.