Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Convert the previous assignment using the mileage table to a program that uses a

ID: 3810717 • Letter: C

Question

Convert the previous assignment using the mileage table to a program that uses arrays. You will use two parallel one-dimensional arrays of length n, where n is the number of mileage values to process. Note that n is determined by reading the first entry in the input file which has the number of entries that follow.

The main purpose of this assignment is to practice using arrays in methods. Therefore, no arrays or related counts and averages should be declared globally (static before the main program begins.) You can declare the Toolkit and constants like tab and new line as global objects if you want.

All output is written to the output file and to the console.

One array will hold the mileage and the other the corresponding reimbursement amount. Use the same input file as for the previous mileage program assignment but rename it to this assignment number. Your program must satisfy the requirements of the previous program, plus the items below. The main program should be mostly (but not necessarily only) method calls with appropriate parameters.

1.   A method that explains the program to the user.

2.   A method with the mileage array as a parameter that reads the mileage values and stores them in the appropriate array. (Use a ‘while’ loop that includes a test for the end of file and the number of elements allocated to the array.) This method doesn’t calculate the reimbursement; it only reads the data. From this method, determine the number of elements in the possibly partially-filled array and return that number.

3.   A method with the two arrays and the number read as parameters that calculates the array of reimbursement amounts. A ‘for’ loop must be used for the calculations. If the mileage value is negative or 0, the corresponding reimbursement amount should be zero.

4.   A method prints the heading and another method prints the detail lines in the table. The latter method will have the two arrays and the number read as parameters. Use a ‘for’ loop. Detail lines are as specified in the previous mileage program assignment.

   Each detail line of the table contains the number of miles (real, print with one decimal place) and the reimbursement amount (real, print with two decimal places). If the input value is <= 0, output five stars instead of the reimbursement amount. As in the earlier assignment, use the leftPad method to format the numbers to one or two decimal places as needed.

5.   After the array of reimbursements has been calculated, a method or methods calculate the average reimbursement and the average number of miles traveled. A ‘for’ loop must be used in doing the calculations. These averages will be the averages for mileage values which are > 0. In other words, you might divide by a number less than the number of elements (which, by the way, could be 0, so check for it before you calculate the average).

6.   A method outputs the summary information at the end of the table. This output should include all the output specified in the previous mileage assignment (the total of the reimbursement values, the number of mileage values processed, and the number of mileage values that were > 0). In addition, output the total of the mileage values that were > 0, and the two averages calculated in the previous step, all with appropriate messages.

Previous program

1 import java.io.File;
2 import java.io.PrintWriter;
3 import java.text.DecimalFormat;
4 import java.util.Scanner;
5
6 public class MileageCalc {
7
8
9 public static void main(String[] args) throws Exception {
10
11 printHeading();
12
13 Scanner scanner = null;
14
15 scanner = new Scanner(new File("sampleInput.txt"));
16 PrintWriter printWriter = new PrintWriter(new File(
17 "sampleOutput.txt"));
18
19 DecimalFormat format = new DecimalFormat("#.#");
20
21 printWriter.write(" Miles Reimbursement Amount ");
22
23 int n = scanner.nextInt();
24 int i =0;
25
26 while(i<n){
27
28 double miles = scanner.nextDouble();
29
30 System.out.print(miles);
31 printWriter.write(format.format(miles) + " ");
32
33 if (miles <= 0) {
34 System.out.print("*****");
35 printWriter.write("***** ");
36
37 } else {
38
39 System.out.print(calculate(miles));
40 printWriter.write(format.format(calculate(miles)) + " ");
41
42 }
43
44 System.out.println();
45
46 i++;
47
48 }
49
50 printWriter.close();
51
52 }
53
54
55 public static double calculate(double miles) {
56
57 double amount = 0;
58
59 if (miles < 400) {
60 amount = miles * 18;
61
62 } else if (miles < 900) {
63 amount = 65 + (400 * 18) + ((miles - 400) * 15);
64
65 } else if (miles < 1300) {
66 amount = 115 + (900 * 18) + ((miles - 900) * 12);
67
68 } else if (miles < 1900) {
69 amount = 140 + (1300 * 18) + ((miles - 1300) * 10);
70
71 } else if (miles < 2600) {
72 amount = 165 + (1900 * 18) + ((miles - 1900) * 8);
73
74 } else {
75
76 amount = 195 + (2600 * 18) + ((miles - 2600) * 6);
77 }
78
79 return amount;
80 }
81
82
83 public static void printHeading() {
84
85 System.out.println("******************************");
86 System.out.println("* Miles Reimbursement Amount *");
87 System.out.println("******************************");
88 System.out.println(" Mileage Reimbursement");
89 System.out.println(" ------- -------------");
90 }
91 }

Explanation / Answer

Here is the code for you....

import java.io.File;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.Scanner;

