This workshop is an extension of Workshop 8 so use Workshop 8 as a starting poin
ID: 3758751 • Letter: T
Question
This workshop is an extension of Workshop 8 so use Workshop 8 as a starting point. You can either use your Workshop 8 code or download the Workshop 8 solution.
Additions to the Automobile Class
Instance variable : String colorOfAuto - represents the color of the Automobile
Modify the constructors to include setting the new colorOfAuto instance variable.
Add accessor and mutator method for the colorOfAuto instance variable.
New Methods
String toString() method - returns a String that represents the instance variables of the Automobile class in a sentence. Use the accessor method to get the instance variables. An example of the String might be "The Red Chevrolet Camaro costs $54982.62"
boolean equals( Automobile otherAuto) - that compares the two Automobile object based on make, model and color and returns true if all those instance variables are the same and false if any are different.
Overloaded void setColor(int colorCode)
an overloaded setColor mutator method that converts parameter colorCode to a String representing the a color and assigns that String to the instance variable colorOfAuto.
The conversion of colorCode to color is: 1 - Red, 2 - Blue, 3 - White, 4 - Black. If the colorCode is not 1 - 4, print an error message stating that the color code is invalid.
Add to the AutomobileTest.java as follows:
Modify the parameterized constructor call to include color.
Use the mutator method, to pass a String representing the color, to set the color for the automobile object that was created using the default constructor.
Test the equals method. in testing this method, use an if statement where the condition is the call to the equals method.
Test the setColor method by changing the color of an Automobile object.
Call the toString method as needed to show your tests worked, for example after you call change the color using one of the mutator methods call the toString to show that the color has changed.
Modify this code:
public class Automobile
{
private String make;
private String model;
private double price;
public Automobile()
{
make = "";
model = "";
price = 0;
}
public Automobile(String aMake, String aModel, double aPrice)
{
make = aMake;
model = aModel;
price = aPrice;
}
public String getMake()
{
return make;
}
public void setMake(String aMake)
{
make = aMake;
}
public String getModel()
{
return model;
}
public void setModel(String aModel)
{
model = aModel;
}
public double getPrice()
public class AutomobileTest
{
public static void main(String[] args)
{
//two constructors
Automobile myCar = new Automobile();
Automobile myTruck = new Automobile("Nissan", "Titan", 23974.83);
//using mutators
myCar.setMake("Chevy");
myCar.setModel("Corvett");
myCar.setPrice(89382.22);
//using accessors
String myMake = myCar.getMake();
String myModel = myCar.getModel();
double myPrice = myCar.getPrice();
System.out.println("The " + myMake + " " + myModel + " costs " + myPrice);
System.out.println("The " + myTruck.getMake() + " " + myTruck.getModel() + " costs " + myTruck.getPrice());
}
Explanation / Answer
public class Automobile
{
private String make;
private String model;
private double price;
private String color;
public Automobile()
{
make = "";
model = "";
price = 0;
color=””
}
public Automobile(String aMake, String aModel, double aPrice,String aColor)
{
make = aMake;
model = aModel;
price = aPrice;
color=aColor;
}
public String getMake()
{
return make;
}
public void setMake(String aMake)
{
make = aMake;
}
public String getModel()
{
return model;
}
public void setModel(String aModel)
{
model = aModel;
}
public double getPrice()
public String getColor()
{
return color;
}
public void setColor(String aColor)
{
color = aColor;
}
public class AutomobileTest
{
public static void main(String[] args)
{
//two constructors
Automobile myCar = new Automobile();
Automobile myTruck = new Automobile("Nissan", "Titan", 23974.83,”Grey”);
//using mutators
myCar.setMake("Chevy");
myCar.setModel("Corvett");
myCar.setPrice(89382.22);
myCar.setColor(“Blue”)
//using accessors
String myMake = myCar.getMake();
String myModel = myCar.getModel();
double myPrice = myCar.getPrice();
String myColor = myCar.getColor();
System.out.println("The " + myMake + " " + myModel + " costs " + myPrice + “and color” + myColor);
System.out.println("The " + myTruck.getMake() + " " + myTruck.getModel() + " costs " + myTruck.getPrice() +”color” + myTruck.getColor());
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.