CSC160 Assignment 8 solved

$30.00

Original Work ?

Download Details:

  • Name: AssignmentEight-dcoi1k.zip
  • Type: zip
  • Size: 123.34 KB

Category: Tag: You will Instantly receive a download link upon Payment||Click Original Work Button for Custom work

Description

5/5 - (1 vote)

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): Sets price = cost * 2.

  • Method:

    • getPrice(): Accessor method returning price.

2. Subclass NewCar (extends Car)

  • Additional Instance Variable:

    • color (e.g., “silver”).

  • Constructor:

    • 2-parameter NewCar(double cost, String color): Calls superclass constructor and initializes color.

  • Methods:

    • equals(NewCar other): Compares price and color for 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 initializes mileage.

  • Methods:

    • equals(UsedCar other): Compares price and mileage for 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