1. This prgram willl consist of a simple class and a tester class. 2. Implement
ID: 3674433 • Letter: 1
Question
1. This prgram willl consist of a simple class and a tester class.
2. Implement a class Automobile (Automobile.java). An Automobile will have three instance variables: a make (a String, for example: "Ford" or "Toyota"), a model(a String, for example: “Focus” or “F250”) and a price (a double, for example: 22999.00 or 24894.84). The class must include a:
a. Default constructor.
b. Parameterized constructor with three parameters (make, model and price).
c. Accessor method called getMake( ) to return the make.
d. Accessor method called getModel( ) to return the model.
e. Accessor method called getPrice( ) to return the price.
f. Mutator method setMake( String newMake) to set the make.
g. Mutator method setModel( String newModel) to set the model.
h. Mutator method setPrice( double newPrice ) to set the price.
3. Implement a demo or tester program (AutomobileTest.java) to test out all the constructors and methods of your class as follows:
a. Create at least two Automobile objects, one using the default constructor and one using the parameterized constructor.
b. Use the mutator methods to set the make, model, and price for the automobile object that was created using the default constructor.
c. Use the accessor methods to get information that is then printed in sentences containing the make , model , and price of each auto. An example sentence might be, "The Toyota Tundra costs $24869.99."
Explanation / Answer
/**Class Automobile that represents the
* object of Automobile class . The class has methods
* to set make,model and price.
* */
//Automobile.java
public class Automobile
{
//private members of class
private String make;
private String model;
private double price;
//a.default constructor of class
public Automobile()
{
make="";
model="";
price=0;
}
//b.parameterized constructor
public Automobile(String make, String model, double price)
{
this.make=make;
this.model=model;
this.price=price;
}
//Accessor method called getMake( ) to return the make.
public String getMake()
{
return make;
}
//d. Accessor method called getModel( ) to return the model.
public String getModel()
{
return model;
}
//e. Accessor method called getPrice( ) to return the price.
public double getPrice()
{
return price;
}
//f.Mutator method setMake( String newMake) to set the make.
public void setMake(String newMake)
{
make=newMake;
}
//g. Mutator method setModel( String newModel) to set the model.
public void setModel(String newModel)
{
make=newModel;
}
// h. Mutator method setPrice( double newPrice ) to set the price.
public void setPrice(double newPrice)
{
price=newPrice;
}
}
------------------------------------------------------------------------------------------------------------------------------------------------
/**The AutomobileTest that tests the Automobile class
* by calling the setter and getter methods and prints
* the results*/
//AutomobileTest.java
public class AutomobileTest
{
public static void main(String[] args)
{
//3a.Create two objects of AutoMobile class
//one is default consturctor and other is parameterized
//constructor
Automobile defaultAuto=new Automobile();
//parameterized construtor
Automobile parameterAuto=new Automobile("Ford", "Focus",22999.00);
//3b.use setter methods for defaultAuto object
//to set make , model and price
defaultAuto.setMake("Toyota");
defaultAuto.setModel("F250");
defaultAuto.setPrice(24894.84);
//3c.use getter methods for parameterAuto object
//to get make , model and price
System.out.println("The "+defaultAuto.getMake()+" "
+defaultAuto.getModel()
+" costs $"+defaultAuto.getPrice());
System.out.println("The "+parameterAuto.getMake()+" "
+parameterAuto.getModel()
+" costs $"+parameterAuto.getPrice());
}
}
------------------------------------------------------------------------------------------------------------------------------------------------
Sample output:
The F250 costs $24894.84
The Ford Focus costs $22999.0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.