Question: JAVA JAVA JAVA JAVA JAVA JAVA Hello, I have one error that I need to be fixed in the code below: NOTE: The error is

JAVA JAVA JAVA JAVA JAVA JAVA

Hello, I have one error that I need to be fixed in the code below:

NOTE: The error is bolded in my output and expected output.

INPUT:

2016 Honda Accord 27.5 15.5 2015 Chevrolet Tahoe 19.2 26.1

MY OUTPUT:

Enter auto model year: Enter auto make and model: Enter auto mileage (in miles per gallon): Enter auto tank capacity (in gallons):

Enter auto model year: Enter auto make and model: Enter auto mileage (in miles per gallon): Enter auto tank capacity (in gallons):

On average, 2016 cars have approximately 24000 miles on the odometer 2016 Honda Accord has a 15.5 gallon tank and gets 27.5 miles per gallon Range is 426 miles

On average, 2015 cars have approximately 24000 miles on the odometer 2015 Chevrolet Tahoe has a 26.1 gallon tank and gets 19.2 miles per gallon Range is 501 miles

2015 Chevrolet Tahoe has a longer range

Expected Output:

Enter auto model year: Enter auto make and model: Enter auto mileage (in miles per gallon): Enter auto tank capacity (in gallons):

Enter auto model year: Enter auto make and model: Enter auto mileage (in miles per gallon): Enter auto tank capacity (in gallons):

On average, 2016 cars have approximately 24000 miles on the odometer 2016 Honda Accord has a 15.5 gallon tank and gets 27.5 miles per gallon Range is 426 miles

On average, 2015 cars have approximately 36000 miles on the odometer 2015 Chevrolet Tahoe has a 26.1 gallon tank and gets 19.2 miles per gallon Range is 501 miles

2015 Chevrolet Tahoe has a longer range

import java.util.Scanner;

public class AutoManager {

public static void main(String[] args) {

/*

*

* Creating an Scanner class object which is used to get the inputs

*

* entered by the user

*

*/

Scanner sc = new Scanner(System.in);

// Creating an instance of Auto class

Auto auto1 = new Auto();

// calling the method

readAutoData(sc, auto1);

System.out.println();

// Displaying the output As requirement comment the following lines.F

// System.out.printf("%d %s has a %.1f gallon tank and gets %.1f miles per

// gallon ", auto.getModelYear(),

// auto.getMakeModel(), auto.getTankCapacity(), auto.getMileage());

Auto auto2 = createAutoWithReadData(sc);

System.out.println();

int auto1Range = (int) auto1.computeRange();

int auto2Range = (int) auto2.computeRange();

double auto1AvgMiles = auto1.computeAvgMiles();

System.out.printf("On average, %d cars have approximately %.0f miles on the odometer ", auto1.getModelYear(),

auto1AvgMiles);

auto1.displayAutoData();

System.out.println("Range is " + auto1Range + " miles");

System.out.println();

double auto2AvgMiles = auto2.computeAvgMiles();

System.out.printf("On average, %d cars have approximately %.0f miles on the odometer ", auto2.getModelYear(),

auto1AvgMiles);

auto2.displayAutoData();

System.out.println("Range is " + auto2Range + " miles");

System.out.println();

if (auto2Range > auto1Range) {

System.out.println(auto2.getModelYear() + " " + auto2.getMakeModel() + " has a longer range");

} else if (auto2Range < auto1Range) {

System.out.println(auto1.getModelYear() + " " + auto1.getMakeModel() + " has a longer range");

} else {

System.out.println("Range of both vehicle is same.");

}

}

// This method will read the input entered by the user

/**

*

* @param sc

* Scanner to get User input

* @return newly created Auto object

*/

private static Auto createAutoWithReadData(Scanner sc) {

System.out.println("Enter auto model year:");

int year = sc.nextInt();

sc.nextLine();

System.out.println("Enter auto make and model:");

String model = sc.nextLine();

System.out.println("Enter auto mileage (in miles per gallon):");

double mileage = sc.nextDouble();

System.out.println("Enter auto tank capacity (in gallons):");

double capacity = sc.nextDouble();

Auto newAuto = new Auto(year, model, mileage, capacity);// call Auto constructor with value obtained from user

// and get a new Auto object. Return that to main

// method.

return newAuto;

}

/**

*

* @param sc Scanner object to read user input

* @param auto Object in which value will be set after getting user input

*/

private static void readAutoData(Scanner sc, Auto auto) {

System.out.println("Enter auto model year:");

int year = sc.nextInt();

sc.nextLine();

System.out.println("Enter auto make and model:");

String make = sc.nextLine();

System.out.println("Enter auto mileage (in miles per gallon):");

double mileage = sc.nextDouble();

System.out.println("Enter auto tank capacity (in gallons):");

double gallons = sc.nextDouble();

// calling the setters

auto.setModelYear(year);

auto.setMakeModel(make);

auto.setMileage(mileage);

auto.setTankCapacity(gallons);

}

}

public class Auto {

// Declaring instance variables

private int modelYear;

private String makeModel;

private double mileage;

private double tankCapacity;

// Zero argument constructor

public Auto() {

this.modelYear = 0;

this.makeModel = "unknown";

this.mileage = 0.0;

this.tankCapacity = 0.0;

}

public Auto(int year, String model, double mileage, double capacity) {

this.modelYear = year;

this.makeModel = model;

this.mileage = mileage;

this.tankCapacity = capacity;

}

// getters and setters

public int getModelYear() {

return modelYear;

}

public void setModelYear(int modelYear) {

this.modelYear = modelYear;

}

public String getMakeModel() {

return makeModel;

}

public void setMakeModel(String makeModel) {

this.makeModel = makeModel;

}

public double getMileage() {

return mileage;

}

public void setMileage(double mileage) {

this.mileage = mileage;

}

public double getTankCapacity() {

return tankCapacity;

}

public void setTankCapacity(double tankCapacity) {

this.tankCapacity = tankCapacity;

}

public double computeAvgMiles() {

final int CURRENT_YEAR = 2018;

final double MILES_PER_YEAR = 12000;

int age = CURRENT_YEAR - modelYear;

return age * MILES_PER_YEAR;

}

public double computeRange() {

return (mileage * tankCapacity);

}

public void displayAutoData() {

System.out.printf("%d %s has a %.1f gallon tank and gets %.1f miles per gallon ", modelYear, makeModel,

tankCapacity, mileage);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!