In: Computer Science
Write an application with five classes Vehicle, Car, Bus, Truck, and Tester class with main method in it.
The following characteristics should be used: make, weight, height, length, maxSpeed, numberDoors, maxPassenges, isConvertable, numberSeats, maxWeightLoad, and numberAxels.
Characteristics that are applicable to all vehicles should be instance variables in the Vehicle class. The others should be in the class(s) where they belong.
Class vehicle should have constructor that initializes all its data. Classes Car, Bus, and Truck will have constructors which will reuse their parents constructor and provide additional code for initializing their specific data.
Class Vehicle should have toString method that returns string representtion of all vehicle data. Classes Car, Bus, and Truck will override inherited toString method from class vehicle in order to provide appropriate string representation of all data for their classes which includes inherited data from Vehicle class and their own data.
Class Tester will instantiate 1-2 objects from those four classes (at least five total) and it will display the information about those objects by invoking their toString methods.
package com.motor;
public class Vehicle
{
String make = null;
double weight = 0.0d;
double height = 0.0d;
double length = 0.0d;
double maxSpeed = 0.0d;
int numberDoors = 0;
boolean isConvertable = false;
int numberSeats = 0;
int numberAxels = 0;
public Vehicle()
{
System.out.println("Enter Default Constructor for Vehicle
Class");
}
//Vehical Class Parameterized Constructor
public Vehicle(String make, double weight, double height, double
length, double maxSpeed, int numberDoors, boolean isConvertable,
int numberSeats, int numberAxels)
{
this.make = make;
this.weight = weight;
this.height = height;
this.length = length;
this.maxSpeed = maxSpeed;
this.numberDoors = numberDoors;
this.isConvertable = isConvertable;
this.numberSeats = numberSeats;
this.numberAxels = numberAxels;
}
//Overriding toString() method
public String toString()
{
return "Make-"+make+" Corp. : Weight-"+weight+" Kgs :
Height-"+height+" meters : Length-"+length+" meters :
MaxSpeed-"+maxSpeed+" KMPH : NumberOfDoors-"+numberDoors+" :
IsConvertable-"+isConvertable+" : NumberOfSeats-"+numberSeats+" :
NumberOfAxels-"+numberAxels;
}
}
package com.motor;
public class Truck extends Vehicle
{
double maxWeightLoad = 0.0d;
public Truck()
{
System.out.println("Enter Default Constructor for Truck
Class");
}
//Truck Class Parameterized Constructor
public Truck(String make, double weight, double height, double
length, double maxSpeed, int numberDoors, boolean isConvertable,
int numberSeats, int numberAxels, double maxWeightLoad)
{
super(make,weight,height,length,maxSpeed,numberDoors,isConvertable,numberSeats,numberAxels);
this.maxWeightLoad = maxWeightLoad;
System.out.println("This is a Truck");
}
//Overriding toString()
public String toString()
{
String retString = super.toString();
return retString+" : MaxWeightLoad-"+maxWeightLoad+" Tonns";
}
}
package com.motor;
public class Car extends Vehicle
{
int maxPassengers = 0;
public Car()
{
System.out.println("Enter Default Constructor for Car
Class");
}
//Car Class Parameterized Constructor
public Car(String make, double weight, double height, double
length, double maxSpeed, int numberDoors, boolean isConvertable,
int numberSeats, int numberAxels, int maxPassengers)
{
super(make,weight,height,length,maxSpeed,numberDoors,isConvertable,numberSeats,numberAxels);
this.maxPassengers = maxPassengers;
System.out.println("This is a Car");
}
//Overriding toString()
public String toString()
{
String retString = super.toString();
return retString+" : MaxPassengers-"+maxPassengers;
}
}
package com.motor;
public class Bus extends Vehicle
{
int maxPassengers = 0;
public Bus()
{
System.out.println("Enter Default Constructor for Bus
Class");
}
//Bus Class Parameterized Constructor
public Bus(String make, double weight, double height, double
length, double maxSpeed, int numberDoors, boolean isConvertable,
int numberSeats, int numberAxels, int maxPassengers)
{
super(make,weight,height,length,maxSpeed,numberDoors,isConvertable,numberSeats,numberAxels);
this.maxPassengers = maxPassengers;
System.out.println("This is a Bus");
}
//Overriding toString()
public String toString()
{
String retString = super.toString();
return retString+" : MaxPassengers-"+maxPassengers;
}
}
package com.motor;
public class Testor
{
public Testor()
{
System.out.println("Enter Default Constructor for Testor
Class");
}
public static void main(String[] args)
{
Vehicle vehicle1 = new Car("Mercedez AMG
Benz",950,2.5,4,220.3,4,false,4,2,4);
System.out.println(vehicle1.toString());
Vehicle vehicle2 = new Bus("Tata
Marcopolo",2050,2.1,7,100.4,3,true,72,3,72);
System.out.println(vehicle2.toString());
Vehicle vehicle3 = new Truck("Ashok
Layland",1980,2.2,9.8,80.7,3,false,4,3,72);
System.out.println(vehicle3.toString());
}
}