Add the following to automobile class, java code found at the bottom. 1. Additio
ID: 3681014 • Letter: A
Question
Add the following to automobile class, java code found at the bottom.
1. Additions to the Automobile Class
a. Instance variable : String colorOfAuto - represents the color of the Automobile
b. Modify the constructors to include setting the new colorOfAuto instance variable.
c. Add accessor and mutator method for the colorOfAuto instance variable.
d. New Methods
1. 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"
2. 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.
3. Overloaded void setColor(int colorCode)
a. 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.
b. 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.
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()
{
return price;
}
public void setPrice(double aPrice)
{
price = aPrice;
}
}
Explanation / Answer
Hi, Please find the updated programs as per requirement.
Automobile.java
public class Automobile
{
private String make;
private String model;
private double price;
private String colorOfAuto;
public Automobile()
{
make = "";
model = "";
price = 0;
}
public Automobile(String aMake, String aModel, double aPrice, String acolorOfAuto)
{
make = aMake;
model = aModel;
price = aPrice;
colorOfAuto = acolorOfAuto;
}
public String getMake()
{
return make;
}
public String getColorOfAuto() {
return colorOfAuto;
}
public void setColorOfAuto(String colorOfAuto) {
this.colorOfAuto = colorOfAuto;
}
public void setMake(String aMake)
{
make = aMake;
}
public String getModel()
{
return model;
}
public void setModel(String aModel)
{
model = aModel;
}
public double getPrice()
{
return price;
}
public void setPrice(double aPrice)
{
price = aPrice;
}
public String toString(){
return "The "+getColorOfAuto()+" "+getMake()+" "+getModel()+ " costs $"+ getPrice();
}
public boolean equals(Automobile otherAuto){
boolean flag;
if(this.getMake().equals(otherAuto.getMake())
&& this.getModel().equals(otherAuto.getModel())
&& this.getColorOfAuto().equals(otherAuto.getColorOfAuto()))
flag = true;
else
flag = false;
return flag;
}
public void setColor(int colorCode){
if(colorCode == 1){
colorOfAuto = "Red";
}
else if(colorCode == 2){
colorOfAuto = "Blue";
}
else if(colorCode == 3){
colorOfAuto = "White";
}
else if(colorCode == 4){
colorOfAuto = "Black";
}
else {
colorOfAuto = "Invalid";
}
}
}
Test2.java
public class Test2 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
java.util.Scanner in = new java.util.Scanner(System.in);
System.out.println("Please Enter 1st Make: ");
String make1 = in.next();
System.out.println("Please Enter 1st Model: ");
String model1 = in.next();
System.out.println("Please Enter 1st Price: ");
double price1 = in.nextDouble();
System.out.println("Please Enter 1st Color Code from 1 to 4: ");
int colorCode1 = in.nextInt();
Automobile au1 = new Automobile(make1, model1, price1, "");
au1.setColor(colorCode1);
System.out.println("------------------------------------------");
System.out.println(au1.toString());
System.out.println("------------------------------------------");
System.out.println("Please Enter 2nd Make: ");
String make2 = in.next();
System.out.println("Please Enter 2nd Model: ");
String model2 = in.next();
System.out.println("Please Enter 2nd Price: ");
double price2 = in.nextDouble();
System.out.println("Please Enter 2nd Color Code from 1 to 4: ");
int colorCode2 = in.nextInt();
Automobile au2 = new Automobile(make2, model2, price2, "");
au2.setColor(colorCode2);
System.out.println("----------------------------------------");
System.out.println(au2.toString());
System.out.println("----------------------------------------");
if(au1.equals(au2)){
System.out.println("Two Objects Same");
}
else{
System.out.println("Two Objects not same");
}
}
}
Output:
Please Enter 1st Make:
Chevrolet
Please Enter 1st Model:
Camaro
Please Enter 1st Price:
54982.62
Please Enter 1st Color Code from 1 to 4:
1
------------------------------------------
The Red Chevrolet Camaro costs $54982.62
------------------------------------------
Please Enter 2nd Make:
Toyoto
Please Enter 2nd Model:
Camaro
Please Enter 2nd Price:
65000.34
Please Enter 2nd Color Code from 1 to 4:
2
----------------------------------------
The Blue Toyoto Camaro costs $65000.34
----------------------------------------
Two Objects not same
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.