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 representation of all vehicle data. Classes Car, Bus, and Truck will override inherited toString method from class vehicle in order to provide apropriate 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 (7 total) and it will display the information about those objects by invoking their toString methods.
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
=================================
// Vehicle.java
public class Vehicle {
// data members declared as private
private String make;
private double weight;
private double height;
private double length;
private int maxSpeed;
private int noOfDoors;
private int numberSeats;
/**
* @param make
* @param weight
* @param height
* @param length
* @param maxSpeed
* @param noOfDoors
* @param maxPassengers
* @param numberSeats
*/
public Vehicle(String make, double weight, double
height, double length,
int maxSpeed,
int noOfDoors, int numberSeats) {
this.make = make;
this.weight = weight;
this.height = height;
this.length = length;
this.maxSpeed = maxSpeed;
this.noOfDoors = noOfDoors;
this.numberSeats = numberSeats;
}
/**
* @return the make
*/
public String getMake() {
return make;
}
/**
* @param make
* the make to set
*/
public void setMake(String make) {
this.make = make;
}
/**
* @return the weight
*/
public double getWeight() {
return weight;
}
/**
* @param weight
* the weight to set
*/
public void setWeight(double weight) {
this.weight = weight;
}
/**
* @return the height
*/
public double getHeight() {
return height;
}
/**
* @param height
* the height to set
*/
public void setHeight(double height) {
this.height = height;
}
/**
* @return the length
*/
public double getLength() {
return length;
}
/**
* @param length
* the length to set
*/
public void setLength(double length) {
this.length = length;
}
/**
* @return the maxSpeed
*/
public int getMaxSpeed() {
return maxSpeed;
}
/**
* @param maxSpeed
* the maxSpeed to set
*/
public void setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
}
/**
* @return the noOfDoors
*/
public int getNoOfDoors() {
return noOfDoors;
}
/**
* @param noOfDoors
* the noOfDoors to set
*/
public void setNoOfDoors(int noOfDoors) {
this.noOfDoors = noOfDoors;
}
/**
* @return the numberSeats
*/
public int getNumberSeats() {
return numberSeats;
}
/**
* @param numberSeats
* the numberSeats to set
*/
public void setNumberSeats(int numberSeats) {
this.numberSeats =
numberSeats;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Make=" + make + ", weight="
+ weight + ", height=" + height
+ ", length=" + length + ", maxSpeed=" +
maxSpeed
+ ", noOfDoors=" + noOfDoors + ", Number of
Seats ="
+ numberSeats;
}
}
==========================================
// Car.java
public class Car extends Vehicle {
private int maxPassengers;
private boolean isConvertable;
public Car(String make, double weight,
double height, double length,
int maxSpeed,
int noOfDoors, int maxPassengers, int numberSeats,
boolean
isConvertable) {
super(make, weight, height, length,
maxSpeed, noOfDoors, numberSeats);
this.maxPassengers =
maxPassengers;
}
/**
* @return the maxPassengers
*/
public int getMaxPassengers() {
return maxPassengers;
}
/**
* @param maxPassengers
* the maxPassengers to set
*/
public void setMaxPassengers(int maxPassengers)
{
this.maxPassengers =
maxPassengers;
}
/**
* @return the isConvertable
*/
public boolean isConvertable() {
return isConvertable;
}
/**
* @param isConvertable
* the isConvertable to set
*/
public void setConvertable(boolean isConvertable)
{
this.isConvertable =
isConvertable;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Car ::" + super.toString()
+ ", MaxPassengers=" + maxPassengers
+ ", isConvertable=" + isConvertable;
}
}
==========================================
// Bus.java
public class Bus extends Vehicle {
private int numberAxles;
public Bus(String make, double weight,
double height, double length,
int maxSpeed,
int noOfDoors, int numberSeats, int numberAxles) {
super(make, weight, height, length,
maxSpeed, noOfDoors, numberSeats);
this.numberAxles =
numberAxles;
}
/**
* @return the numberAxles
*/
public int getNumberAxles() {
return numberAxles;
}
/**
* @param numberAxles
* the numberAxles to set
*/
public void setNumberAxles(int numberAxles) {
this.numberAxles =
numberAxles;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Bus ::" + super.toString()
+ ", Number Axles=" + numberAxles;
}
}
============================================
// Truck.java
public class Truck extends Vehicle {
private double maxWeightLoad;
/**
* @param make
* @param weight
* @param height
* @param length
* @param maxSpeed
* @param noOfDoors
* @param numberSeats
* @param maxWeightLoad
*/
public Truck(String make, double weight, double
height, double length,
int maxSpeed,
int noOfDoors, int numberSeats, double maxWeightLoad) {
super(make, weight, height, length,
maxSpeed, noOfDoors, numberSeats);
this.maxWeightLoad =
maxWeightLoad;
}
/**
* @return the maxWeightLoad
*/
public double getMaxWeightLoad() {
return maxWeightLoad;
}
/**
* @param maxWeightLoad
* the maxWeightLoad to set
*/
public void setMaxWeightLoad(double maxWeightLoad)
{
this.maxWeightLoad =
maxWeightLoad;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Truck ::" +
super.toString() + ", Max Weight Load="
+ maxWeightLoad;
}
}
=========================================
// Test.java
import java.util.ArrayList;
public class Test {
public static void main(String[] args)
{
ArrayList<Vehicle>
vehicles=new ArrayList<Vehicle>();
vehicles.add(new Car("Benz", 350,
2, 7, 230, 4, 4, 4, true));
vehicles.add(new Car("Toyota", 320,
3, 7, 220, 4, 6, 7, false));
vehicles.add(new Bus("Volvo", 2000,
7, 15, 180,3, 56, 2));
vehicles.add(new Bus("Benz", 2300,
7, 15, 190, 3, 32, 2));
vehicles.add(new Truck("Mahindra",
1200, 5, 10, 160, 2, 2, 1000));
vehicles.add(new Truck("Tata",
1100,5, 10, 170, 2, 2,1200));
vehicles.add(new Truck("Ashok
Leyland", 1100, 5, 10, 160, 2, 2, 1400));
for(int
i=0;i<vehicles.size();i++)
{
System.out.println(vehicles.get(i));
System.out.println("-----------------------------------------------------------------------------");
}
}
}
=======================================
Output:
Car ::Make=Benz, weight=350.0, height=2.0,
length=7.0, maxSpeed=230, noOfDoors=4, Number of Seats =4,
MaxPassengers=4, isConvertable=false
-----------------------------------------------------------------------------
Car ::Make=Toyota, weight=320.0, height=3.0, length=7.0,
maxSpeed=220, noOfDoors=4, Number of Seats =7, MaxPassengers=6,
isConvertable=false
-----------------------------------------------------------------------------
Bus ::Make=Volvo, weight=2000.0, height=7.0, length=15.0,
maxSpeed=180, noOfDoors=3, Number of Seats =56, Number
Axles=2
-----------------------------------------------------------------------------
Bus ::Make=Benz, weight=2300.0, height=7.0, length=15.0,
maxSpeed=190, noOfDoors=3, Number of Seats =32, Number
Axles=2
-----------------------------------------------------------------------------
Truck ::Make=Mahindra, weight=1200.0, height=5.0, length=10.0,
maxSpeed=160, noOfDoors=2, Number of Seats =2, Max Weight
Load=1000.0
-----------------------------------------------------------------------------
Truck ::Make=Tata, weight=1100.0, height=5.0, length=10.0,
maxSpeed=170, noOfDoors=2, Number of Seats =2, Max Weight
Load=1200.0
-----------------------------------------------------------------------------
Truck ::Make=Ashok Leyland, weight=1100.0, height=5.0, length=10.0,
maxSpeed=160, noOfDoors=2, Number of Seats =2, Max Weight
Load=1400.0
-----------------------------------------------------------------------------
=====================Could you plz rate me well.Thank
You