Using the following java Car class listed below, what would be the output produc
ID: 3572108 • Letter: U
Question
Using the following java Car class listed below, what would be the output produced by the following java statements:
Car car1 = new Car(2006, "Toyota");
Car car2 = new Car(2007, "Toyota", "Avalon");
Car car3 = new Car();
System.out.println(car1.toString());
System.out.println(car2.toString());
System.out.println(car3.toString());
public class Car {
private int year; //Instance variable to store the car year
private String make; //Instance variable to store the car make
private String model; //Instance variable to store the car model
Car() {
year = 2008; //default year value
make = "Toyota"; //default make value
model = "4Runner"; //default year value
}
Car(int yr, String pMake) {
year = yr; //Sets car year equal to the value of yr
make = pMake; //Sets car make equal to the value of pMake
model = "Camry"; //Sets car model to Camry
}
Car(int yr, String pMake, String pModel) {
year = yr; //Sets car year equal to the value of yr
make = pMake; //Sets car make equal to the value of pMake
model = pModel; //Sets car model equal to the value of pModel
}
public String toString() {
StringBuffer buf = new StringBuffer();
buf.append("Year: " + year + " ");
buf.append("Make: " + make + " ");
buf.append("Model: " + model);
return buf.toString();
}
}
Explanation / Answer
The output would be produced by the java statements :-
Year: 2006; Make: Toyota; Model:Camry;
Year: 2007; Make: Toyota; Model:Avalon;
Year: 2008; Make: Toyota; Model:4Runner;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.