8/1/2018 Setup Your Java Files Abstract a zip file a Key + public Additional Req
ID: 3920055 • Letter: 8
Question
8/1/2018 Setup Your Java Files Abstract a zip file a Key + public Additional Requirements . Vehicle class: - The model of vehicle (e g. Corolla, Dungfeng. Panther). Empty string by default. The model should be stored in -getCategory is an abstract method and represents the type of the vehicle. -Format returned by toString "Model: .Truck class: cargoCapacity is an integer representing cargo capacity of the truck. getCategory returns the category of the Truck based on its cargoCapacity. If capacity is less or equal to 3300, its LIGHT WEIGHT, between 3300 and 6000 (inclusive) its MEDIUM, and above 6000 is HEAVY DUTY Constructors and toString should invoke appropriate counterparts in parent class. Format returned by toString: Model :Explanation / Answer
abstract class Vehicle // base class
{
private String model;
//constructors
public Vehicle()
{
model = " ";
}
public Vehicle(String model)
{
this.model = model;
}
public Vehicle( Vehicle toCopy)
{
this.model = toCopy.model;
}
//get and set methods
public String getModel()
{
return model;
}
public void setModel(String model)
{
this.model = model;
}
public String toString()
{
return " Model : "+model +" Category : "+getCategory();
}
public boolean isMidRange()
{
if(getCategory() == "MEDIUM")
return true;
else
return false;
}
abstract String getCategory();
}
class Car extends Vehicle
{
private int noOfSeats;
public Car(String model,int noOfSeats)// argument constructor
{
super(model);
this.noOfSeats = noOfSeats;
}
public Car(Car toCopy)// copy constructor
{
this.noOfSeats = toCopy.noOfSeats;
}
//get and set methods
public int getNoOfSeats()
{
return noOfSeats;
}
public void setNoOfSeats(int noOfSeats)
{
this.noOfSeats = noOfSeats;
}
public String getCategory()// implement abstract method of base class
{
if(noOfSeats < 3)
return "SMALL";
else if(noOfSeats >=3 && noOfSeats <=5)
return "MEDIUM";
else
return "FAMILY";
}
public String toString()
{
return " "+super.toString() +" noOfSeats : "+noOfSeats;
}
}
class Truck extends Vehicle
{
private int cargoCapacity;
public Truck(String model,int cargoCapacity)
{
super(model);
this.cargoCapacity = cargoCapacity;
}
public Truck(Truck toCopy)
{
this.cargoCapacity = toCopy.cargoCapacity;
}
public int getCargoCapacity()
{
return cargoCapacity;
}
public void setCargoCapacity(int cargoCapcity)
{
this.cargoCapacity = cargoCapacity;
}
public String getCategory()
{
if(cargoCapacity <= 3300)
return "LIGHT WEIGHT";
else if(cargoCapacity >3300 && cargoCapacity <=6000)
return "MEDIUM";
else
return "HEAVY DUTY";
}
public String toString()
{
return " "+super.toString() +" Cargo Capacity : "+cargoCapacity;
}
}
class TestVehicle
{
public static void main (String[] args)
{
Car car = new Car("HONDA",11);
System.out.println(car);
Truck truck = new Truck("FORD",3000);
System.out.println(truck);
}
}
Output:
Model : HONDA Category : FAMILY noOfSeats : 11
Model : FORD Category : LIGHT WEIGHT Cargo Capacity : 3000
DO ask if any doubt. Please upvote.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.