Question
Sophie, Sally and Jack are about to open for business. Their CarLot class needs some enhancements, however. Add to our previous CarLot the following methods:
Sophie, Sally and Jack are about to open for business. Their CarLot class needs some enhancements, however. Add to our previous CarLot the following methods:
Accessors
Car getCarHighestMilage() returns the Car with the highest mileage in the CarLot. If the CarLot is empty, return null.
int getTotalMiles() returns the total mileage of all cars on the CarLot
modify toString to include the above information, but don't include the sorted-by-MPG list.
ArrayList getSortedByMPG() returns a new ArrayList that is ordered by MPG (highest MPG first). The returned ArrayList is empty if the CarLot is empty. Note that this is an accessor; the method must not modify the instance variable in any way. Modify the SelectionSort code from the book to perform the sort.
Test Code
Enhance CarLotMain to add the new information. For example, after all Cars have been entered, print the CarLot sorted by MPG .
Grading Elements
getCarHighestMilage, getTotalMiles are accessors, have correct return types and signatures.
getCarHighestMilage, getTotalMiles return the expected values
getSortedByMPG returns a copy of the CarLot inventory that is sorted appropriately; it does not change the CarLot instance variable.
toString enhanced to include new information
CarLotMain enhanced to report new information and print the sorted-by-MPG list after all cars have been entered.
Code To edit:
Car.java*
class Car { //Private instance variables private String id; private double MPG; //Arg Constructor public Car(String tid, double tMpg) { id = tid; MPG = tMpg; } //Getter method for ID public String getID() { return id; } //Getter method for MPG public double getMPG() { return MPG; } //Method for displaying car details public String toString() { //Returning string return "Car ID: " + id + " \t MPG: " + MPG; } }
CarLot.java*
import java.util.ArrayList; class CarLot { //instance variable, an ArrayList of Car elements private ArrayList CarLotMain.java* import java.util.Scanner; class CarLotMain { //Main method public static void main(String args[]) { int carsNum; //Scanner class object Scanner reader = new Scanner(System.in); //Prompting user System.out.print(" Enter number of cars to be added to a CarLot: "); carsNum = reader.nextInt(); String cId; double mpg; //Creating CarLot class object CarLot carsLot = new CarLot(); //Reading car info and adding to list for(int i=0; i
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started