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

1. Create a class named Vehicle that contains private variables for the followin

ID: 3788670 • Letter: 1

Question

1. Create a class named Vehicle that contains private variables for the following: number of wheels and average number of miles per gallon (mpg). The class should also contain a parameterized constructor that takes int noWheels and int mpg. Create a toString() method that returns the string representation of the Vehicle object. Create an equals(Vehicle v) method that tests the equality of two vehicles based on no of wheels and mpg.

2. Create two subclasses, Car and Motorcycle that extend your Vehicle class. These subclasses should contain a constructor that accepts only miles per gallon (mpg) and an OVERRIDDEN toString() method that returns a string specific for the subclass type. The toString() methods should return the following formatted string based on Car and Motorcycle class:

For Car: This car has 4 wheels and gets 45 mpg.

For Motorcycle: This motorcycle has 2 wheels and gets 45 mpg.

3. Create a driver program called Driver that will instantiate 2 Car objects and 2 motorcycle objects so that the car objects are the same and the motorcycle objects are not. Print the details of each object and the result of testing their equality as shown below. Also instantiate 1 Vehicle object and call it’s toString() method. Sample values for creating Car and Motorcycle objects:

Car with mpg = 45 Car with mpg = 45

Motorcycle with mpg = 15 Motorcycle with mpg = 45

Vehicle with 3 wheels and 70 mpg.

NOTE: Remember to include appropriate accessor and mutator methods.

Sample Run:

Car One Info: This car has 4 wheels and gets 45 mpg.

Car Two Info: This car has 4 wheels and gets 45 mpg.

Motorcycle One Info: This motorcycle has 2 wheels and gets 15 mpg.

Motorcycle Two Info: This motorcycle has 2 wheels and gets 45 mpg.

Car One IS THE SAME AS Car Two

Motorcycle One IS NOT THE SAME AS Motorcycle Two

Vehicle Info: This vehicle has 3 wheels and gets 70 mpg.

Explanation / Answer

Vehicle.java

public class Vehicle {
   //Declaring variables
   private int noWheels;
   private int mpg;
  
   //Parameterized constructor
   public Vehicle(int noWheels, int mpg) {
       super();
       this.noWheels = noWheels;
       this.mpg = mpg;
   }
  
   //Setters and getters
   public int getNoWheels() {
       return noWheels;
   }
   public void setNoWheels(int noWheels) {
       this.noWheels = noWheels;
   }
   public int getMpg() {
       return mpg;
   }
   public void setMpg(int mpg) {
       this.mpg = mpg;
   }
  
   //toString() method is used to display the contents of an Object inside it
   @Override
   public String toString() {
       return "This car has "+noWheels+" wheels and gets "+mpg+" mpg.";
   }
   @Override
   public int hashCode() {
       final int prime = 31;
       int result = 1;
       result = prime * result + mpg;
       result = prime * result + noWheels;
       return result;
   }
   @Override
   public boolean equals(Object obj) {
       if (this == obj)
           return true;
       if (obj == null)
           return false;
       if (getClass() != obj.getClass())
           return false;
       Vehicle other = (Vehicle) obj;
       if (mpg != other.mpg)
           return false;
       if (noWheels != other.noWheels)
           return false;
       return true;
   }
  

}

___________________

Car.java

public class Car extends Vehicle {
   //Parameterized constructor
   public Car(int mpg) {
       super(4, mpg);
   }
  
   //toString() method is used to display the contents of an Object inside it
   @Override
   public String toString() {
       System.out.println(super.toString());
       return "";
   }

}

____________________

Motorcycle.java

public class Motorcycle extends Vehicle {
   //Parameterized constructor
   public Motorcycle(int mpg) {
       super(2,mpg);

   }

   //toString() method is used to display the contents of an Object inside it
   @Override
   public String toString() {
       System.out.println(super.toString());
       return "";
   }
}

____________________

Driver.java

public class Driver {

   public static void main(String[] args) {
       //Creating the Car One class Object
       Car car1=new Car(45);
      
       //Displaying the Car one information
       System.out.print("Car One Info: ");
       System.out.println(car1.toString());
      
       //Creating the Car Two class Object
       Car car2=new Car(45);
      
       //Displaying the Car two information
       System.out.print("Car Two Info: ");
       System.out.println(car2.toString());
      
       //Creating the Motorcycle One class Object
       Motorcycle motor1=new Motorcycle(15);
      
       //Displaying the Motorcycle one information
       System.out.print("Motorcycle One Info: ");
       System.out.println(motor1.toString());

       //Creating the Motorcycle Two class Object
       Motorcycle motor2=new Motorcycle(45);
      
       //Displaying the Motorcycle two information
       System.out.print("Motorcycle Two Info: ");
       System.out.println(motor2.toString());
      
       //Checking the whether Car one class object is equal to Car Two class object or not
       if(car1.equals(car2))
           System.out.println("Car One IS THE SAME AS Car Two");
       else
           System.out.println("Car One IS NOT THE SAME AS Car Two");

       //Checking the whether Motorcycle one class object is equal to Motorcycle Two class object or not
       if(motor1.equals(motor2))
           System.out.println("Motorcycle One IS THE SAME AS Motorcycle Two");
       else
           System.out.println("Motorcycle One IS NOT THE SAME AS Motorcycle Two");
      
       //Creating the Vehicle One class Object
       Vehicle vehicle1=new Vehicle(3, 70);
      
       //Displaying the vehicle object class info
       System.out.print("Vehicle Info: ");
       System.out.println(vehicle1.toString());

   }

}

_____________________

output:

Car One Info: This car has 4 wheels and gets 45 mpg.

Car Two Info: This car has 4 wheels and gets 45 mpg.

Motorcycle One Info: This car has 2 wheels and gets 15 mpg.

Motorcycle Two Info: This car has 2 wheels and gets 45 mpg.

Car One IS THE SAME AS Car Two
Motorcycle One IS NOT THE SAME AS Motorcycle Two
Vehicle Info: This car has 3 wheels and gets 70 mpg.

______________Thank You