Follow exactly all instructions given below: Write a class called Car that conta
ID: 3812147 • Letter: F
Question
Follow exactly all instructions given below:
Write a class called Car that contains instance data that represents the make, model, and year of the car. Define constructors to initialize these values. Include accessor(getter) and mutator(setter) methods for all instance data, and a toString method that returns the data of the car. Create a driver class called CarTest, whose main method instantiates and updates several Car objects.
In the Car Class, you will need:
Three private members to store data for the make model and year
Two constructors:
The first constructor should not accept any values during instantiation (This constructor can initialize the members to an ‘empty string or dash - i.e. “ - “ - or 0).
The second constructor should accept three values for the members during instantiation.
Accessors and Mutators for each private member of the class
A toString method to output the information on each car
Comments where necessary
In the CarTest driver, you will need:
At three Car objects:
Two of the three objects can be initialized with hardcoded data
One should be initialized with the constructor that accepts no parameters
A Scanner Object to read get information on the third car
Local variables to store information on the third car (and pass it on to the object’s private members)
To test the accessors and mutators you made:
You must use at least one accessor method from the first two Car objects
You must use the mutator to update at least one characteristic from the first two Car objects
Comments where necessary
Sample Output:
Here is the information I have on Mr. Smith's three cars:
2015 Ford Taurus
2007 Honda Accord
0 - -
Whoops! It seems we do not have the info on one of his cars!
Enter the information for the third car here:
Make: Hyundai
Model: Elantra
Year: 2014
Mr. Smith traded the Taurus for another Ford from the same year.
Enter the new Ford model: Explorer
Mr. Smith sold the 2007 Accord to buy a more current version.
Enter the new year for the Accord: 2017
Here is the updated information I now have on Mr. Smith's three cars:
2015 Ford Explorer
2017 Honda Accord
2014 Hyundai Elantra
Explanation / Answer
import java.util.*;
import java.lang.*;
import java.io.*;
class Car {
private String make;
private String model;
private int year;
public Car () //constructor1
{
make = "-";
model="-";
year = 0;
}
public Car(String _make,String _model,int _year){ //constructor2
make=_make;
model=_model;
year=_year;
}
//accessors
String getMake(){
return make;
}
String getModel(){
return model;
}
int getYear(){
return year;
}
//muitators
void setMake(String _make){
make=_make;
}
void setModel(String _model){
model=_model;
}
void setYear(int _year){
year=_year;
}
public String to_String(){
return this.getYear()+" "+this.getMake()+" "+this.getModel();
}
}class CarTester{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int year,nyear;
String make,model,nmodel;//variables
Car carObj1=new Car("Ford","Taurus",2015);
Car carObj2=new Car("Honda","Accord",2007);//2007 Honda Accord
Car carObj3=new Car();//no params
System.out.println("Here is the information I have on Mr. Smith's three cars:");
System.out.println(carObj1.to_String());
System.out.println(carObj2.to_String());
System.out.println(carObj3.to_String());
System.out.println("Whoops! It seems we do not have the info on one of his cars! Enter the information for the third car here:");
System.out.println("Make:");
make=sc.nextLine();
carObj3.setMake(make);//setter
System.out.println("Model:");
model=sc.nextLine();
carObj3.setModel(model);
System.out.println("Year:");
year=Integer.parseInt(sc.nextLine());
carObj3.setYear(year);
System.out.println("Mr. Smith traded the "+carObj1.getModel()+" for another "+carObj1.getMake()+" from the same year. Enter the new Ford model: ");
nmodel=sc.nextLine();
carObj1.setModel(nmodel);
System.out.println("Mr. Smith sold the "+carObj2.getYear()+" "+carObj2.getModel()+" to buy a more current version. Enter the new year for the Accord: ");
nyear=Integer.parseInt(sc.nextLine());
carObj2.setYear(nyear);
System.out.println("Here is the updated information I now have on Mr. Smith's three cars:");
System.out.println(carObj1.to_String());
System.out.println(carObj2.to_String());
System.out.println(carObj3.to_String());
}
}