To accomplish these objectives, it is required theimplementation and the utiliza
ID: 3614768 • Letter: T
Question
To accomplish these objectives, it is required theimplementation and the utilization of a class Carand of a class CarTest.
Project Specification
2.1 Class Car specification
The following self describing instance variables should bedefined for the class Car:
make (of type String)
model (of type String)
year (of type integer)
price (of type integer)
The class Car should define the followingmethods:
four get type methods (getMake, getModel,getYear, getPrice), each returning the value ofthe corresponding instance variable.
setPrice for setting a new price for the car. The newprice is specified as method parameter.
print for displaying car's specific data. When theprint method is invoked, the make, model, yearand price values should be displayed on the screen, eachon a separate line.
The class Car also defines a defaultconstructor and a constructor with parameters.
The default constructor builds Carobjects with the following values of their instance variables:"Ford" for make, "Mondeo" formodel, 2009 for year and 19,900dollars for price.
The constructor with parameters builds and initializesa car object based on constructor parameters.
Explanation / Answer
publicclass Car { // instancefields privateString make; privateString model; privateint year; privateint price; //constructors public Car() { this("Ford","Mondeo", 2009, 19900); } publicCar(String make, String model, intyear, int price) { this.make = make; this.model =model; this.year = year; this.price =price; } // accessors publicString getMake() { return make; } publicString getModel() { return model; } publicint getYear() { return year; } publicint getPrice() { return price; } // print publicvoid print() { System.out.println(toString()); } publicString toString() { return getMake()+" "+getModel()+" "+getYear()+" "+getPrice(); } // modifiers publicvoid setPrice(int price) { this.price =price; } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.