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
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 its 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.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started