Question: just need some help - in java on intellij, thanks - been stuck for a while just cant get it usually i would submit this
just need some help - in java on intellij, thanks - been stuck for a while just cant get it
usually i would submit this as 3 seperate questions but checkpoint 43 and 44 need the context of the previous, thank you
Task 1/4: Checkpoint 42
Copy the following code into the skeleton classes VehicleTester and Vehicle
public class VehicleTester { public static void main(String[] args) { Car car1 = new Car(1200, "Holden", "sedan", "Barina"); Car car2 = new Car(1500, "Mazda", "sedan", "323"); car1.print(); car2.print(); } } public class Vehicle { // parent class private int capacity; private String make; public Vehicle(int theCapacity, String theMake) { capacity = theCapacity; make = theMake; } public int getCapacity() { return capacity; } public String getMake() { return make; } public void print() { System.out.println("Vehicle Info:"); System.out.println(" capacity = " + capacity + "cc"); System.out.println(" make = " + make); } } Complete the definition of the class Car by adding
instance variables to store the type and model (both Strings) of the car
a constructor with the following header:
Car(int capacity, String make, String type, String model)
and which assigns the respective instance variables. Note, the first statement in the constructor should be a call to the constructor of the super class (Vehicle):
super( ...
Getters and setters for the instance variables.
The following output should be produced when the program is run (the indents are 2 spaces): Vehicle Info: capacity = 1200cc make = Holden Vehicle Info: capacity = 1500cc make = Mazda
Note that the class Car does not have a print method and thus the type and model are not printed. This will be added as part of the next task.
Task 2/4: Checkpoint 43
Modify the class Car to include a print method which invokes its parents print method (by making use of super) and prints out the type and model. The following output should be produced when the program is run: Vehicle Info: capacity = 1200cc make = Holden type = sedan model = Barina Vehicle Info: capacity = 1500cc make = Mazda type = sedan model = 323
Task 3/4: Checkpoint 44
Modify the class Vehicle, the parent class, to include a method called setCapacity which allows the engine capacity to be changed (see example below).
Modify the class Car so that it overrides the method setCapacity with its own version which output the message "Cannot change the engine capacity of a car" and does not change the engine capacity.
Modify the class VehicleTester to be:
public class VehicleTester { public static void main(String[] args) { Car car1 = new Car(1200, "Holden", "sedan", "Barina"); Vehicle v1 = new Vehicle(1500, "Mazda"); v1.setCapacity(1600); v1.print(); car1.setCapacity(1600); car1.print(); } } The following output should be produced when the program is run: Changing vehicle engine capacity: new capacity = 1600 Vehicle Info: capacity = 1600cc make = Mazda Cannot change the engine capacity of a car Vehicle Info: capacity = 1200cc make = Holden
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
