Answered step by step
Verified Expert Solution
Question
1 Approved Answer
You are designing a game with different types of vehicles, such as cars, trucks, and motorcycles. All vehicles have a name, a top speed, and
You are designing a game with different types of vehicles, such as cars, trucks, and motorcycles. All vehicles have a name, a top speed, and a number of wheels. However, each type of vehicle has unique characteristics. Cars have a number of doors, trucks have a cargo capacity in tons, and motorcycles have a type eg sport, cruiser, etc.
To represent this in code, you have created an abstract class named Vehicle with the following fields and methods:
public abstract class Vehicle
protected String name;
protected double topSpeed;
protected int numWheels;
public VehicleString name, double topSpeed, int numWheels
this.name name;
this.topSpeed topSpeed;
this.numWheels numWheels;
add the setters and getters for name, topSpeed, numWheels
public abstract void displayInfo;
You have also created three classes that extend the Vehicle class: Car, Truck, and Motorcycle. Each of these classes should have a constructor that takes in the appropriate additional parameters number of doors, cargo capacity, or type and they should implement the displayInfo method to print out all the information about the vehicle.
Complete the Vehicle class to include the missing setters and getters.
Create the Car class that extends Vehicle:
The Car class should have an additional field for the number of doors.
The constructor should take in parameters for all the fields.
Implement the displayInfo method to print out all the information about the car.
Create the Truck class that extends Vehicle:
The Truck class should have an additional field for the cargo capacity in tons.
The constructor should take in parameters for all the fields.
Implement the displayInfo method to print out all the information about the truck.
Create the Motorcycle class that extends Vehicle:
The Motorcycle class should have an additional field for the type eg sport, cruiser, etc.
The constructor should take in parameters for all the fields.
Implement the displayInfo method to print out all the information about the motorcycle.
In main, create an ArrayList named vehicles that contains instances of each of the three classes you created:
For example, you might have a Car object named myCar, a Truck object named myTruck, and a Motorcycle object named myMotorcycle, and add them to the vehicles ArrayList.
Example of inputoutput:
Suppose you have the following vehicles:
Car myCar new CarMustang;
Truck myTruck new TruckF;
Motorcycle myMotorcycle new MotorcycleNinja "Sport";
You add them to an ArrayList named "vehicles".
When you loop through the ArrayList, filter it to only include vehicles with more than wheels, sort it based on the vehicle's top speed, and print out all the information using the displayInfo method, the output should be:
Vehicle name: Ninja, top speed: mph number of wheels: type: Sport
Vehicle name: Mustang, top speed: mph number of wheels: number of doors:
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