public class MileageCalc {


public static void main(String[] args) throws Exception {
  
printHeading();

Scanner scanner = null;
scanner = new Scanner(new File("sampleInput.txt"));
int n = scanner.nextInt();

  

// You will use two parallel one-dimensional arrays of length n, where n is the
// number of mileage values to process.
// One array will hold the mileage and the other the corresponding reimbursement amount.
double[] miles = new double[n];
double[] reimbursement = new double[n];
int i =0;   
   n = readMiles(miles, scanner);
//1. Explanation.
printExplanation();
//2. Stores miles.
int size = readMiles(miles, scanner);
//3. Calculate reimbursements.
calculate(miles, reimbursement, size);
//4. Print heading and details.
printHeading();
printDetails(miles, reimbursement, size);
//5. Summary.
double milesAvg = avgMiles(miles, size);
double reimbursementAvg = avgReimbursement(reimbursement, size);

}


//3. A method with the two arrays and the number read as parameters that calculates
//the array of reimbursement amounts. A ‘for’ loop must be used for the calculations.
//If the mileage value is negative or 0, the corresponding reimbursement amount should be zero.
public static void calculate(double[] miles, double[] amount, int size) {

for(int i = 0; i < size; i++) {
        if(miles[i] < 0)
            amount[i] = 0;
        else if (miles[i] < 400)
            amount[i] = miles[i] * 18;
        else if (miles[i] < 900)
           amount[i] = 65 + (400 * 18) + ((miles[i] - 400) * 15);
        else if (miles[i] < 1300)
           amount[i] = 115 + (900 * 18) + ((miles[i] - 900) * 12);
        else if (miles[i] < 1900)
           amount[i] = 140 + (1300 * 18) + ((miles[i] - 1300) * 10);
        else if (miles[i] < 2600)
           amount[i] = 165 + (1900 * 18) + ((miles[i] - 1900) * 8);
        else
           amount[i] = 195 + (2600 * 18) + ((miles[i] - 2600) * 6);
}
}


//4. A method prints the heading and another method prints the detail lines in the
//table. The latter method will have the two arrays and the number read as parameters.
//Use a ‘for’ loop.
//Detail lines are as specified in the previous mileage program assignment.
public static void printHeading() {

System.out.println("******************************");
System.out.println("* Miles Reimbursement Amount *");
System.out.println("******************************");
System.out.println(" Mileage Reimbursement");
System.out.println(" ------- -------------");
}
//Each detail line of the table contains the number of miles (real, print with one
//decimal place) and the reimbursement amount (real, print with two decimal places).
//If the input value is <= 0, output five stars instead of the reimbursement amount.
//As in the earlier assignment, use the leftPad method to format the numbers to one
//or two decimal places as needed.
public static void printDetails(double[] miles, double[] amount, int size) throws Exception{
   PrintWriter printWriter = new PrintWriter(new File(
"sampleOutput.txt"));

DecimalFormat format = new DecimalFormat("#.#");

printWriter.write(" Miles Reimbursement Amount ");
for(int i = 0; i < size; i++) {
       System.out.print(miles[i]);
       printWriter.write(format.format(miles[i]) + " ");
       if (miles[i] <= 0) {
           System.out.print("*****");
           printWriter.write("***** ");
}
else {
   System.out.print(amount[i]);
   printWriter.write(format.format(amount[i]) + " ");
}
System.out.println();
}
}
  
//5. After the array of reimbursements has been calculated, a method or methods
//calculate the average reimbursement and the average number of miles traveled. A
//‘for’ loop must be used in doing the calculations. These averages will be the
//averages for mileage values which are > 0. In other words, you might divide by a
//number less than the number of elements (which, by the way, could be 0, so check
//for it before you calculate the average).
public static double avgReimbursement(double[] amount, int size) {
   double avg = 0;
   int count = 0;
   for(int i = 0; i < size; i++)
       if(amount[i] > 0) {
           avg += amount[i];
           count++;
       }
   return avg / count;  
}
  
public static double avgMiles(double[] miles, int size) {
   double avg = 0;
   int count = 0;
   for(int i = 0; i < size; i++)
       if(miles[i] > 0) {
           avg += miles[i];
           count++;
       }
   return avg / count;  
}
  
//1. A method that explains the program to the user.
public static void printExplanation() {
   System.out.println("This application reads the miles from the input file");
   System.out.println("sampleInput.txt, then will calculate the amount to be");
   System.out.println("reimbursed to the customer. The mileage and reimbursement");
   System.out.println("will be printed to the screen, and also will be written to");
   System.out.println("the file named: sampleOutput.txt");
}
  
//2. A method with the mileage array as a parameter that reads the mileage values
//and stores them in the appropriate array. (Use a ‘while’ loop that includes a test
//for the end of file and the number of elements allocated to the array.) This method
//doesn’t calculate the reimbursement; it only reads the data. From this method,
//determine the number of elements in the possibly partially-filled array and
//return that number.
public static int readMiles(double[] miles, Scanner scanner) {
   //scanner = new Scanner(new File("sampleInput.txt"));   
   int count = 0;
   int n = miles.length;
   while(scanner.hasNext() && count < n) {
       miles[count++] = scanner.nextDouble();
   }
   return count;
}
}