can you please help me with this, please follow all the given instructions and t
ID: 3811113 • Letter: C
Question
can you please help me with this, please follow all the given instructions and try to not devaite from them. consturct a program for a begginner java coder.
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:
1. Three private members to store data for the make model and year
2. Two constructors:
1.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).
2. The second constructor should accept three values for the members during instantiation.
3. Accessors and Mutators for each private member of the class
4. A toString method to output the information on each car
In the CarTest driver, you will need:
1. At three Car objects:
1.Two of the three objects can be initialized with hardcoded data
2.One should be initialized with the constructor that accepts no parameters
2. A Scanner Object to read get information on the third car
3. Local variables to store information on the third car (and pass it on to the object’s private members)
4. To test the accessors and mutators you made:
1.You must use at least one accessor method from the first two Car objects
2.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
Car.java
public class Car {
// Declaring variables
private String make;
private String model;
private int year;
// Zero argumented constructor
public Car() {
super();
this.make = "-";
this.model = "-";
this.year = 0;
}
// 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;
}
// toString method is used to display the contents of an object inside it
@Override
public String toString() {
return year + " " + make + " " + model;
}
}
____________________
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());
}
}
______________________
Output:
Displaying the 3 Car's Info :
2015 Ford Taurus
2007 Honda Accord
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:
2015 Ford Taurus
2007 Honda Accord
2014 Hyundai Elantra
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
2015 Ford Explorer
2017 Honda Accord
2014 Hyundai Elantra
_____________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.