Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 cars=new ArrayList(); //no argument constructor public CarLot() { } //Method that finds the car based on id public Car find(String carId) { //Iterating over arraylist for(int i=0; i (cars.get(highMPG)).getMPG()) { //Update highMPG highMPG = i; } } //returns car with highest MPG return cars.get(highMPG); } } //returns the number of Cars on the CarLot public int size() { return cars.size(); } //returns a String representation of the CarLot public String toString() { String carsList = ""; //Iterating over arraylist for(int i=0; i

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

9th Edition

B01JXPZ7AK, 9780805360479

More Books

Students also viewed these Databases questions