[85%) you will be writing six classes which implement a hierarchy of classes. Th
ID: 3875658 • Letter: #
Question
[85%) you will be writing six classes which implement a hierarchy of classes. The classes all have to do with motor vehicles I. A MotorVehicle has several attributes: make, model, year, maximum speed, weight, price A car has all the attributes ofa MotorVehicle, plus another attribute: four wheels. A MotorCycle has all the attributes of a MotorVehicle, plus three other attributes: two wheels, two passengers and one of three possible styles ( "Scooter", "Regular" and "Sport") A Compact has all the attributes of a Car, plus two other attributes: can have 4 passengers, 2 doors and a possible sunroof. A HatchBack has all the attributes of a Car, plus two other attributes: can have 5 passengers, 3 doors and a possible sunroof. AMiniVan has all the attributes of a Car, plus two other attributes: can have 7 passengers, 5 doors and a possible trailer-hitch. Here's a test program which uses the above hierarchy: // File Garage.java // Author: Daniel Ling / Purpose: A test harness program, to instantiate and use objects of related types (inheritance hierarchy) public class Garage public static void main (String[] args) f Motorvehicle [ ] vehicles new Motorvehicle [5] ; vehicles [0] = new Motorcycle ("YAMAHA", "YW50FB", vehicles [1] new Compact ("Chevrolet", "Sprite", 1998, 120, 1000, vehicles [2] =new Minivan ("Toyota", "Sienna", 2005, 180, 2200, vehicles [3] new Hatchback ( "Volkswagen", "Gorgon", 1999, 140, 1500, vehicles [4]new MotorCycle ("SUZUKI", "Boulevard C50", for (MotorVehicle motorVehicle : vehicles) 2012, 100, 170, 5395.00, "Scooter" 1800.00, true) 12500.00, true) 3900.00, false) 2005, 180, 210, 5200.00, "Sport"); System.out.println (motorVehicle); 1 // main / class GarageExplanation / Answer
MotorVehicle.java
public class MotorVehicle {
//Declaring instance variables
private String make ;
private String model;
private int year;
private int maxSpeed;
private double weight;
private double price;
//Parameterized constructor
public MotorVehicle(String make, String model, int year, int maxSpeed,
double weight, double price) {
this.make = make;
this.model = model;
this.year = year;
this.maxSpeed = maxSpeed;
this.weight = weight;
this.price = price;
}
//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 int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMaxSpeed() {
return maxSpeed;
}
public void setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "Make=" + make + " Model=" + model + " Year=" + year
+ " Maximum Speed=" + maxSpeed + " Weight=" + weight + " Price="
+ price;
}
}
________________
Car.java
public class Car extends MotorVehicle {
private int noOfWheels;
public Car(String make, String model, int year, int maxSpeed, double weight,
double price) {
super(make, model, year, maxSpeed, weight, price);
this.noOfWheels = 4;
}
public int getNoOfWheels() {
return noOfWheels;
}
public void setNoOfWheels(int noOfWheels) {
this.noOfWheels = noOfWheels;
}
@Override
public String toString() {
return super.toString()+" Number of Wheels = " + noOfWheels;
}
}
_____________________
MotorCycle.java
public class MotorCycle extends MotorVehicle {
//Declaring instance variables
private int noOfWheels=2;
private int noOfPassengers=2;
private String style;
//Parameterized constructor
public MotorCycle(String make, String model, int year, int maxSpeed,
double weight, double price, String style) {
super(make, model, year, maxSpeed, weight, price);
this.style = style;
}
//getters and setters
public String getStyle() {
return style;
}
public void setStyle(String style) {
this.style = style;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return super.toString()+" Number Of Wheels = " + noOfWheels + " Number Of Passengers = "
+ noOfPassengers + " ";
}
}
__________________
Compact .java
public class Compact extends Car {
//Declaring instance variables
private int noOfPassengers=4;
private int noOfDoors=2;
private boolean hasSunroof;
//Parameterized constructor
public Compact(String make, String model, int year, int maxSpeed,
double weight, double price, boolean hasSunroof) {
super(make, model, year, maxSpeed, weight,price);
this.hasSunroof = hasSunroof;
}
//getters and setters
public boolean isHasSunroof() {
return hasSunroof;
}
public void setHasSunroof(boolean hasSunroof) {
this.hasSunroof = hasSunroof;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return super.toString()+" Number Of Passengers = " + noOfPassengers + " No Of Doors = "
+ noOfDoors + ", Sunroof Option = " + hasSunroof+" ";
}
}
___________________
HatchBack,java
public class HatchBack extends Car {
//Declaring instance variables
private int noOfPassengers=5;
private int noOfDoors=3;
private boolean hasSunroof;
//Parameterized constructor
public HatchBack(String make, String model, int year, int maxSpeed,
double weight, double price,boolean hasSunroof) {
super(make, model, year, maxSpeed, weight, price);
this.hasSunroof = hasSunroof;
}
// getters and setters
public boolean isHasSunroof() {
return hasSunroof;
}
public void setHasSunroof(boolean hasSunroof) {
this.hasSunroof = hasSunroof;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return super.toString()+" Number Of Passengers = " + noOfPassengers + " No Of Doors = "
+ noOfDoors + ", Sunroof Option = " + hasSunroof+" ";
}
}
_________________
MiniVan.java
public class MiniVan extends Car {
//Declaring instance variables
private int noOfPassengers=7;
private int noOfDoors=5;
private boolean hasTrailerHitch;
//Parameterized constructor
public MiniVan(String make, String model, int year, int maxSpeed,
double weight, double price,boolean hasTrailerHitch) {
super(make, model, year, maxSpeed, weight, price);
this.hasTrailerHitch = hasTrailerHitch;
}
// getters and setters
public boolean isHasTrailerHitch() {
return hasTrailerHitch;
}
public void setHasTrailerHitch(boolean hasTrailerHitch) {
this.hasTrailerHitch = hasTrailerHitch;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return super.toString()+" Number Of Passengers = " + noOfPassengers + " No Of Doors = "
+ noOfDoors + ", Trailer Hitch Option = " + hasTrailerHitch+" ";
}
}
___________________
Garage.java
public class Garage {
public static void main(String[] args) {
MotorVehicle[] vehicles=new MotorVehicle[5];
vehicles[0]=new MotorCycle("YAMAHA","YW50FB",2012,100,170,5395.00,"Scooter");
vehicles[1]=new Compact("Chevrolet","Sprite",1998,120,1000,1800.00,true);
vehicles[2]=new MiniVan("Toyota","Sienna",2005,180,2200,12500.00,true);
vehicles[3]=new HatchBack("Volkswagen"," Gorgon",1999,140,1500,3900.00,false);
vehicles[4]=new MotorCycle("SUZUKI","Boulevard C50",2005,180,210,5200.00,"Sport");
for(MotorVehicle motorVehicle:vehicles)
{
System.out.println(motorVehicle);
}
}
}
________________
Output
Make=YAMAHA
Model=YW50FB
Year=2012
Maximum Speed=100
Weight=170.0
Price=5395.0
Number Of Wheels = 2
Number Of Passengers = 2
Style = Scooter
Make=Chevrolet
Model=Sprite
Year=1998
Maximum Speed=120
Weight=1000.0
Price=1800.0
Number of Wheels = 4
Number Of Passengers = 4
No Of Doors = 2,
Sunroof Option = true
Make=Toyota
Model=Sienna
Year=2005
Maximum Speed=180
Weight=2200.0
Price=12500.0
Number of Wheels = 4
Number Of Passengers = 7
No Of Doors = 5,
Trailer Hitch Option = true
Make=Volkswagen
Model= Gorgon
Year=1999
Maximum Speed=140
Weight=1500.0
Price=3900.0
Number of Wheels = 4
Number Of Passengers = 5
No Of Doors = 3,
Sunroof Option = false
Make=SUZUKI
Model=Boulevard C50
Year=2005
Maximum Speed=180
Weight=210.0
Price=5200.0
Number Of Wheels = 2
Number Of Passengers = 2
Style = Sport
__________________Thank YOu
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.