Please write in java complier I am using is eclispe Write a

Please write in java complier I am using is eclispe

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.

Example Output:

CarTest Java Application] C:\\Program 2007 Ford Taurus 2011 Honda Odyssey 1999 Toyota Corolla 1966 Ford Mustang 2007 Ford Explorer 2010 Honda Odyssey 1999 Nissan Maxima 1966 Ford Mustang

Solution

Car.java

import java.util.Calendar;

public class Car {
//Declaring private variables
private String make;
private String model;
private int year;

//Parameterized constructor
public Car(String make, String model, int year) {
   super();
   this.make = make;
   this.model = model;
   this.year = year;
}

//Setters and getters
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;
}

/* This metthod will check whether the car is Antique or not
* Params : void
* Return : boolean value
*/
public boolean isAntique()
{
   Calendar c=Calendar.getInstance();
   // Getting the current year
   if(c.get(Calendar.YEAR)-getYear()>45)
   return true;
   else
   return false;
}

//toString() method will display the contents of an Object inside it
@Override
public String toString() {
   return getYear()+\" \"+getMake()+\" \"+getModel();
}

}

_____________________________________

CarTest.java

public class CarTest {

   public static void main(String[] args) {
      
       //Declaring boolean variable
       boolean bool;
      
       //Creating a Car Array
       Car cararr[]={new Car(\"Ford\",\"Taurus\", 2007),
               new Car(\"Honda\",\"Odyssey\",2011),
               new Car(\"Toyota\",\"Corolla\",1999),
               new Car(\"Foed\",\"Mustang\",1966),
               new Car(\"Ford\",\"Explorer\",2007),
               new Car(\"Honda\",\"Odyssey\",2010),
               new Car(\"Nissan\",\"Maxima\",1999),
               new Car(\"Ford\",\"Mustang\",1966)};
      
       //Displaying the contents of Car Object
       for(int i=0;i<cararr.length;i++)
       {
           System.out.println(cararr[i].toString());
          
           //Checking whether the car is Antique or not by calling the isAntique()
           bool=cararr[i].isAntique();
          
           //If the method return value is true then display the message in the if block
           if(bool==true)
               System.out.println(\"::This is Antique car ::\ \");
           else
           //If the method return value is else then display the message in the if-else block  
           System.out.println(\"::This is not Antique car ::\ \");
       }

   }

}

______________________________________

output:

2007 Ford Taurus
::This is not Antique car ::

2011 Honda Odyssey
::This is not Antique car ::

1999 Toyota Corolla
::This is not Antique car ::

1966 Foed Mustang
::This is Antique car ::

2007 Ford Explorer
::This is not Antique car ::

2010 Honda Odyssey
::This is not Antique car ::

1999 Nissan Maxima
::This is not Antique car ::

1966 Ford Mustang
::This is Antique car ::

___________Thank You

Please write in java complier I am using is eclispe Write a class called Car that contains instance data that represents the make, model, and year of the car. D
Please write in java complier I am using is eclispe Write a class called Car that contains instance data that represents the make, model, and year of the car. D
Please write in java complier I am using is eclispe Write a class called Car that contains instance data that represents the make, model, and year of the car. D

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site