In: Computer Science
Create a super class named Vehicle, which has the following attributes: VIN, engine size, Year, Speed. Check
Add a subclass named Car, which has the following additional attributes: Wheel size, Model, Miles (OOD). Check
Add another subclass to the vehicle superclass named Boat which has the following additional attributes: Hours, Gas type, Type (Fishing Boat, Deck boat, bowrider boat), and Check
Write a method named DisplayInfo() in your main to display the information of the Vehicle. In this method print all the features of the vehicle.
Add a method in each class to estimate the price of the vehicle subclasses base on some factors of each type. And call this method from the method DisplayInfo().
In the main method create three objects one of type Vehicle, the second of type Cars, and the third of type Boat.
I have the classes and variables made. How would i create the object (i know how to create) and pass this information to the display info.
Java language
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
_________________
// Vehicle.java
public class Vehicle {
private String vin;
private double engineSize;
private int year;
private int speed;
public Vehicle(String vin, double engineSize, int
year, int speed) {
this.vin = vin;
this.engineSize = engineSize;
this.year = year;
this.speed = speed;
}
public String getVin() {
return vin;
}
public void setVin(String vin) {
this.vin = vin;
}
public double getEngineSize() {
return engineSize;
}
public void setEngineSize(double engineSize)
{
this.engineSize = engineSize;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
@Override
public String toString() {
return " VIN :" + vin + ", Engine
Size :" + engineSize + ", Year :"
+ year + ", Speed :" + speed;
}
}
_________________________
// Boat.java
public class Boat extends Vehicle {
private double hours;
private String gasType;
private String type;
public Boat(String vin, double engineSize, int
year, int speed,
double hours,
String gasType, String type) {
super(vin, engineSize, year,
speed);
this.hours = hours;
this.gasType = gasType;
this.type = type;
}
public double getHours() {
return hours;
}
public void setHours(double hours) {
this.hours = hours;
}
public String getGasType() {
return gasType;
}
public void setGasType(String gasType) {
this.gasType = gasType;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "Boat Hours :" + hours + ",
Gas Type :" + gasType + ", Type="
+ type + super.toString();
}
public void estimateCost() {
if(getEngineSize()<=100)
{
System.out.println("Boat Cost : $23000");
System.out.println(toString());
}
else
if(getEngineSize()>100)
{
System.out.println("Boat Cost : $43000");
System.out.println(toString());
}
}
}
_____________________________
// Car.java
public class Car extends Vehicle {
private double wheelSize;
private String model;
private double miles;
public Car(String vin, double engineSize, int year, int
speed,
double wheelSize, String model,
double miles) {
super(vin, engineSize, year, speed);
this.wheelSize = wheelSize;
this.model = model;
this.miles = miles;
}
public double getWheelSize() {
return wheelSize;
}
public void setWheelSize(double wheelSize) {
this.wheelSize = wheelSize;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public double getMiles() {
return miles;
}
public void setMiles(double miles) {
this.miles = miles;
}
@Override
public String toString() {
return "Car : WheelSize :" + wheelSize + ", Model=" +
model + ", Miles :"
+ miles +
super.toString();
}
public void estimateCost() {
if(model.equals("Mahindra XUV500"))
{
System.out.println("Car Price
:$20000");
System.out.println(toString());
}
else if(model.equals("Swift"))
{
System.out.println("Car Price
:$10000");
System.out.println(toString());
}
}
}
____________________________
// Test.java
public class Test {
public static void main(String[] args) {
Vehicle v=new
Vehicle("SERQW1234567",5883,2019,120);
Car c=new
Car("MKAWES234156",2384,2017,160,16,"Mahindra XUV500",23000);
Boat b=new
Boat("BHAERS5467783",1500,2018,100,12,"E10","Fishing Boat");
DisplayInfo(c);
DisplayInfo(b);
DisplayInfo(v);
}
private static void DisplayInfo(Object o) {
if(o instanceof Car)
{
((Car)o).estimateCost();
}
else if(o instanceof Boat)
{
((Boat)o).estimateCost();
}
}
}
____________________________
Output:
Car Price :$20000
Car : WheelSize :16.0, Model=Mahindra XUV500, Miles :23000.0 VIN
:MKAWES234156, Engine Size :2384.0, Year :2017, Speed :160
Boat Cost : $43000
Boat Hours :12.0, Gas Type :E10, Type=Fishing Boat VIN
:BHAERS5467783, Engine Size :1500.0, Year :2018, Speed
:100
_______________Thank You