In: Computer Science
JAVA:
*USING INHERITANCE
*USING SUPER IN TH CONSTRUCTOR
*USING SUPER IN THE METHOD
*METHOD OVERRIDING
Create an application with 5 classes:
1.) Vehicle
2.) Car
3.) Bus
4.) Truck
5.) Tester
Following characteristics should be used:
make, weight, height, length, maxSpeed, numberDoors, maxPassengers, isCpnvertable, numberSeats,
maxWeightLoad, numberAxels
**Class Vehicle: should have a constructor that initializes all of it's data
**Classes Car, Bus, Truck: will have constructors that resuse their parents constructors and provide additional code for initalizing their specific data
**Class Vehicle: Should have toString method that returns string representatipn of all vehicle data. Classes Car, Bus, Truck will override the inheritted toString method from class vehicle in order to provide appropriate string representation of all data for their classes which includes inheritted data from Vehicle class and their own data.
** Class Tester will instantiate 1-2 objects from those 4 classes (at least 5 total) and it will display the information about those objects by invoking their toString methods
The java code is as follows:-
class Vehicle //parent class
{
String make;
double weight, height, length, maxSpeed, maxWeightLoad;
int numberDoors, maxPassengers, numberSeats, numberAxels;
boolean isCpnvertable;
Vehicle(String m,double w,double h, double l, double ms, double
mwl, int nd, int mp, int ns, int na, boolean ic) //parent
constructor
{
make= m;
weight=w;
height=h;
length=l;
maxSpeed=ms;
numberDoors=nd;
maxPassengers=mp;
isCpnvertable=ic;
numberSeats=ns;
maxWeightLoad=mwl;
numberAxels=na;
}
@Override
public String toString() //override toString method
{
return "made by: "+this.make+", weight: "+this.weight+" kg, height:
"+this.height+" metre, length: "+this.length+" metre, maxSpeed:
"
+this.maxSpeed+" km/h, numberDoors: "+this.numberDoors+",
maxPassengers: "+this.maxPassengers+", isCpnvertable:
"+this.isCpnvertable+
", numberSeats: "+this.isCpnvertable+", numberSeats:
"+this.numberSeats+", maxWeightLoad: "+this.maxWeightLoad+" kg,
numberAxels: "+this.numberAxels;
}
}
class Car extends Vehicle //child class
{
String color,type; //additional attributes of child class
Car(String m,double w,double h, double l, double ms, double mwl,
int nd, int mp, int ns, int na, boolean ic,String c,String t)
{
super(m, w, h, l, ms, mwl, nd, mp, ns, na, ic);//call constructor
of parent class
color=c;
type=t;
}
@Override
public String toString() //override toString method
{
return "Car details:\n"+super.toString()+" color: "+this.color+"
type: "+this.type;
}
}
class Bus extends Vehicle //child class
{
int deckers; //additional attributes of child class
Bus(String m,double w,double h, double l, double ms, double mwl,
int nd, int mp, int ns, int na, boolean ic,int d)
{
super(m, w, h, l, ms, mwl, nd, mp, ns, na, ic);//call constructor
of parent class
deckers=d;
}
@Override
public String toString() //override toString method
{
return "Bus details:\n"+super.toString()+" number of deckers:
"+this.deckers;
}
}
class Truck extends Vehicle //child class
{
int tyres; //additional attributes of child class
Truck(String m,double w,double h, double l, double ms, double mwl,
int nd, int mp, int ns, int na, boolean ic,int t)
{
super(m, w, h, l, ms, mwl, nd, mp, ns, na, ic);//call constructor
of parent class
tyres=t;
}
@Override
public String toString() //override toString method
{
return "Truck details:\n"+super.toString()+" number of tyres used:
"+this.tyres;
}
}
public class Main //tester class
{
public static void main(String[] args)
{
//5 objects of parent or child class
Vehicle v=new
Vehicle("TATA",125,1.5,5,100,200,4,5,5,4,true);
System.out.println("Vehicle
details:\n"+v.toString());
Car c1=new
Car("Toyoto",125,1.5,3,120,300,4,7,7,4,true,"blue","small");
System.out.println(c1.toString());
Car c2=new
Car("Nano",100,1,2,100,300,4,4,4,4,true,"red","small");
System.out.println(c2.toString());
Bus b=new
Bus("VRL",335,3,5,120,2500,2,50,52,4,true,2);
System.out.println(b.toString());
Truck t=new
Truck("TATA",535,3.3,7,120,2500,2,5,5,4,true,10);
System.out.println(t.toString());
}
}
The output of the above code is as follows:-
Vehicle details:
made by: TATA, weight: 125.0 kg, height: 1.5 metre, length: 5.0
metre, maxSpeed: 100.0 km/h, numberDoors: 4, maxPassengers: 5,
isCpnvertable: true, numberSeats: true, numberSeats: 5,
maxWeightLoad: 200.0 kg, numberAxels: 4
Car details:
made by: Toyoto, weight: 125.0 kg, height: 1.5 metre, length: 3.0
metre, maxSpeed: 120.0 km/h, numberDoors: 4, maxPassengers: 7,
isCpnvertable: true, numberSeats: true, numberSeats: 7,
maxWeightLoad: 300.0 kg, numberAxels: 4 color: blue type:
small
Car details:
made by: Nano, weight: 100.0 kg, height: 1.0 metre, length: 2.0
metre, maxSpeed: 100.0 km/h, numberDoors: 4, maxPassengers: 4,
isCpnvertable: true, numberSeats: true, numberSeats: 4,
maxWeightLoad: 300.0 kg, numberAxels: 4 color: red type:
small
Bus details:
made by: VRL, weight: 335.0 kg, height: 3.0 metre, length: 5.0
metre, maxSpeed: 120.0 km/h, numberDoors: 2, maxPassengers: 50,
isCpnvertable: true, numberSeats: true, numberSeats: 52,
maxWeightLoad: 2500.0 kg, numberAxels: 4 number of deckers: 2
Truck details:
made by: TATA, weight: 535.0 kg, height: 3.3 metre, length: 7.0
metre, maxSpeed: 120.0 km/h, numberDoors: 2, maxPassengers: 5,
isCpnvertable: true, numberSeats: true, numberSeats: 5,
maxWeightLoad: 2500.0 kg, numberAxels: 4 number of tyres used:
10
The screenshot of the above code is as follows:-