Write a java program to create class car, truck and motorcycle which extends the vehicle class (attribute registration number, color, type of vehicle) with their own attribute like make, CC and fuel type. Input data from the user and print all the details.
CODE:
import java.util.Scanner;
class Vehicle {
private String registrationNumber;
private String color;
private String type;
// Constructor
public Vehicle(String registrationNumber, String color, String type) {
this.registrationNumber = registrationNumber;
this.color = color;
this.type = type;
}
// Getters
public String getRegistrationNumber() {
return registrationNumber;
}
public String getColor() {
return color;
}
public String getType() {
return type;
}
}
class Car extends Vehicle {
private String make;
private int CC;
private String fuelType;
// Constructor
public Car(String registrationNumber, String color, String type, String make, int CC, String fuelType) {
super(registrationNumber, color, type);
this.make = make;
this.CC = CC;
this.fuelType = fuelType;
}
// Getters
public String getMake() {
return make;
}
public int getCC() {
return CC;
}
public String getFuelType() {
return fuelType;
}
}
class Truck extends Vehicle {
private String make;
private int CC;
private String fuelType;
// Constructor
public Truck(String registrationNumber, String color, String type, String make, int CC, String fuelType) {
super(registrationNumber, color, type);
this.make = make;
this.CC = CC;
this.fuelType = fuelType;
}
// Getters
public String getMake() {
return make;
}
public int getCC() {
return CC;
}
public String getFuelType() {
return fuelType;
}
}
class Motorcycle extends Vehicle {
private String make;
private int CC;
private String fuelType;
// Constructor
public Motorcycle(String registrationNumber, String color, String type, String make, int CC, String fuelType) {
super(registrationNumber, color, type);
this.make = make;
this.CC = CC;
this.fuelType = fuelType;
}
// Getters
public String getMake() {
return make;
}
public int getCC() {
return CC;
}
public String getFuelType() {
return fuelType;
}
}
public class VehicleTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input for Car
System.out.println("Enter Car Details:");
System.out.print("Registration Number: ");
String carRegNumber = scanner.nextLine();
System.out.print("Color: ");
String carColor = scanner.nextLine();
System.out.print("Type: ");
String carType = scanner.nextLine();
System.out.print("Make: ");
String carMake = scanner.nextLine();
System.out.print("CC: ");
int carCC = scanner.nextInt();
scanner.nextLine(); // Consume newline
System.out.print("Fuel Type: ");
String carFuelType = scanner.nextLine();
// Create Car object
Car car = new Car(carRegNumber, carColor, carType, carMake, carCC, carFuelType);
// Input for Truck
System.out.println("\nEnter Truck Details:");
System.out.print("Registration Number: ");
String truckRegNumber = scanner.nextLine();
System.out.print("Color: ");
String truckColor = scanner.nextLine();
System.out.print("Type: ");
String truckType = scanner.nextLine();
System.out.print("Make: ");
String truckMake = scanner.nextLine();
System.out.print("CC: ");
int truckCC = scanner.nextInt();
scanner.nextLine(); // Consume newline
System.out.print("Fuel Type: ");
String truckFuelType = scanner.nextLine();
// Create Truck object
Truck truck = new Truck(truckRegNumber, truckColor, truckType, truckMake, truckCC, truckFuelType);
// Input for Motorcycle
System.out.println("\nEnter Motorcycle Details:");
System.out.print("Registration Number: ");
String motorcycleRegNumber = scanner.nextLine();
System.out.print("Color: ");
String motorcycleColor = scanner.nextLine();
System.out.print("Type: ");
String motorcycleType = scanner.nextLine();
System.out.print("Make: ");
String motorcycleMake = scanner.nextLine();
System.out.print("CC: ");
int motorcycleCC = scanner.nextInt();
scanner.nextLine(); // Consume newline
System.out.print("Fuel Type: ");
String motorcycleFuelType = scanner.nextLine();
// Create Motorcycle object
Motorcycle motorcycle = new Motorcycle(motorcycleRegNumber, motorcycleColor, motorcycleType, motorcycleMake, motorcycleCC, motorcycleFuelType);
// Printing details
System.out.println("\nCar Details:");
printVehicleDetails(car);
System.out.println("\nTruck Details:");
printVehicleDetails(truck);
System.out.println("\nMotorcycle Details:");
printVehicleDetails(motorcycle);
scanner.close();
}
// Method to print vehicle details
public static void printVehicleDetails(Vehicle vehicle) {
System.out.println("Registration Number: " + vehicle.getRegistrationNumber());
System.out.println("Color: " + vehicle.getColor());
System.out.println("Type: " + vehicle.getType());
if (vehicle instanceof Car) {
Car car = (Car) vehicle;
System.out.println("Make: " + car.getMake());
System.out.println("CC: " + car.getCC());
System.out.println("Fuel Type: " + car.getFuelType());
} else if (vehicle instanceof Truck) {
Truck truck = (Truck) vehicle;
System.out.println("Make: " + truck.getMake());
System.out.println("CC: " + truck.getCC());
System.out.println("Fuel Type: " + truck.getFuelType());
} else if (vehicle instanceof Motorcycle) {
Motorcycle motorcycle = (Motorcycle) vehicle;
System.out.println("Make: " + motorcycle.getMake());
System.out.println("CC: " + motorcycle.getCC());
System.out.println("Fuel Type: " + motorcycle.getFuelType());
}
}
}
- Define a class Vehicle with private instance variables registrationNumber, color, and type.
- Define a constructor in the Vehicle class to initialize these variables.
- Define getter methods to retrieve the values of these variables.
- Define subclasses Car, Truck, and Motorcycle, each extending the Vehicle class.
- Each subclass has additional attributes: make, CC, and fuelType.
- Each subclass has a constructor that initializes both the attributes inherited from Vehicle and its own attributes.
- Define getter methods in each subclass to retrieve the values of its additional attributes.
- Define a class VehicleTest.
- Inside the VehicleTest class:
- Define the printVehicleDetails method:
- End of the program.