Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Database Systems For Advanced Applications 18th International Conference Dasfaa 2013 Wuhan China April 22 25 2013 Proceedings Part 2 Lncs 7826

Authors: Weiyi Meng ,Ling Feng ,Stephane Bressan ,Werner Winiwarter ,Wei Song

2013th Edition

3642374492, 978-3642374494

More Books

Students also viewed these Databases questions

Question

5. How would you describe your typical day at work?

Answered: 1 week ago

Question

7. What qualities do you see as necessary for your line of work?

Answered: 1 week ago