In: Computer Science
Java
Write a class called Car that contains instance data that represents the make, model, and year of the car.
Define the Car constructor to initialize these values
Include getter and setter methods for all instance data and a toString method that returns a one-line description of the car.
Add a method called isAntique that returns a boolean indicating if the car is an antique (if it is more than 45 years old).
Create a driver class called CarTest, whose main method instantiates and updates several Car objects
Must have two constructors, one accepts nothing and another overloaded constructor that accepts parameters.
Must have accessor and mutator methods for each instance variable, and the toString method
Must test ALL (the two constructors, the accessors, and mutators for each variable and the other methods if there are any) methods explicitly in the driver class’s main method with at least two objects.
Addon:
Create an array of five objects of the class you have created. Use a for loop to display the five objects first. Then test all methods using the objects in the array and finally print out each object using a for loop again.
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
_________________
// Car.java
import java.time.Year;
public class Car {
//Declaring instance variables
private String make;
private String model;
private int year;
//Zero argumented constructor
public Car() {
}
//Parameterized constructor
public Car(String make, String model, int year)
{
this.make = make;
this.model = model;
this.year = year;
}
// getters and setters
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 int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
//toString method is used to display the contents
of an object inside it
@Override
public String toString() {
return "Car : Make=" + make + ",
Model:" + model + ", Year:" + year;
}
public boolean isAntique()
{
if(Year.now().getValue()-year>45)
{
return
true;
}
else
return
false;
}
}
___________________
// CarTest.java
public class CarTest {
public static void main(String[] args) {
//Creating an instance of Car
class
Car car1=new Car();
Car car2=new Car("Honda","Civic",2018);
car1.setMake("Ford");
car1.setModel("T-Model");
car1.setYear(1927);
//Displaying the Car class object Info
System.out.println(car1);
boolean b=car1.isAntique();
if(b)
{
System.out.println(car1.getModel()+" is
Antique.");
}
else
{
System.out.println(car1.getModel()+" is not
Antique.");
}
System.out.println(car2);
b=car2.isAntique();
if(b)
{
System.out.println(car2.getModel()+" is
Antique.");
}
else
{
System.out.println(car2.getModel()+" is not
Antique.");
}
Car cars[]={new Car("Volkswagen","Ameo",2019),new
Car("Volkswagen","Polo",2016),new Car("BMW","Gran Coupe",2019),new
Car("Hyundai","Grand i10",2019),new
Car("Renault","Duster",2017)};
for(int i=0;i<cars.length;i++)
{
System.out.println(cars[i]);
}
}
}
__________________________
Output:
Car : Make=Ford, Model:T-Model, Year:1927
T-Model is Antique.
Car : Make=Honda, Model:Civic, Year:2018
Civic is not Antique.
Car : Make=Volkswagen, Model:Ameo, Year:2019
Car : Make=Volkswagen, Model:Polo, Year:2016
Car : Make=BMW, Model:Gran Coupe, Year:2019
Car : Make=Hyundai, Model:Grand i10, Year:2019
Car : Make=Renault, Model:Duster, Year:2017
_______________Could you plz rate me well.Thank You