Description Modify your program from Assignment 3 that defined and tested a vehi
ID: 3891025 • Letter: D
Question
Description Modify your program from Assignment 3 that defined and tested a vehicle class with a single engine to represent either a electric engine vehicle or an internal combustion vehicle. Pay careful attention to changes within each individual class. The Vehicle class represents a single vehicle. For the purpose of this assignment, assume that each of these vehicles contains one of two engines: either an internal combustion or electric engine. The InternalCombustionEngine class and ElectricEngine class are both subclasses of the Engine class. The Vehicle class should contain: . An Engine data member named engine that contains an Engine instance (which can either be electric or internal combustion). . A String make that sets the make for the vehicle. This should default to Toyabu". A String model that sets the model for the vehicle. This should default to "Priata". .A default no-arg constructor that creates a default Vehicle. Set your default values here by using the appropriate set methods. A constructor that allows the client to set the engine, vehicle make, and vehicle model. Accessor and mutator methods for all data members. A method named toString() that returns a String indicating the make, model, internal combustion engine's type and number of cylinders and/or electric engine's type and output rating. See sample output for the needed format. The Engine class should contain: · A String data member named type that indicates the type of engine. This should default to internal combustion. A default no-arg constructor that creates a default Engine. Set your default values here by using calling the other constructor with the this keyword and the appropriate default values (71 kW for electric, 4 cylinders for internal combustion). A constructor that allows the client to set the type of engine with a parameter. Accessor and mutator methods for all data members. Mutators should check values as described above. A method named toString() that returns a String indicating the type of engine as a String. . . .Explanation / Answer
Engine.java
public class Engine {
//Declaring instance variables
private String type;
//Zero argumented constructor
public Engine() {
this.type = "Internal Combustion";
}
//Parameterized constructor
public Engine(String type) {
super();
this.type = type;
}
//getters and setters
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return type;
}
}
____________________
Vehicle.java
public class Vehicle {
//Declaring instance variables
private String make;
private String model;
private Engine engine;
//Zero argumented constructor
public Vehicle() {
setMake("Toyabu");
setModel("Priata");
}
//Parameterized constructor
public Vehicle(Engine engine, String make, String model) {
super();
this.engine = engine;
this.make = make;
this.model = model;
}
//getters and setters
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public Engine getEngine() {
return engine;
}
public void setEngine(Engine engine) {
this.engine = engine;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return make + " " + model + " " + engine;
}
}
______________________
ElectricEngine.java
public class ElectricEngine extends Engine {
//Declaring instance variables
private int outputRating;
//Zero argumented constructor
public ElectricEngine() {
this.outputRating = 71;
}
//Parameterized constructor
public ElectricEngine(int outputRating) {
super();
this.outputRating = outputRating;
}
//getters and setters
public int getOutputRating() {
return outputRating;
}
public void setOutputRating(int outputRating) {
if (outputRating < 0)
this.outputRating = 71;
else
this.outputRating = outputRating;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return super.toString() + " Electric :" + outputRating + " kw";
}
}
______________________
InternalCombustionEngine.java
public class InternalCombustionEngine extends Engine {
//Declaring instance variables
private int cylinders;
//Zero argumented constructor
public InternalCombustionEngine() {
setCylinders(4);
}
//Parameterized constructor
public InternalCombustionEngine(int cylinders) {
super();
this.cylinders = cylinders;
}
//getters and setters
public int getCylinders() {
return cylinders;
}
public void setCylinders(int cylinders) {
if (cylinders < 0)
this.cylinders = 4;
else
this.cylinders = cylinders;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return super.toString() + " Internal Combustion :" + cylinders;
}
}
_________________________
Test.java
public class Test {
public static void main(String[] args) {
//Creating Two Engine class objects
Engine e1, e2;
e1 = new InternalCombustionEngine();
e2 = new ElectricEngine();
//Creating Two Vehicle class objects
Vehicle v1, v2;
v1 = new Vehicle(e2, "Toyabu", "Priata");
v2 = new Vehicle(e1, "Hyunzda", "Sonius");
//Displaying the Vehicle class info
System.out.println(v1.toString());
System.out.println(v2.toString());
}
}
_________________________
Output:
Toyabu Priata Internal Combustion
Electric :71 kw
Hyunzda Sonius Internal Combustion
Internal Combustion :4
_____________Could you rate me well.Plz .Thank You