In: Computer Science
/***********************************Car.java******************************/
public class Car {
/*
* private instance variable
*/
private String model;
private String color;
private int year;
private double currentSpeed;
/*
* Default constructor
*/
Car() {
this.model = "";
this.color = "";
this.year = 2000;
this.currentSpeed = 0.0;
}
/*
* Parameterized constructor
*/
public Car(String model, String color, int year,
double currentSpeed) {
this.model = model;
this.color = color;
this.year = year;
this.currentSpeed =
currentSpeed;
}
// getter and setter method for get and set the
instance variables
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public double getCurrentSpeed() {
return currentSpeed;
}
public void setCurrentSpeed(double currentSpeed)
{
this.currentSpeed =
currentSpeed;
}
// display method
public void display() {
System.out.println(model + ", "
+ color + ", " + year + "(" + currentSpeed + ")");
}
}
/*******************************TestCar.java********************************/
public class TestCar {
public static void main(String[] args) {
//instantiate a car object
Car car = new Car("Ford", "Red",
2019, 43.0);
//display car
car.display();
//modification in car's
features
car.setYear(2018);
System.out.println("Car's
features");
car.display();
car.setColor("Green");
car.setYear(2017);
System.out.println("Car's
features");
car.display();
car.setColor("YELLOW");
car.setYear(2019);
car.setCurrentSpeed(50.0);
System.out.println("Car's
features");
car.display();
car.setColor("Purple");
car.setYear(2015);
car.setCurrentSpeed(70.0);
System.out.println("Car's
features");
car.display();
}
}
/***********************************output************************/
Ford, Red, 2019(43.0)
Car's features
Ford, Red, 2018(43.0)
Car's features
Ford, Green, 2017(43.0)
Car's features
Ford, YELLOW, 2019(50.0)
Car's features
Ford, Purple, 2015(70.0)
Please let me know if you have any doubt or modify the answer, Thanks :)