Consider the following situation ? • A Vehicle has fuelCapacity, fuelOnBoard, se
ID: 3717205 • Letter: C
Question
Consider the following situation ?
• A Vehicle has fuelCapacity, fuelOnBoard, serialNumber, passengerCapacity, and netWeight
• A Car is - a Vehicle with additional properties of tireWear, licensePlate, and trunkCapacity
• A Plane is - a Vehicle with the additional property of numberOfPropellers
• A Boat is - a Vehicle with the additional property of draft (distance from water line to bottom of boat)
• A SailBoat is - a Boat with the additi onal property of numberSails
• A MotorBoat is - a Boat with the additional property of horsePower
Code each of these classes with a parameterized constructor and a toString() method, which should list a title for each instance variable (such as:
return “Fuel Capacity: t” + fuelCapacity + ……). Use super whenever possible.
Code a main method to test each of these classes by creating an object of each class (hard code the data) and then print the object.
Explanation / Answer
//Vechile class definition
class Vehicle
{
float fuelCapacity, netWeight;
int passengerCapacity;
String serialNumber,fuelOnBoard;
Vehicle(float fcap,String fob,String sno,int pcap,float netw)
{
fuelCapacity=fcap;
fuelOnBoard=fob;
serialNumber=sno;
passengerCapacity=pcap;
netWeight=netw;
}
public String toString() {
return "Fuel Capacity : "+ fuelCapacity+ " FuelOnBoard: "+fuelOnBoard+"
serialNumber:"+serialNumber+" Passenger Capacity:"+passengerCapacity+" Net Weight:"+
netWeight;
}
}
//Car class definition
class Car extends Vehicle
{
String tireWear;
String licensePlate;
float trunkCapacity;
Car(float fcap,String fob,String sno,int pcap,float netw,String tw,String lp,float tc)
{
super(fcap,fob,sno,pcap,netw);
tireWear=tw;
licensePlate=lp;
trunkCapacity=tc;
}
public String toString(){
String ss=super.toString();
return ss+" Tire Wear: "+tireWear+" License Plate:"+licensePlate+" Trunk Capacity:"+
trunkCapacity;
}
}
//Plane class definition
class Plane extends Vehicle
{
int numberOfPropellers;
Plane(float fcap,String fob,String sno,int pcap,float netw,int np)
{
super(fcap,fob,sno,pcap,netw);
numberOfPropellers=np;
}
public String toString(){
String ss=super.toString();
return ss+" Number Of Propellers: "+numberOfPropellers;
}
}
//Boat class definition
class Boat extends Vehicle
{
float draft;
Boat(float fcap,String fob,String sno,int pcap,float netw,float d)
{
super(fcap,fob,sno,pcap,netw);
draft=d;
}
public String toString(){
String ss=super.toString();
return ss+" Draft: "+draft;
}
}
//SailBoat class definition
class SailBoat extends Boat
{
int numberSails;
SailBoat(float fcap,String fob,String sno,int pcap,float netw,float d,int ns)
{
super(fcap,fob,sno,pcap,netw,d);
numberSails=ns;
}
public String toString(){
String ss=super.toString();
return ss+" Number Sails: "+numberSails;
}
}
//MotorBoat class definition
class MotorBoat extends Boat
{
float horsePower;
MotorBoat(float fcap,String fob,String sno,int pcap,float netw,float d,float hp)
{
super(fcap,fob,sno,pcap,netw,d);
horsePower=hp;
}
public String toString(){
String ss=super.toString();
return ss+" Horse Power: "+horsePower;
}
}
public class TestClasses{
public static void main(String args[]){
//Creating object of Car class
Car c=new Car(36,"Petrol","76421",5,3500,"Tread Wear","123 WSP",61);
//Creating object of Plane class
Plane p=new Plane( 23858,"Jet fuel","768335",532,12500,3);
//Creating object of Boat class
Boat b=new Boat(30,"gasoline","FI-BUSA0001K506",20,2200,5);
//Creating object of SailBoat class
SailBoat sb=new SailBoat(0,"No Fuel","SI-SAIL0001",7,1100,5,10);
//Creating object of MotorBoat class
MotorBoat mb=new MotorBoat(30,"gasoline","FI-BUSA0001K506",20,2200,5,300);
//Printing objects of class
System.out.println("Testing Car class");
System.out.println(c);
System.out.println("Testing Plane class");
System.out.println(p);
System.out.println("Testing Boat class");
System.out.println(b);
System.out.println("Testing SailBoat class");
System.out.println(sb);
System.out.println("Testing MotorBoat class");
System.out.println(mb);
}}