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

JAVA (Gas Mileage) Drivers are concerned with the mileage their automobiles get.

ID: 3651815 • Letter: J

Question

JAVA (Gas Mileage) Drivers are concerned with the mileage their automobiles get. One driver has kept track of several trips by recording the miles driven and gallons used for each tankful. Develop a Java application that will input the miles driven and gallons used (both as integers) for each trip. The program should calculate and display the miles per gallon obtained for each trip and print the combined miles per gallon obtained for all trips up to this point. All averaging calculations should produce floating-point results. Use class Scanner and sentinel-controlled repetition to obtain the data from the user.

Explanation / Answer

import java.util.Scanner; public class Trip { /** * @param args */ int milesDriven; int gallon; Trip(){ setMilesDriven(); setGallon(); calculateMilesPerGallon(); } public int getMilesDriven() { return milesDriven; } public void setMilesDriven() { System.out.println("Enter the driven miles"); Scanner input = new Scanner(System.in); milesDriven=input.nextInt(); } public int getGallon() { return gallon; } public void setGallon() { System.out.println("enter the gallon used"); Scanner input = new Scanner(System.in); gallon=input.nextInt(); } void calculateMilesPerGallon(){ System.out.println("Miles per gallon for is "+(float)(getMilesDriven()/getGallon())); } public static void main(String[] args) { Trip T1=new Trip(); Trip T2=new Trip(); Trip T3=new Trip(); Trip T4=new Trip(); float total=(T1.getMilesDriven()/T1.getGallon())+(T2.getMilesDriven()/T2.getGallon())+ (T3.getMilesDriven()/T3.getGallon())+(T4.getMilesDriven()/T4.getGallon()); System.out.println("Combined Miles per gallon for is"+total); } }