Java
For this assignment you need to create at least 5 classes.
1.Create a vehicle class ( super class)
2,3. Create two sub classes ( car, bus , truck train or any) for
vehicle class
4 Create a subclass (sportscar or schoolbus or Goodstrain or any)
for car or bus
You need to use the following atleast for one class.
Use a protected variable
Add constructor for superclass and subclass
Override a method which displays a description about the
vehicle
Use super keyword to call a superclass constructor
Use super keyword to call a superclass method to return the number
of wheels
5.Create a vehicle test class to create object of subclass
(sportscar or schoolbus or any) and an object for subclass (car or
bus or any)
You need to invoke all classes using only 2 objects
Code is Given Below:
===========================
Vehicle.java
============================
//here vehicle is base class
public class Vehicle {
//variables
protected String name;
protected int max_speed;
protected int wheels;
public Vehicle(String name, int max_speed) {
this.name = name;
this.max_speed = max_speed;
}
//getter and setter methods
public String getName() {
return name;
}
public int getWheels() {
return wheels;
}
public void setWheels(int wheels) {
this.wheels = wheels;
}
public void setName(String name) {
this.name = name;
}
public int getMax_speed() {
return max_speed;
}
public void setMax_speed(int max_speed) {
this.max_speed = max_speed;
}
//tostring method
public String toString() {
return "Name is : "+name+"\nMax
Speed is : "+max_speed;
}
}
Car.java
=======================
//here Car is child class of vehicle
public class Car extends Vehicle {
//variable
protected int number_of_cylinders;
public Car(String name, int max_speed, int
number_of_cylinders) {
super(name, max_speed);
this.number_of_cylinders =
number_of_cylinders;
}
//getter and setter method
public int getNumber_of_cylinders() {
return number_of_cylinders;
}
public void setNumber_of_cylinders(int
number_of_cylinders) {
this.number_of_cylinders =
number_of_cylinders;
}
//tostring method
public String toString() {
return super.toString()+"\nNumber
of Cylinders Are: "+number_of_cylinders+"\nNo. of Wheels :
"+super.getWheels();
}
}
Bus.java
=====================
public class Bus extends Vehicle{
protected String fuelType;
public Bus(String name, int max_speed, String
fuelType) {
super(name, max_speed);//calling
super class constructor
this.fuelType = fuelType;
}
//getter and setter method
public String getFuelType() {
return fuelType;
}
public void setFuelType(String fuelType) {
this.fuelType = fuelType;
}
public String toString() {
return super.toString()+"\nFuel
Type: "+fuelType;
}
}
SchoolBus.java
=====================
public class SchoolBus extends Bus {
protected int seats;
public SchoolBus(String name, int max_speed, String
fuelType, int seats) {
super(name, max_speed,
fuelType);
this.seats = seats;
}
public String toString() {
return super.toString()+"\nSeats :
"+seats;
}
}
SportsCar.java
====================
public class SportsCar extends Car {
protected double milage;
public SportsCar(String name, int max_speed, int
number_of_cylinders, double milage) {
super(name, max_speed,
number_of_cylinders);
this.milage = milage;
}
public String toString() {
return super.toString()+"\nMilage
is : "+milage;
}
}
Test.java
================
public class Test {
public static void main(String[] args) {
//creating object
SportsCar sc=new SportsCar("BMW",
320, 12, 8);
SchoolBus sb=new
SchoolBus("mercedes", 120, "CNG", 32);
Car c=new Car("Fiat", 180,
10);
Bus b=new Bus("F8",120,
"Petrol");
Vehicle v=new
Vehicle("Van",80);
c.setWheels(4);
//printing details
System.out.println("Sport car
");
System.out.println(sc);
System.out.println("===========================");
System.out.println("School bus
");
System.out.println(sb);
System.out.println("===========================");
System.out.println("Car ");
System.out.println(c);
System.out.println("===========================");
System.out.println("Bus ");
System.out.println(b);
System.out.println("===========================");
System.out.println("Vehicle
");
System.out.println(v);
System.out.println("===========================");
}
}
Output:
===========
Code Snapshot:
==================
Get Answers For Free
Most questions answered within 1 hours.