Java question An enum Vehicle is defined as shown in the class diagram. It conta
ID: 3825197 • Letter: J
Question
Java question
An enum Vehicle is defined as shown in the class diagram. It contains A private String variable engineSize A private String variable capacity 3 enumerated values CAR, VAN and BUS. For each value an associated String engineSize - CAR: "2 litres" - VAN: "4 litres" - BUS:"12.5 litres" For each value an associated String capacity - CAR: "4 passengers" - VAN:"7passengers" - BUS: "29 passengers" A public method getEngineSize() that returns the engineSize of the enum A public method getCapacity() that returns the capacity of the enum Write the enum Vehicle in the answer box below.Explanation / Answer
Code to declare and define enum as was asked in the question with main method to test the program
public class EnumTesting {
enum Vehicle {
CAR("2 litres","4 passengers"), //Initializing vehicle of type Car
VAN("4 litres,","7 passengers"), //Initializing vehicle of type Van
BUS("12.5 litres","29 passengers"); //Initializing vehicle of type Bus
private Vehicle(String engineSize,String capacity) //Constructor initializing values of enum
{
this.engineSize = engineSize;
this.capacity = capacity;
}
private String engineSize;
private String capacity;
public String getEngineSize() //Method to get engine size
{
return engineSize;
}
public String getCapacity() //Method to get capacity
{
return capacity;
}
}
public static void main(String[] args)
{
Vehicle vehicle = Vehicle.valueOf("BUS");
System.out.println("A " + vehicle.name().toLowerCase() + " with the engine size of " +
vehicle.getEngineSize() + " can carry " + vehicle.getCapacity());
}
}
Output
A bus with the engine size of 12.5 litres can carry 29 passengers
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.