Question
Java Program: Complete the definition of theVehicleclass by: adding an instance variable of typeintnamedengineCapacity adding an instance variable of typeStringnamedmake creating a constructor that takes
Java Program:
- Complete the definition of theVehicleclass by:
- adding an instance variable of typeintnamedengineCapacity
- adding an instance variable of typeStringnamedmake
- creating a constructor that takes two formal parameters and assigns the values to the instance variables
- create getters and setters for all instance variables
- adding atoStringmethod that returns information about the vehicle (see below)
- Complete the definition of the classCar
by adding instance variables to store thetypeandmodel(bothStrings) of the car
by adding a constructor with the following header:
and which assigns the respective instance variables. Note,the first statement in the constructor should be a call to the constructor of the superclass(Vehicle):
by adding getters and setters for all instance variables
by adding atoStringmethod which invokes its parent'stoStringmethod (by making use ofsuper) and prints out thetypeandmodel.
- Add the following to themainmethod:
The following output should be produced when the program is run(the indents are 2 spaces): Vehicle Info: engine capacity = 1200cc make = Holden type = sedan model = Barina Vehicle Info: engine capacity = 1500cc make = Mazda type = sports model = 323
Task 2 - Checkpoint 43
Modify the classVehicle, the parent class, to include a method calledsetEngineCapacitywhich allows the engine capacity to be changed. Assuming aVehiclenamedv1has been created, the call
should produce the following output: Changing vehicle engine capacity: new capacity = 1600
Modify the classCarso that itoverridesthe methodsetEngineCapacitywith its own version which output the message "Cannot change the engine capacity of a car" and does not change the engine capacity.
Add the following to bottom of themainmethod inVehicleTester:
The following output should be produced when the program is run: Vehicle Info: engine capacity = 1200cc make = Holden type = sedan model = Barina Vehicle Info: engine capacity = 1500cc make = Mazda type = sports model = 323 Vehicle Info: engine capacity = 1500cc make = Mazda Changing vehicle engine capacity: new capacity = 1600 Vehicle Info: engine capacity = 1600cc make = Mazda Cannot change the engine capacity of a car Vehicle Info: engine capacity = 1200cc make = Holden type = sedan model = Barina
Task 3 - Checkpoint 44 & 45
Add a class calledVehicleDBwhich can store 5Vehicleobjects (Youmustuse andarray, and you will need to keep track of how many vehicles have been added (private int numV). It should include the following methods:
- a method calledaddVehiclewhich adds aVehicle, or any of its descendants, to the database (see example below).
- a method that increases the size of the database by 10 if it becomes full.
- a method calledprintwhich prints all theVehiclesin the database (see example below). Hint:make use of eachVehicle'stoStringmethod.
- Add getters for the size of the database and the number of vehicles in it.
Modify the classVehicleTesterto be:
The following output should be produced when the program is run: === Vehicle Data Base === Vehicle Info: capacity = 1200cc make = Holden type = sedan model = Barina Vehicle Info: capacity = 1500cc make = Mazda Vehicle Info: capacity = 1600cc make = Nissan type = suv model = X-Trail Vehicle Info: capacity = 1800cc make = Jeep Vehicle Info: capacity = 1000cc make = Suzuki Vehicle Info: capacity = 2000cc make = Kia type = sedan model = Cerato
Add a class calledHoldenDBwhich inherits from (extends)VehicleDB. It should include:
- a constructor which has no formal parameters and which calls its parent's constructor with an actual parameter of 20 (this may cause you to add code toVehicleDb)
- a method calledaddCarwhich takes three parameters (see example below), creates aCarobject and adds it to the database by making use one of its parent's methods.
- anoverridenversion of theprintmethod that prints=== Holdens Only ===and then calls its parentsprintmethod
Add the following code at the bottom of themainmethod in the classVehicleTester:
The following output should be appended to the output above when the program is run: === Holdens Only === === Vehicle Data Base === Vehicle Info: capacity = 1200cc make = Holden type = sedan model = Barina Vehicle Info: capacity = 3800cc make = Holden type = wagon model = Commodore
HoldenDB hdb = new HoldenDB(); hdb.addCar(1200, "sedan", "Barina"); hdb.addCar(3800, "wagon", "Commodore"); hdb.print();
public class VehicleTester { public static void main(String[] args) { VehicleDB vdb = new VehicleDB(); vdb.addVehicle(new Car(1200, "Holden", "sedan", "Barina")); vdb.addVehicle(new Vehicle(1500, "Mazda")); vdb.addVehicle(new Car(1600, "Nissan", "suv", "X-Trail")); vdb.addVehicle(new Vehicle(1800, "Jeep")); vdb.addVehicle(new Vehicle(1000, "Suzuki")); vdb.addVehicle(new Car(2000, "Kia", "sedan", "Cerato")); vdb.print(); } }
Vehicle v1 = new Vehicle(1500, "Mazda"); System.out.println(v1); v1.setEngineCapacity(1600); System.out.println(v1); car1.setEngineCapacity(1600); System.out.println(car1);
v1.setEngineCapacity(1600);
Car car1 = new Car(1200, "Holden", "sedan", "Barina"); Car car2 = new Car(1500, "Mazda", "sports", "323"); System.out.println(car1); System.out.println(car2);
super( ...
public Car(int engineCapacity, String make, String type, String model)
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