Question

Java Mr. Dealer is trying to keep track of his new cars and used cars by...

Java

Mr. Dealer is trying to keep track of his new cars and used cars by writing a Java program. He needs your help in setting up his classes.

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’s 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’s color). UsedCar should contain a mileage instance variable (the car’s odometer reading). The NewCar and UsedCar classes should each contain a 2-parameter constructor, an equals method, and a display method:

1. The equals method compares the values of instance variables (i.e., price and color for NewCar, and price and mileage for UsedCar).

2. The display method should print the values of all the instance variables within its class variables (i.e., price and color for NewCar, and price and mileage for UsedCar).

In the interest of elegance and maintainability, don’t forget to have your subclass constructors call your superclass constructors when appropriate.

Provide a driver class that tests your three car classes. Your driver class should contain this main method:

-------------------------------------

CODE PROVIDED BELOW:

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 // for new cars

price = $5,000.00, mileage = 100,000 // for used cars

0 0
Add a comment Improve this question Transcribed image text
Answer #1

MrDealer.java

class Car{
   protected 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 color) {
       super(cost);
       this.color = color;
   }
  
   @Override
   public boolean equals(Object obj) {
       if (this == obj)//checks if the reference are same
           return true;
       //checks if the objects are of same class type
       if(obj == null || obj.getClass()!=this.getClass())
           return false;
       NewCar other = (NewCar) obj;
       //comparing and returning the state of the object
       return (other.price == this.price && other.color == this.color);
   }
  
   public void display() {//displaying the result
       System.out.println("New Car price = $"+super.price+" color= "+color);
   }
}

class UsedCar extends Car{
   private int mileage;

   public UsedCar(double cost, int mileage) {
       super(cost);
       this.mileage = mileage;
   }
   @Override
   public boolean equals(Object obj) {
       if (this == obj)
           return true;
       if(obj == null || obj.getClass()!=this.getClass())
           return false;
       UsedCar other = (UsedCar) obj;
       return (other.price == this.price && other.mileage == this.mileage);
   }
  
   public void display() {
       System.out.println("Used Car price = $"+super.price+" mileage= "+mileage);
   }
  
}

public class MrDealer {
   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();
       }
   }
}

Program Screenshots

Output

Add a comment
Know the answer?
Add Answer to:
Java Mr. Dealer is trying to keep track of his new cars and used cars by...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please try to write the code with Project 1,2 and 3 in mind. And use java language, thank you very much. Create an Edit Menu in your GUI Add a second menu to the GUI called Edit which will have one me...

    Please try to write the code with Project 1,2 and 3 in mind. And use java language, thank you very much. Create an Edit Menu in your GUI Add a second menu to the GUI called Edit which will have one menu item called Search. Clicking on search should prompt the user using a JOptionPane input dialog to enter a car make. The GUI should then display only cars of that make. You will need to write a second menu...

  • Java Programming --- complete the given classes DeLand Space Car Dealer needs a new program for...

    Java Programming --- complete the given classes DeLand Space Car Dealer needs a new program for their inventory management system. The program needs the following classes: A main class (SpaceCarDealer.java) that includes the main method and GUI. Two instantiable classes: CarDealer.java o Variables to store the dealer name, and the carsOnLot array in Car type. o A constructor to initialize the name variable with the given dealer name. o An accessor method to return the name of the dealer. o...

  • Create an abstract class “Appointment” that represents an event on a calendar. The “Appointment” class will...

    Create an abstract class “Appointment” that represents an event on a calendar. The “Appointment” class will have four instance variables: • An instance variable called “year” which will be of type int • An instance variable called “month” which will be of type int • An instance variable called “day” which will be of type int • An instance variable called “description” which will be of type String The “Appointment” class must also implement the following methods: • A getter...

  • *** FOR A BEGINNER LEVEL JAVA CLASS, PLEASE KEEP CODE SIMPLE, AND WE USE BLUE J...

    *** FOR A BEGINNER LEVEL JAVA CLASS, PLEASE KEEP CODE SIMPLE, AND WE USE BLUE J IN CLASS (IF IT DOESNT MATTER PLEASE COMMENT TELLING WHICH PROGRAM YOU USED TO WRITE THE CODE, PREFERRED IS BLUE J!!)*** ArrayList of Objects and Input File Use BlueJ to write a program that reads a sequence of data for several car objects from an input file. It stores the data in an ArrayList<Car> list . Program should work for input file containing info...

  • Java Program Please help me with this. It should be pretty basic and easy but I...

    Java Program Please help me with this. It should be pretty basic and easy but I am struggling with it. Thank you Create a superclass called VacationInstance Variables destination - String budget - double Constructors - default and parameterized to set all instance variables Access and mutator methods budgetBalance method - returns the amount the vacation is under or over budget. Under budget is a positive number and over budget is a negative number. This method will be overwritten in...

  • Question #1 (CLO: 1,2.3) Make a class called Car. Declate its data, model, colour, price Also...

    Question #1 (CLO: 1,2.3) Make a class called Car. Declate its data, model, colour, price Also make a constructor to initialize these data variables. Make a getPrice() method to return price. Make a Child class of Car, called Truck. It has a data variable, weight. Make a constructor that takes all the data variables including weight and those of Car class. Also override getPrice() method, which return 20% discount price if the weight is more than 20,000 else it returns...

  • Java Eclipse Coding question: Use data abstraction (real-world modeling) to come up with 3 instance variables...

    Java Eclipse Coding question: Use data abstraction (real-world modeling) to come up with 3 instance variables and 2 effectors for a new data type – the class HospitalPatient. You have the freedom to design the instance variables and effectors. instance variables effectors 1. 1. 2. 2. 3. Write the code for class HospitalPatient, according to the requirement above. Include the default constructors with no argument, and the constructor with all arguments. Provide a getter and setter for each individual instance...

  • Update your Assignment 4 as follows (Assignment 4 codes will be on the bottom) Create a...

    Update your Assignment 4 as follows (Assignment 4 codes will be on the bottom) Create a new method called displayAll, that takes an ArrayList (of your base class) as a parameter, and doesn't return anything [25 pts] The displayAll method will loop through the ArrayList, and call the display (or toString) method on each object   [25 pts] In the main method, create an ArrayList containing objects of both your base class and subclass. (You should have at least 4 objects)  [25...

  • Java Create four classes: 1.      An Animal class that acts as a superclass for Dog and...

    Java Create four classes: 1.      An Animal class that acts as a superclass for Dog and Bird 2.      A Bird class that is a descendant of Animal 3.      A Dog class that is a descendant of Animal 4.      A “driver” class named driver that instantiates an Animal, a Dog, and a Bird object. The Animal class has: ·        one instance variable, a private String variable named   name ·        a single constructor that takes one argument, a String, used to set...

  • Java Time Class Class declarations are one of the ways of defining new types in Java....

    Java Time Class Class declarations are one of the ways of defining new types in Java. we will create two classes that both represent time values for a 24 hours period. The valid operations are as follows. int getHours(): returns the number of hours int getMinutes(): returns the number of minutes int getSeconds(): returns the number of seconds String toString(): returns a String representation boolean equals(Time other): returns true if and only if other designates an object that has the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT