Question
Using the solution created in question 2 you are going to create a nested static class that will hold the details of the engine used
Using the solution created in question 2 you are going to create a nested static class that will hold the details of the engine used within the vehicle.
a. Open the vehicle class.
b. Under the constructor method create a public static class named Engine.
c. Create two private static final variables named MAKE and CAPACITY. Make will hold text while capacity will store whole numbers.
d. The values that you should assign to the variables are Predicter and 1600
e. Do not create a constructor for this class.
f. Create two public static getters for both of the variables created.
g. Update the toString method in the Vehicle class to display the engine make and model. Remember the Engine is a static class.
The output of the program for each vehicle should now look like this:
The vehicle is manufactured by: Seer
The model type is Edict
The chassis number is ch2
The engine make is Predicter
The engine capacity is 1600cc
Solution for question 2:
class Vehicle{
public static String MAKE ="Augur";
public static int numVehicles = 0;
private String chassisNo, model;
public Vehicle(){};
public Vehicle(String model){
this.model = model;
numVehicles = numVehicles+1;
chassisNo = "Ch"+Integer.toString(numVehicles);
System.out.println("Vehicle manufactured");
}
// added a method setMake() and set the static variable name to the name passed in this method
public void setMake(String make){
MAKE=make;
}
public void setChassisNo(String cNo){
this.chassisNo = cNo;
}
public void setModel(String model){
this.model = model;
}
public String getChassisNo(){
return chassisNo;
}
public String getModel(){
return model;
}
public void ToString(){
System.out.println("The vehicle is Manufactured by : "+ MAKE);
System.out.println("The model type is : "+this.getModel());
System.out.println("Number of vehicles manufactured: "+this.getChassisNo());
System.out.println("Number of vehicles manufactured: "+numVehicles);
}
}
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