Skip to main content

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());

        }

    }

}


Algorithm:
  1. Define a class Vehicle with private instance variables registrationNumber, color, and type.
  2. Define a constructor in the Vehicle class to initialize these variables.
  3. Define getter methods to retrieve the values of these variables.
  4. Define subclasses Car, Truck, and Motorcycle, each extending the Vehicle class.
  5. Each subclass has additional attributes: make, CC, and fuelType.
  6. Each subclass has a constructor that initializes both the attributes inherited from Vehicle and its own attributes.
  7. Define getter methods in each subclass to retrieve the values of its additional attributes.
  8. Define a class VehicleTest.
  9. Inside the VehicleTest class:
  10. Define the printVehicleDetails method:
  11. End of the program.










Popular posts from this blog

krushnaaa

  import java . awt .* ; import java . awt . event .* ; public class SimpleAWTExample {     public static void main ( String [] args ) {         // Create a Frame         Frame frame = new Frame ( "Simple AWT Example" );         // Create a Button         Button button = new Button ( "Click Me!" );         // Set layout for the Frame         frame . setLayout ( new FlowLayout ());         // Add the Button to the Frame         frame . add ( button );         // Set size of the Frame         frame . setSize ( 300 , 200 ); // Width: 300 pixels, Height: 200 pixels         // Make the Frame visible         frame . setVisible ( true );         // Add a WindowListener to handle closing event     ...

Write a program to create a class Student2 along with two method getData (), printData () to get the value through argument and display the data in printData. Create the two objects s1, s2 to declare and access the values from class STtest.

 CODE: import java.util.Scanner; class Student2 {     private String name;     private int age;          // Method to set data     public void getData(String name, int age) {         this.name = name;         this.age = age;     }          // Method to print data     public void printData() {         System.out.println("Name: " + name);         System.out.println("Age: " + age);     } } public class STtest {     public static void main(String[] args) {         Scanner scanner = new Scanner(System.in);                  // Creating objects of Student2 class         Student2 s1 = new Student2();         Student2 s2 = new Student2();              ...

Create an in-order threaded binary search tree and perform the traversals.(Double Thread)

 CODE: #include <iostream> using namespace std; struct Node {     int data;     Node* left;     Node* right;     bool leftThread;     bool rightThread; }; Node* createThreadedBST(int arr[], int n) {     Node* root = nullptr;     for (int i = 0; i < n; ++i) {         Node* newNode = new Node;         newNode->data = arr[i];         newNode->left = newNode->right = nullptr;         newNode->leftThread = newNode->rightThread = true;         if (root == nullptr) {             root = newNode;         } else {             Node* current = root;             Node* parent = nullptr;             while (true) {                ...