I need help with this in a Java Language please. Write a class called Car that h
ID: 3681587 • Letter: I
Question
I need help with this in a Java Language please. Write a class called Car that holds information about an automobile: Ex,Make (e.g., Ford, Toyota, Fiat) Model (e.g., Taurus, Campry, 500) Year (e.g., 2012). Define a constructor to initialize these values. Define a toString method. Define a method called isAntique that returns true if the car is more than 25 years old, and false otherwise. Create a test driver called Car_Test Accepts inputs from the keyboard for each member variable. Instantiates a Car object with the entered values. Outputs the values to the screen using println(). Determines if the car is an antique and outputs the result.
Explanation / Answer
import java.util.Calendar;
public class Car {
private String make;
private String model;
private int year;
public Car(String make, String model, int year) {
this.make = make;
this.model = model;
this.year = year;
}
public boolean isAntique(){
// getting current year
int year = Calendar.getInstance().get(Calendar.YEAR);
if(Math.abs(year - this.year) >25)
return true;
else
return false;
}
@Override
public String toString() {
return "Make: "+make+", Model: "+model+", Year: "+year;
}
}
########### Car_Test.java ##############
import java.util.Scanner;
public class Car_Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter make for car: ");
String make = sc.next();
System.out.print("Enter model for car: ");
String model = sc.next();
System.out.print("Enter year of manufacturing: ");
int year = sc.nextInt();
// creating Car object
Car c = new Car(make, model, year);
System.out.println(c.toString());
System.out.println("isAntique ? : "+c.isAntique());
}
}
/*
Output:
Enter make for car: Toyota
Enter model for car: Campry
Enter year of manufacturing: 1995
Make: Toyota, Model: Campry, Year: 1995
isAntique ? : false
*/