Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create and modify the following java code in the following ways: 1) Additions to

ID: 3759001 • Letter: C

Question

Create and modify the following java code in the following ways:

1) Additions to the Automobile Class

i) Instance variable : String colorOfAuto - represents the color of the Automobile

ii) Modify the constructors to include setting the new colorOfAuto instance variable.

iii) Add accessor and mutator method for the colorOfAuto instance variable.

iv) ) New Methods

i) 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"

ii) 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)

i) 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.

ii) 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 colorOfAuto;

public Automobile()
{
make = "";
model = "";
price = 0;
}

public Automobile(String aMake, String aModel, double aPrice, String colorOfAutoSet)
{
make = aMake;
model = aModel;
price = aPrice;
colorOfAuto=colorOfAutoSet;
}
public String getcolorOfAuto()
{
return colorOfAuto;
}

public void setcolorOfAuto(String colorOfAutoSet)
{
colorOfAuto = colorOfAutoSet;
}


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 String toString()
{
return "Make : "+make+" Model: "+model+" Proce: "+price+" Color of Auto"+colorOfAuto;
}


boolean equals( Automobile otherAuto)
{
private String makeOtherAuto=otherAuto.getMake();
private String modelOtherAuto=otherAuto.getModel();
private double priceOtherAuto=otherAuto.getPrice();
private String colorOfAutoOther=otherAuto.getcolorOfAuto();

if(makeOtherAuto.equals(make))
if(modelOtherAuto.equals(model))
  if(priceOtherAuto.equals(price))
   if(colorOfAutoOther.equals(colorOfAuto))
    return true;
else return false;
   
}
public class AutomobileTest
{

public static void main(String[] args)
{
     //two constructors
Automobile myCar = new Automobile();
Automobile myTruck = new Automobile("Nissan", "Titan", 23974.83,"red");
   
     //using mutators
myCar.setMake("Chevy");
myCar.setModel("Corvett");
myCar.setcolorOfAuto("red");
myCar.setPrice(89382.22);
   

     //using mutators
myTruck.setMake("ChevyACV");
myTruck.setModel("CorvettER");
myTruck.setcolorOfAuto("blue");
myTruck.setPrice(234434.22);

boolean test=myCar.equals(myTruck);
if(test)
System.out.println("Both are equal");

System.out.println(myCar.toString());

//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());
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote