In a Java languages. Write a class called Car that holds information about an au
ID: 3681018 • Letter: I
Question
In a Java languages. Write a class called Car that holds information about an automobile: Make (e.g., Chevy, Toyota, Fiat ) Model (e.g., Impala, Corolla, Uno) Year (e.g., 2012). Define a constructor to initialize these values. Define a to String method. Define a method called is Antique 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
Hi, Please find the below programs for your requirements.
Car.java
import java.util.Calendar;
public class Car {
private String automobile;
private String model;
private int year;
Car(String autmobie, String model, int year){
this.automobile = autmobie;
this.model = model;
this.year = year;
}
public String toString(){
String s;
s = "Automobile :"+automobile+" | Model : "+model+" | Year :"+year;
return s;
}
public boolean antique(){
boolean flag;
//determine current year
Calendar today = Calendar.getInstance();
int currentYear = today.get(Calendar.YEAR);
if( (currentYear - year) > 25)
flag = true;
else
flag = false;
return flag;
}
}
Car_Test.java
public class Car_Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
java.util.Scanner in = new java.util.Scanner(System.in);
System.out.println("Please Enter Automobile: ");
String autommobile = in.next();
System.out.println("Please Enter Model: ");
String model = in.next();
System.out.println("Please Enter Year: ");
int year = in.nextInt();
Car c = new Car(autommobile, model, year);
System.out.println(c.toString());
boolean status = c.antique();
System.out.println("The Car is more than 25 years old :"+status);
}
}
Output:
Please Enter Automobile:
Toyota
Please Enter Model:
Corolla
Please Enter Year:
2012
Automobile :Toyota | Model : Corolla | Year :2012
The Car is more than 25 years old :false
Note: Input vaidations not performed. So please provide valid inputs for program execution.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.