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

Purpose Use Jgrasp, Calculate mileage reimbursements using arrays and methods. S

ID: 3600429 • Letter: P

Question

Purpose Use Jgrasp, Calculate mileage reimbursements using arrays and methods.

Submissions Via ZyLabs.

Java Topics Arrays, Java I/O, while loops, if-else-if, methods, summing/averaging data.

                        Use of leftPad in the Toolkit class.

References   Textbook – use the index for relevant topics.

                        The programming standards and guidelines as discussed in class.

                        Toolkit.java

Specification

Convert the previous mileage reimbursement 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, Main_08_Input.txt. 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 from the method.

3.   A void 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. Note that the reimbursement is calculated into the reimbursement array parameter and is not returned as a return type.

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 assignment was

Purpose          Calculate mileage reimbursement based on a table.

Due Date         Per the Course at a Glance. Cannot be resubmitted.

Submissions   Via ZyLabs.

Java Topics    Java I/O, while loops, if-else-if statements, summing and averaging data. Use of leftPad in the Toolkit class.

References     Textbook – use the index for relevant topics.

                        Toolkit.java (must be stored in the same folder as your program)

Specification

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,

Main_06_Input.txt

Main_06_Output.txt

The first entry of the input file contains an integer number of data values to process (and it is not a mileage value!). After that first entry are real (double) numbers that are the number of miles traveled. 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. If the mileage value is <= 0, the reimbursement is 0.

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 format and 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.

The input data is in file Main_06_Input.txt which has been uploaded to ZyLabs. You can use this file for testing; it is found in Blackboard > Assignments. The data is:

      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.)

DO NOT change these numbers.

Here is the 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 your program as has been done in previous assignments.

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)

___________________________________________________________________________

Sample Tabular Layout for the Report

Mileage            Reimbursement

----------           ---------------------

   250.6            (calc’d reimbursement)

     99.4            (calc’d reimbursement)

     -2.8                    *****

etc.

Follow the detail lines with the summary inform

Explanation / Answer

//Source code//

package chegg;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.util.Scanner;

public class JavaArray {

static int length; //number of element scanned from the file

public static void explain() //method that explains the program to user

{

System.out.println("This program takes input on the milage from the user and calculate the milage and avarage milage");

}

public static void takeMilage(double milage[]) throws FileNotFoundException // scan file and store in milage array

{

Scanner scanner = new Scanner(new FileReader("Main_08_Input.txt"));

int i=0;

while(scanner.hasNext())

{

milage[i]=scanner.nextDouble();

i++;

length++;

}

}

public static void calculateReimbursement(double milage[], double reimbursement[]) //calculate reimbursement

{

int i=0;

for(i=0;i<length;i++)

{

if(milage[i]<0)

reimbursement[i]=0;

else if(milage[i]<400)

reimbursement[i]=(milage[i]*18/100);

else if(milage[i]>=400 && milage[i]<900)

reimbursement[i]=65+((milage[i]-400)*15/100);

else if(milage[i]>=900 && milage[i]<1300)

reimbursement[i]=115+((milage[i]-900)*12/100);

else if(milage[i]>=1300 && milage[i]<1900)

reimbursement[i]=140+((milage[i]-1300)*10/100);

else if(milage[i]>=1900 && milage[i]<2600)

reimbursement[i]=165+((milage[i]-1900)*8/100);

else if(milage[i]>=2600)

reimbursement[i]=195+((milage[i]-2600)*6/100);

}

}

public static void printHeading() //print the heading of the table

{

System.out.println("Milage Reimbursement");

System.out.println("------- ---------------");

}

public static void printTable(double milage[], double reimbursement[]) //print the content of the table

{

int i=0;

for(i=0;i<length;i++)

{

if(milage[i]<0)

System.out.println(String.format("%.2f",milage[i])+" *******"); //formatting to get precission in this case. Leftpad only used to trim leading 0 from integer

else

System.out.println(String.format("%.2f",milage[i]) + " "+ String.format("%.2f",reimbursement[i]));

}

}

public static double getAvgReimbursement(double milage[], double reimbursement[]) // average reimbursement

{

double avg =0.0;

int count=0;

for(int i=0;i<length;i++)

{

if(milage[i]>0)

{

avg+=reimbursement[i];

count++;

}

}

return avg/count;

}

public static double getAvgMile(double milage[]) //average milage

{

double avg =0.0;

int count=0;

for(int i=0;i<length;i++)

{

if(milage[i]>0)

{

avg+=milage[i];

count++;

}

}

return avg/count;

}

public static void printSummary(double milage[], double reimbursement[])

{

double total_reimbursement = 0;

int count=0;

double total_milage=0;

for(int i=0;i<length;i++)

{

if(milage[i]>0)

{

total_milage+=milage[i];

total_reimbursement += reimbursement[i];

count++;

}

}

System.out.println("----Summary------");

System.out.println("Total Reimbursement "+total_reimbursement);

System.out.println("number of Milage processed "+length);

System.out.println("number of Milage processed with greater than 0 value "+count);

System.out.println("Total positive milage "+total_milage);

System.out.println("Average Milage "+getAvgMile(milage));

System.out.println("Avarage Reimbursement "+getAvgReimbursement(milage, reimbursement));

}

public static void main(String args[]) throws FileNotFoundException

{

explain();

double milage[] = new double[20]; //assuming file have less than 20 element you can change it accordingly

double reimbursement[] = new double[20];

takeMilage(milage);

calculateReimbursement(milage, reimbursement);

printHeading();

printTable(milage, reimbursement);

printSummary(milage, reimbursement);

}

}

//Sample output//

This program takes input on the milage from the user and calculate the milage and avarage milage

Milage Reimbursement

------- ---------------

10.00 1.80

250.60 45.11

99.40 17.89

-2.78 *******

999.40 126.93

799.40 124.91

1899.80 199.98

0.00 0.00

1300.20 140.02

1101.70 139.20

3333.30 239.00

----Summary------

Total Reimbursement 1034.8400000000001

number of Milage processed 11

number of Milage processed with greater than 0 value 9

Total positive milage 9793.8

Average Milage 1088.1999999999998

Avarage Reimbursement 114.98222222222223

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote