The Mathematical Association of America hosts an annual summer meeting. Each sta
ID: 3804478 • Letter: T
Question
The Mathematical Association of America hosts an annual summer meeting. Each state sends one official delegate to the section officers’ meeting at this summer session. The national organization reimburses the official state delegates according to the scale at the bottom of the page. Write a Java program to calculate the reimbursement values, satisfying the following:
Read data from a file and write data to a file. All data written to the output file should be echoed on the console. There should be only one input file and only one (separate) output file. The files are named, respectively,
YourName_S_06_Input.txt “S” is the section number
YourName_S_06_Output.txt
The first line of the input file contains an integer number of data values to process. After the first line of the input file, each line contains a real number which is the number of miles. Use a while loop to process the mileage values from the input file. Do NOT use an array.
Use an “if/else/if” construct and the scale below to calculate the mileage reimbursement if the input value is > 0. We’ll discuss an alternative method in class that you can use if you want.
The main method should declare all the variables at the start. Do not use a separate class for storage. Create at least the following methods, both called from main:
Method to print heading for the table
Method to output the summary information
Also use the leftPad method in the Toolkit to pad numbers.
Output the results to the file and to the console in a table format. (See the end of this document for a sample layout.) There should be a heading for each of the columns of the table. Print one line of output for each mileage value processed. The columns of the table should be lined up by the decimal point which can be done using the leftPad method. Each detail line of the table will contain the number of miles (double – print with one decimal place) and the reimbursement amount (double – print with two decimal places). If the input value is <= 0, output five stars in place of the reimbursement amount.
After all the data values have been processed, output to the file the total of the reimbursement values, the number of mileage values processed, and the number of mileage values that are > 0. Include phrases to identify each of the numbers.
Using a text editor, you will need to create an input file containing the following data – the file should contain one number per line. Use this data in the input file to your program. DO NOT change these numbers.
10 250.6 99.4 -2.78 999.4 799.4 1899.8 0 1300.2 1101.7 3333.3
(The first number is the number of numbers to process.)
Reimbursement scale:
Round trip mileage Rate
less than 400 miles 18 cents per mile
400, < 900 miles $ 65.00 plus 15 cents for each mile over 400
900 miles, < 1300 miles $115.00 plus 12 cents for each mile over 900
1300, < 1900 miles $140.00 plus 10 cents for each mile over 1300
1900, < 2600 miles $165.00 plus 8 cents for each mile over 1900
2600 miles $195.00 plus 6 cents for each mile over 2600
Be sure to appropriately document this program as has been done in previous assignments. Hand in printed copies (in this order) of the source code (with line numbers), your input file, and the output file.
Discussion
1. Outlining the program
2. Methods to use
3. How to test the program (sample input file: 1 250.6. I.E., use one number at a time in each scale range)
4. The leftPad method (time permitting: develop the method in class)
Explanation / Answer
import java.io.File;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.Scanner;
public class MileageReimbursement {
/**
* @param args
*/
public static void main(String[] args) {
printHeading();
Scanner scanner = null;
try {
scanner = new Scanner(new File("YourName_S_06_Input.txt"));
PrintWriter printWriter = new PrintWriter(new File(
"YourName_S_06_Output.txt"));
DecimalFormat format = new DecimalFormat("#.#");
printWriter.write(" Miles Reimbursement Amount ");
int n = scanner.nextInt();
for (int i = 0; i < n; i++) {
double miles = scanner.nextDouble();
System.out.printf("%.1f ", miles);
printWriter.write(format.format(miles) + " ");
if (miles <= 0) {
System.out.print("*****");
printWriter.write("***** ");
} else {
System.out.printf("%.1f", calculate(miles));
printWriter.write(format.format(calculate(miles)) + " ");
}
System.out.println();
}
printWriter.flush();
printWriter.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
if (scanner != null)
scanner.close();
}
}
/**
* method to calculate reimbursement amount according to rules
*
* @param miles
* @return
*/
public static double calculate(double miles) {
double amount = 0;
if (miles < 400) {
amount = miles * 18;
} else if (miles < 900) {
amount = 65 + (400 * 18) + ((miles - 400) * 15);
} else if (miles < 1300) {
amount = 115 + (900 * 18) + ((miles - 900) * 12);
} else if (miles < 1900) {
amount = 140 + (1300 * 18) + ((miles - 1300) * 10);
} else if (miles < 2600) {
amount = 165 + (1900 * 18) + ((miles - 1900) * 8);
} else {
amount = 195 + (2600 * 18) + ((miles - 2600) * 6);
}
return amount;
}
/**
* method to print the heading
*/
public static void printHeading() {
System.out.println("**********************************");
System.out.println("* Miles Reimbursement Amount *");
System.out.println("**********************************");
}
}
YourName_S_06_Input.txt
10 250.6 99.4 -2.78 999.4 799.4 1899.8 0 1300.2 1101.7 3333.3
YourName_S_06_Output.txt
Miles Reimbursement Amount
250.6 4510.8
99.4 1789.2
-2.8 *****
999.4 17507.8
799.4 13256
1899.8 29538
0 *****
1300.2 23542
1101.7 18735.4
3333.3 51394.8
OUTPUT:
*****************************************
* Miles Reimbursement Amount *
*****************************************
250.6 4510.8
99.4 1789.2
-2.8 *****
999.4 17507.8
799.4 13256.0
1899.8 29538.0
0.0 *****
1300.2 23542.0
1101.7 18735.4
3333.3 51394.8
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.