Description
Assignment: Car Class Hierarchy
Implement a superclass and two subclasses to model cars, with specific requirements for constructors, methods, and testing.
1. Superclass Car
-
Instance Variable:
-
price(derived from input cost).
-
-
Constructor:
-
1-parameter
Car(double cost): Setsprice = cost * 2.
-
-
Method:
-
getPrice(): Accessor method returningprice.
-
2. Subclass NewCar (extends Car)
-
Additional Instance Variable:
-
color(e.g., “silver”).
-
-
Constructor:
-
2-parameter
NewCar(double cost, String color): Calls superclass constructor and initializescolor.
-
-
Methods:
-
equals(NewCar other): Comparespriceandcolorfor equality. -
toString(): Returns formatted string (e.g.,"price = $16,000.66, color = silver").
-
3. Subclass UsedCar (extends Car)
-
Additional Instance Variable:
-
mileage(e.g., 100000).
-
-
Constructor:
-
2-parameter
UsedCar(double cost, int mileage): Calls superclass constructor and initializesmileage.
-
-
Methods:
-
equals(UsedCar other): Comparespriceandmileagefor equality. -
toString(): Returns formatted string (e.g.,"price = $5,000.00, mileage = 100,000").
-
4. Driver Class (CarDemo)
-
Main Method:
public static void main(String[] args) { NewCar new1 = new NewCar(8000.33, "silver"); NewCar new2 = new NewCar(8000.33, "silver"); if (new1.equals(new2)) { System.out.println(new1); } UsedCar used1 = new UsedCar(2500, 100000); UsedCar used2 = new UsedCar(2500, 100000); if (used1.equals(used2)) { System.out.println(used1); } } // end main
-
Expected Output:
price = $16,000.66, color = silver price = $5,000.00, mileage = 100,000


