Design and implement a class called Car that contains instance data that represents the make, model, and year of the car

Design and implement a class called Car that contains instance data that represents the make, model, and year of the car

LANGUAGE: Java

CHALLENGE:

Design and implement a class called Car that contains instance data that represents the make, model, and year of the car (as a String, String and int value respectively).

Define the Car constructor to initialize these values (in that order).
Include getter and setter methods for all instance data, and a toString method that returns a one-line description of the car of the form make-model (year)

for example:
Ford-Thunderbird (1955)

SOLUTION:


public class Car {
    private String make;
    private String model;
    private String year;
    
    public Car(String make,String model,String year) {
        this.make = make;
        this.model=model;
        this.year = year;   
    }

    public String getMake() {
        return make;
    }

    public void setMake(String make) {
        this.make = make;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }
    
    public String toString() {
        return "Make : "+this.make+" Model : "+this.model+" Year: "+this.year;
    }       
}

Add comment