Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I can\'t get this program to run properly. Implement a superclass named Car that

ID: 3538448 • Letter: I

Question

I can't get this program to run properly.


Implement a superclass named Car that contains a price instance variable, a getPrice method, and a 1-parameter constructor. The getPrice method is a simple accessor method that returns the price instance variable%u2019s value. The 1-parameter constructor receives a cost parameter and assigns a value to the price instance variable based on this formula:



price = cost * 2;



Implement two classes named NewCar and UsedCar; they are both derived from the Car superclass. NewCar should contain a color instance variable (the car%u2019s color). UsedCar should contain a mileage instance variable (the car%u2019s odometer reading). The NewCar and UsedCar classes should each contain a 2-parameter constructor, an equals method, and a display method. In the interest of elegance and maintainability, don%u2019t forget to have your subclass constructors call your superclass constructors when appropriate. The display method should print the values of all the instance variables within its class.



Provide a driver class that tests your three car classes. Your driver class should contain this 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))

{

new1.display();

}



UsedCar used1 = new UsedCar(2500, 100000);

UsedCar used2 = new UsedCar(2500, 100000);

if (used1.equals(used2))

{

used1.display();

}

} // end main



Output:



price = $16,000.66, color = silver

price = $5,000.00, mileage = 100,000

Explanation / Answer

class Car

{

private double price;

public Car(double cost)

{ price=cost*2; }

public double getPrice()

{ return price; }

}


class NewCar extends Car

{

private String color;

public NewCar(double cost,String clr)

{

super(cost);

color=clr;

}

public String getColor()

{ return color; }


public boolean equals(NewCar nc)

{

if(this.getPrice()==nc.getPrice() && color.equals(nc.getColor()))

return true;


return false;

}

public void display()

{

System.out.println("Price: $"+this.getPrice()+" Color: "+color);

}

}


class UsedCar extends Car

{

private int mileage;

public UsedCar(double cost,int m)

{

super(cost);

mileage=m;

}

public int getMileage()

{ return mileage; }


public boolean equals(UsedCar nc)

{

if(this.getPrice()==nc.getPrice() && mileage==nc.getMileage())

return true;


return false;

}

public void display()

{

System.out.println("Price = $"+this.getPrice()+", Mileage = "+mileage);

}

}


class Driver{

public static void main(String[] args)

{


NewCar new1 = new NewCar(8000.33, "silver");


NewCar new2 = new NewCar(8000.33, "silver");


if (new1.equals(new2))


{


new1.display();


}




UsedCar used1 = new UsedCar(2500, 100000);


UsedCar used2 = new UsedCar(2500, 100000);


if (used1.equals(used2))


{


used1.display();


}


} // end main

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote