Question
I found myself a bit stuck on this little project, I could use some help. - Modify Car.java to implement Comparable, then sort the cars
I found myself a bit stuck on this little project, I could use some help.
- Modify Car.java to implement Comparable, then sort the cars in the lot by make, then model, then year.
- Write a class that implements Comparator such that it sorts the cars in the lot by price.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class Car {
private String make; private String model; private int year; private double horsepower; private double msrp; public Car(String make, String model, int year, double horsepower, double msrp) { super(); this.make = make; this.model = model; this.year = year; this.horsepower = horsepower; this.msrp = msrp; } public String getMake() { return make; } public String getModel() { return model; } public int getYear() { return year; } public double getHorsepower() { return horsepower; } public double getMsrp() { return msrp; } public String toString() { return String.format("%4d %-22s%4.0f hp $ %9.2f", year, (make + " " + model) , horsepower , msrp); } }
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.util.ArrayList; import java.util.List;
public class ParkingLot {
public static void main(String[] args) { List lot = new ArrayList(); lot.add(new Car("Jeep", "Wrangler", 2019, 285., 31696.)); lot.add(new Car("Mazda", "Miata", 2019, 181., 26625.)); lot.add(new Car("Ford", "Mustang", 2018, 310., 26813.)); lot.add(new Car("Chevy", "Camaro", 2017, 275., 26690.)); lot.add(new Car("Chevy", "Corvette", 1999, 345., 38320.)); lot.add(new Car("Ferrari", "488", 2016, 660., 245400.)); lot.add(new Car("Porsche", "911", 2015, 350., 58807.)); lot.add(new Car("Mercedes-Benz", "E-Class", 2016, 302., 53100.)); lot.add(new Car("Rolls-Royce", "Wraith", 2018, 624., 349123.)); lot.add(new Car("Fiat", "500", 2017, 101., 15700.)); // print out all the cars in the lot for (Car car : lot) { System.out.println(car); } // print out all the cars with an MSRP of $20,000 for (Car car : lot) { if (car.getMsrp() == 20000) { System.out.println(car.getMake() + " " + car.getModel() + " " + car.getMsrp()); } } // print out all the cars with horsepower greater than 400 for (Car car : lot) { if (car.getHorsepower() >= 400) { System.out.println(car.getMake() + " " + car.getModel() + " " + car.getHorsepower()+ " Horsepower"); } }
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