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

Write a class called Car that contains instance data that represents the make, m

ID: 3813188 • Letter: W

Question

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

Explanation / Answer


package chegg.april;

import java.util.Scanner;

/**
*
* @author Sam
*/
public class CarTest {
    private static Car c1; //create three car objects as mentioned
    private static Car c2;
    private static Car c3;
    public static void main(String[] args) {
        String tempMake, tempModel;
        int tempYear;
        Scanner sc = new Scanner(System.in);
        c1 = new Car("Ford", "Taurus", 2015); //initialize them with parameterised constructor
        c2 = new Car("Honda", "Accord", 2007); //initialize them with parameterised constructor
        c3 = new Car(); //initialize them with default constructor
        System.out.println("Here is the information I have on Mr. Smith's three cars:");
        System.out.println(c1); //this will automatically call toString() method
        System.out.println(c2);
        System.out.println(c3);
      
      
        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.print("Make: ");
        c3.setMake(sc.next()); //use setMake method;
        System.out.print("Model: ");
        c3.setModel(sc.next());//use setModel method;
        System.out.print("Year: ");
        c3.setYear(sc.nextInt());
      
        System.out.println("Mr. Smith traded the Taurus for another Ford from the same year.");
        System.out.print("Enter the new Ford model: ");
        c1.setModel(sc.next());
        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: ");
        c2.setYear(sc.nextInt());
       /* remaining part is as exactly mentioned in question, no tweak required1 */
        System.out.println("Here is the updated information I now have on Mr. Smith's three cars:");
        System.out.println(c1);
        System.out.println(c2);
        System.out.println(c3);
    }
}

class Car {
    private String make;
    private String model;
    private int year;

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

    public Car() { //default constructor
        make = "--";
        model = "--";
        year = 0;
    }

    // Setters and getters below

    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;
    }

    //toString() method

    @Override
    public String toString() {
        return year +" " + make + " " + model;
    }
  
}

I made the code very simple. let me know if need any doubt of yours cleared. If you have any doubt, pls feel free to comment below.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote