Orignal Java Code....need help for part 2 import java.util.Scanner; /* * To chan
ID: 3713999 • Letter: O
Question
Orignal Java Code....need help for part 2
import java.util.Scanner;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Topic11part1
* @author
*/
public class AutoManager {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//Creating an instance of Auto class
Auto auto=new Auto();
//calling the method
readAutoData(sc,auto);
System.out.println();
//Displaying the output
System.out.printf("%d %s has a %.1f gallon tank and gets %.1f miles per gallon ",auto.getModelYear(),auto.getMakeModel(),auto.getTankCapacity(),auto.getMileage());
}
//This method will read the input entered by the user
private static void readAutoData(Scanner sc, Auto auto) {
System.out.print("Enter auto model year :");
int year=sc.nextInt();
sc.nextLine();
System.out.print("Enter auto make and model :");
String make=sc.nextLine();
System.out.print("Enter auto mileage (in miles per gallon) :");
double mileage=sc.nextDouble();
System.out.print("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);
}
}
part 2
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author
*/
class Auto {
// Declaring instance variables
private int modelYear;
private String makeModel;
private double mileage;
private double tankCapacity;
// Zero argumented constructor
public Auto() {
this.modelYear = 0;
this.makeModel = "unknown";
this.mileage = 0.0;
this.tankCapacity = 0.0;
}
// 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;
}
}
Explanation / Answer
Auto.java
public class Auto {
public int modelYear;
public String modelMake;
public double mileage;
public double tankCapacity;
public final int CURRENT_YEAR = 2018;
public final int AVG_MILES = 12000;
public Auto(int modelYear, String modelMake, double mileage, double tankCapacity)
{
this.modelYear = modelYear;
this.modelMake = modelMake;
this.mileage = mileage;
this.tankCapacity = tankCapacity;
}
public Auto() {
this.modelYear = 0;
this.modelMake = "unknown";
this.mileage = 0.0;
this.tankCapacity = 0.0;
}
public void setModelYear(int modelYear) {
this.modelYear = modelYear;
}
public void setModelMake(String modelMake) {
this.modelMake = modelMake;
}
public void setMileage(double mileage) {
this.mileage = mileage;
}
public void setTankCapacity(double tankCapacity) {
this.tankCapacity = tankCapacity;
}
public int computeAvgMiles() {
int carYear = CURRENT_YEAR - modelYear;
return carYear * AVG_MILES;
}
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,modelMake,tankCapacity,mileage);
}
}
________________
AutoManager.java
import java.util.Scanner;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
public class AutoManager {
public Auto createAutoWithReadData(Scanner keyboard){
System.out.println("Enter auto model year:");
int modelYear = Integer.parseInt(keyboard.nextLine());
System.out.println("Enter auto make and model:");
String makeModel = keyboard.nextLine();
System.out.println("Enter auto mileage (in miles per gallon):");
double mileage = keyboard.nextDouble();
System.out.println("Enter auto tank capacity (in gallons):");
double tankCapacity = keyboard.nextDouble();
Auto Auto = new Auto(modelYear, makeModel, mileage, tankCapacity);
return Auto;
}
public static int computerRange(Auto autoObj)
{
return (int)(autoObj.mileage * autoObj.tankCapacity);
}
public static String computeAvgMiles(Auto autoObj)
{
return "On average, "+ autoObj.modelYear + " cars have approximately " + autoObj.computeAvgMiles() + " miles on the odometer";
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Scanner sc1 = new Scanner(System.in);
AutoManager autoManagerObj = new AutoManager();
Auto autoObj1 = autoManagerObj.createAutoWithReadData(sc);
System.out.println();
Auto autoObj2 = autoManagerObj.createAutoWithReadData(sc1);
autoObj1.displayAutoData();
System.out.println();
System.out.println(computeAvgMiles(autoObj1));
System.out.println(autoObj1.modelYear + " " + autoObj1.modelMake + " has a " + autoObj1.tankCapacity + " gallon tank and gets " + autoObj1.mileage + " miles per gallon");
System.out.println("Range is " +computerRange(autoObj1)+ " miles");
System.out.println();
autoObj2.displayAutoData();
System.out.println();
System.out.println(computeAvgMiles(autoObj2));
System.out.println(autoObj2.modelYear + " " + autoObj2.modelMake + " has a " + autoObj2.tankCapacity + " gallon tank and gets " + autoObj2.mileage + " miles per gallon");
System.out.println("Range is " +computerRange(autoObj2) + " miles");
System.out.println();
if ((autoObj1.mileage * autoObj1.tankCapacity) > (autoObj2.mileage * autoObj2.tankCapacity))
System.out.println(autoObj1.modelYear + " " + autoObj1.modelMake + " " + "has a longer range");
else
System.out.println(autoObj2.modelYear + " " + autoObj2.modelMake + " " + "has a longer range");
}
}
__________________
Output:
Enter auto model year:
2010
Enter auto make and model:
Honds Accord
Enter auto mileage (in miles per gallon):
27.5
Enter auto tank capacity (in gallons):
15.5
Enter auto model year:
2014
Enter auto make and model:
Chevrolet Tahoe
Enter auto mileage (in miles per gallon):
19.2
Enter auto tank capacity (in gallons):
26.1
2010 Honds Accord has a 15.5 gallon tank and gets 27.5 miles per gallon
On average, 2010 cars have approximately 96000 miles on the odometer
2010 Honds Accord has a 15.5 gallon tank and gets 27.5 miles per gallon
Range is 426 miles
2014 Chevrolet Tahoe has a 26.1 gallon tank and gets 19.2 miles per gallon
On average, 2014 cars have approximately 48000 miles on the odometer
2014 Chevrolet Tahoe has a 26.1 gallon tank and gets 19.2 miles per gallon
Range is 501 miles
2014 Chevrolet Tahoe has a longer range
_______________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.