Write the complete code for the class Car as given in the class diagram below. Y
ID: 3675710 • Letter: W
Question
Write the complete code for the class Car as given in the class diagram below. You
will chain the constructors so be sure not
to duplicate code in the constructors. For
example, the constructors with 3 arguments should call the one with 1 argument to
set the value for cylinder.
Use the provided file
Car
Tester.java
to test your class.
Car
-
numberC
ylinders:int
-
manufacturer:String
-
model:String
-
modelYear: int
+ Car()
+ Car(String manufacturer, String model
,
int cylinders
)
+ Car(String manufacturer, String model,
int cylinders,
int modelYear
)
+ get
NumberC
ylinders
():int
+ setNumberC
ylinders
(int
numberC
ylinders
): void
+ getManufacturer():String
+ setManufacturer(String manufacturer):void
+ getModel():String
+ setModel(String model):void
+ get
ModelYear
():
int
+ set
ModelYear
(
int modelYear
):void
+
toString(): String
Note: toString
method should display values of instance fields with proper
labels
Explanation / Answer
Tester.java
class Car {//car class
int Cylinders;
String manufacturer;
String model;
int modelYear;
public int getCylinders() {//setter ad getter methods
return Cylinders;
}
public void setCylinders(int Cylinders) {
this.Cylinders = Cylinders;
}
public String getManufacturer() {
return manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public int getModelYear() {
return modelYear;
}
public void setModelYear(int modelYear) {
this.modelYear = modelYear;
}
Car() {//constructor with no arguments
Cylinders = 10;
manufacturer = "sonat";
model = "A123";
modelYear = 2013;
System.out.println("Cylinders =" + Cylinders + " manufactures=" + manufacturer + " model=" + model + " modelyear=" + modelYear);
}
Car(String manufacturer, String model, int cylinders) {//constructor with arguments
System.out.println("Cylinders =" + cylinders + " manufactures=" + manufacturer + " model=" + model);
}
Car(String manufacturer, String model, int cylinders, int modelYear) {//constructor with arguments
System.out.println( "manufactures= " + manufacturer + " model=" + model + " modelear=" + modelYear);
}
}
public class Tester {//driver program
public static void main(String args[]) {
Car C1 = new Car();//constructor objects
Car C2 = new Car("IBM", "23M", 56);//constructor objects
Car C3 = new Car("sonata", "a1234", 16,2000);//constructor objects
}
}
output
Cylinders =10 manufactures=sonat model=A123 modelyear=2013
Cylinders =56 manufactures=IBM model=23M
manufactures= sonata model=a1234 modelear=2000
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.