I need help extending the code below: JAVA JAVA JAVA JAVA JAVA JAVA Within the A
ID: 3712137 • Letter: I
Question
I need help extending the code below:
JAVA JAVA JAVA JAVA JAVA JAVA
Within the Auto class (used to create Auto type objects):
? Include top of file comments with descriptions and an @author tag
? Add a second constructor that:
o Has parameters for each data field/member of the class (model year, make/model, mileage,
and tank capacity, in that order)
o Uses the parameter values to initialize the data fields.
? Add the following additional methods to the Auto class (must use these exact method names):
o computeAvgMiles()- public method to compute average miles on a car for its age
? Create a constant to hold the current year (2018).
? Create a constant to hold the average of 12,000 miles per year old.
? Use the model year to compute the car’s age and then use the age to compute the
car’s average miles for its age.
This method will have no parameters, and will return the car’s avg miles for its age.
o computeRange() - public method to compute the car’s range (miles per tank)
This method will have no parameters, and will return the car’s range
o displayAutoData() - public method to all display the data field values,
formatted as in Topic11part1
This method will have no parameters and will not return anything.
? Include method comments above each method, with method descriptions, @return tags, and
@param tags
Within the AutoManager class:
? Include top of file comments with descriptions and an @author tag
? Include method comments above each method, with method descriptions, @return tags, and
@param tags
? Add another method called createAutoWithReadData(). This method will:
o Have one parameter:
? a Scanner object (to read data from the keyboard)
o Prompt the user for the data for the automobile (year, make/model, mileage, and tank
capacity, in that order), and read the user input.
o Define and instantiate an Auto object using the second constructor, with the values read
from the user as the arguments for the constructor parameters.
o Return the Auto object.
Within the AutoManager class’ main method:
? After the code that creates the first Auto object and calls the readAutoData() method,
DELETE the code that uses the getters to display the values of each data field of the Auto object.
? Display a blank line.
? Add code to call the createAutoWithReadData() method to create a second Auto object.
Note that you will have to capture the object that is returned from the method, and store it into an
Auto object variable.
Sample call:
Auto autoObjectName = createAutoWithReadData(scannerName);
? Display a blank line.
? Add code to call the computerRange() method twice (once with each auto object), to compute
the cars’ ranges, and save the returned values in integer variables.
o HINT: Use cast operator to store the returned value to an integer.
? Add code to call the computeAvgMiles() method with the first Auto object and display the value
returned in the format:
On average, 2010 cars have approximately 96000 miles on the odometer
? Add code to call the displayAutoData() method with the first Auto object.
? Display the range that was computed for the first Auto.
? Display a blank line.
? Add code to call the computeAvgMiles() method with the second Auto object and display the
value returned using the same format as the first Auto object.
? Add code to call the displayAutoData() method with the second Auto object.
? Display the range that was computed for the second Auto.
? Display a blank line.
? Finally, add code to determine which car has a longer range,
and display the results in one of the following formats,
using getters to access the Auto object’s data:
Year make model has a longer range
The range of both vehicles is the same
Sample run:
Enter auto model year:
2010
Enter auto make and model:
Honda 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
On average, 2010 cars have approximately 96000 miles on the odometer
2010 Honda Accord has a 15.5 gallon tank and gets 27.5 miles per gallon
Range is 426 miles
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
Explanation / Answer
Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
Auto.java
---------
public 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;
}
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 tank of %.1f gallons and gets %.1f miles per gallon. ",
modelYear, makeModel, tankCapacity, mileage);
}
}
AutoManager.java
----------------
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 auto = new Auto();
// calling the method
readAutoData(sc, auto);
System.out.println();
// Displaying the output
Auto auto2 = createAutoWithReadData(sc);
System.out.println();
int range1 = (int)auto.computeRange();
int range2 = (int)auto2.computeRange();
System.out.printf("On an average, %d cars have approximately %.1f miles on the odometer ", auto.getModelYear(), auto.computeAvgMiles());
auto.displayAutoData();
System.out.printf("Range is %d miles ", range1);
System.out.println();
System.out.printf("On an average, %d cars have approximately %.1f miles on the odometer ", auto2.getModelYear(), auto2.computeAvgMiles());
auto2.displayAutoData();
System.out.printf("Range is %d miles ", range2);
System.out.println();
if(range1 > range2)
System.out.printf("%d %s has a longer range ", auto.getModelYear(), auto.getMakeModel());
else if(range2 > range1)
System.out.printf("%d %s has a longer range ", auto2.getModelYear(), auto2.getMakeModel());
else
System.out.println("The range of both vehicles is the same");
}
// This method will read the input entered by the user
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);
}
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 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();
Auto auto = new Auto(year, make, mileage, gallons);
return auto;
}
}
output
------
Enter auto model year:
2010
Enter auto make and model:
Honda 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
On an average, 2010 cars have approximately 96000.0 miles on the odometer
2010 Honda Accord has a tank of 15.5 gallons and gets 27.5 miles per gallon.
Range is 426 miles
On an average, 2014 cars have approximately 48000.0 miles on the odometer
2014 Chevrolet Tahoe has a tank of 26.1 gallons and gets 19.2 miles per gallon.
Range is 501 miles
2014 Chevrolet Tahoe has a longer range
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.