Answered step by step
Verified Expert Solution
Link Copied!

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 (e.g. 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 Vehicle(String 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 (e.g. 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 input/output:
Suppose you have the following vehicles:
Car myCar = new Car("Mustang",120.0,4,2);
Truck myTruck = new Truck("F-150",90.0,4,3.5);
Motorcycle myMotorcycle = new Motorcycle("Ninja",150.0,2, "Sport");
You add them to an ArrayList named "vehicles".
When you loop through the ArrayList, filter it to only include vehicles with more than 2 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: 150.0 mph, number of wheels: 2, type: Sport
Vehicle name: Mustang, top speed: 120.0 mph, number of wheels: 4, number of doors: 2

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

b. What groups were most represented? Why do you think this is so?

Answered: 1 week ago

Question

3. Describe phases of minority identity development.

Answered: 1 week ago

Question

5. Identify and describe nine social and cultural identities.

Answered: 1 week ago