Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Class Exercise Write a class called Car that contains instance data that represe

ID: 3814472 • Letter: C

Question

Class Exercise

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

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

Here is some sample output: (Note: your output does not have to follow the same narrative. You can be as creative as you want in order to test the class and objects.)

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

FIX THIS CODE PLEASE

import java.util.Scanner;

public class Test {

   public static void main(String[] args) {
       //Declaring local variables
       int year;
       String make,model;
      
       //creating the car class obejcts
       Car car1=new Car("Ford","Taurus",2015);
       Car car2=new Car("Honda","Accord",2007);
   Car car3=new Car();
     
   //Displaying car's info
   System.out.println("Displaying the 3 Car's Info :");
System.out.println(car1.toString());
System.out.println(car2.toString());
System.out.println(car3.toString());
   //Scanner object is used to get the inputs entered by the user
          Scanner sc=new Scanner(System.in);
         
          //Getting the user entered info
          System.out.println("Enter the information for the third car here:");
System.out.print("Make :");
make=sc.next();
System.out.print("Model :");
model=sc.next();
System.out.print("Year :");
year=sc.nextInt();
  
//Setting the car info on the 3rd Car
car3.setMake(make);
car3.setModel(model);
car3.setYear(year);
System.out.println(" ");
  
  
//Dispalying the car's info
System.out.println("Displaying the 3 Car's Info After updating 3'rd Car info:");
System.out.println(car1.toString());
System.out.println(car2.toString());
System.out.println(car3.toString());
System.out.println("Mr. Smith traded the Taurus for another Ford from the same year.");
System.out.print("Enter the new Ford model:");
model=sc.next();
car1.setModel(model);
System.out.println("Mr. Smith sold the 2007 Accord to buy a more current version.");
System.out.print("Enter the new year for the Accord:");
year=sc.nextInt();
car2.setYear(year);
System.out.println(" Here is the updated information");
System.out.println(car1.toString());
System.out.println(car2.toString());
System.out.println(car3.toString());
  
  
   }

}

Explanation / Answer

Test.java


import java.util.Scanner;
public class Test {
public static void main(String[] args) {
//Declaring local variables
int year;
String make,model;
  
//creating the car class obejcts
Car car1=new Car("Ford","Taurus",2015);
Car car2=new Car("Honda","Accord",2007);
Car car3=new Car();

//Displaying car's info
System.out.println("Displaying the 3 Car's Info :");
System.out.println(car1.toString());
System.out.println(car2.toString());
System.out.println(car3.toString());
//Scanner object is used to get the inputs entered by the user
Scanner sc=new Scanner(System.in);

//Getting the user entered info
System.out.println("Enter the information for the third car here:");
System.out.print("Make :");
make=sc.next();
System.out.print("Model :");
model=sc.next();
System.out.print("Year :");
year=sc.nextInt();
  
//Setting the car info on the 3rd Car
car3.setMake(make);
car3.setModel(model);
car3.setYear(year);
System.out.println(" ");
  
  
//Dispalying the car's info
System.out.println("Displaying the 3 Car's Info After updating 3'rd Car info:");
System.out.println(car1.toString());
System.out.println(car2.toString());
System.out.println(car3.toString());
System.out.println("Mr. Smith traded the Taurus for another Ford from the same year.");
System.out.print("Enter the new Ford model:");
model=sc.next();
car1.setModel(model);
System.out.println("Mr. Smith sold the 2007 Accord to buy a more current version.");
System.out.print("Enter the new year for the Accord:");
year=sc.nextInt();
car2.setYear(year);
System.out.println(" Here is the updated information");
System.out.println(car1.toString());
System.out.println(car2.toString());
System.out.println(car3.toString());
  
  
}
}

Car.java


public class Car {
   private String make, model;
   private int year ;
   public Car(String make, String model, int year){
       this.make=make;
       this.model= model;
       this.year = year;
   }
   public Car() {
       make="-";
       model="-";
       year=0;
   }
   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;
   }
   public String toString() {
       return "Make: "+getMake()+" Model: "+getModel()+" Year: "+getYear();
   }
}

Output:

Displaying the 3 Car's Info :
Make: Ford Model: Taurus Year: 2015
Make: Honda Model: Accord Year: 2007
Make: - Model: - Year: 0
Enter the information for the third car here:
Make :Hyundai
Model :Elantra
Year :2014


Displaying the 3 Car's Info After updating 3'rd Car info:
Make: Ford Model: Taurus Year: 2015
Make: Honda Model: Accord Year: 2007
Make: Hyundai Model: Elantra Year: 2014
Mr. Smith traded the Taurus for another Ford from the same year.
Enter the new Ford model:Ford
Mr. Smith sold the 2007 Accord to buy a more current version.
Enter the new year for the Accord:2015

Here is the updated information
Make: Ford Model: Ford Year: 2015
Make: Honda Model: Accord Year: 2015
Make: Hyundai Model: Elantra Year: 2014