Write A Program In Java Which Has A Class Car Having Two Instance Variables Topspeed And Name. Override Tostring() Method In Car Class. Create 5 Instances Of Car Class And Print The Instances

0

Write A Program In Java Which Has A Class Car Having Two Instance Variables Topspeed And Name. Override Tostring() Method In Car Class. Create 5 Instances Of Car Class And Print The Instances.

Write a program in java which has a class car having two instance variables topspeed and name. Override tostring() method in car class. Create 5 instances of car class and print the instances

Here's a program in java that creates a class car with instance variables topspeed and name, overrides the tostring() method, creates five instances of the car class, and prints them:


Program

public class Car {
    private int topSpeed;
    private String name;

    public Car(int topSpeed, String name) {
        this.topSpeed = topSpeed;
        this.name = name;
    }

    public String toString() {
        return "Car{" +
                "topSpeed=" + topSpeed +
                ", name='" + name + '\'' +
                '}';
    }

    public static void main(String[] args) {
        Car car1 = new Car(120, "Honda");
        Car car2 = new Car(140, "Toyota");
        Car car3 = new Car(180, "BMW");
        Car car4 = new Car(200, "Mercedes");
        Car car5 = new Car(250, "Audi");

        System.out.println(car1);
        System.out.println(car2);
        System.out.println(car3);
        System.out.println(car4);
        System.out.println(car5);
    }
}

Output
Car{topSpeed=120, name='Honda'} Car{topSpeed=140, name='Toyota'} Car{topSpeed=180, name='BMW'} Car{topSpeed=200, name='Mercedes'} Car{topSpeed=250, name='Audi'}

Explanation

We define a class called car with two instance variables topspeed and name. These instance variables are private, which means they can only be accessed within the car class itself.


We define a constructor for the car class that takes two parameters: topspeed and name. This constructor assigns the values of these parameters to the topspeed and name instance variables, respectively.


We override the tostring() method for the car class. This method returns a string representation of the car instance, which consists of the topspeed and name instance variables enclosed in curly braces.


We define a main() method that creates five instances of the car class with different topspeed and name values.


We call system.Out.Println() on each car instance, which automatically calls the tostring() method of the car class to print a string representation of each car instance.


When we run the program, it prints out the string representation of each car instance created in the main() method.


So the program essentially creates a car class with two instance variables and a tostring() method, creates five instances of the car class with different topspeed and name values, and then prints out a string representation of each car instance.


❤️ I Hope This Helps You Understand, Click Here To Do More Exercise In Java Programming.



Tags

Post a Comment

0Comments
Post a Comment (0)