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

Additions to the Automobile Class Instance variable : String colorOfAuto - repre

ID: 3758923 • Letter: A

Question

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.

Explanation / Answer

CODE :

public class Car {

     

    private String name;

    private String color;

     

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    public String getColor() {

        return color;

    }

    public void setColor(String color) {

        this.color = color;

    }  

}

public class Main {

    public static void main(String[] args) {

         

        Car bmw = new Car();

        bmw.setName("BMW");

        bmw.setColor("White");

         

        Car ferrari = new Car();

        ferrari.setName("Ferrari");

        ferrari.setColor("Red");

         

        //Swapping object

        swappingObject(bmw, ferrari);

         

        //Check is contains still same object

        System.out.println("BMW : "+bmw.getColor());

        System.out.println("Ferrari : "+ferrari.getColor());

         

    }

private static void swappingObject(Car carBMW,Car carFerrari){

        Car carTemp = carBMW;

        carBMW = carFerrari;

        carFerrari = carTemp;

    }

     

}

public class Car {

     

    private String name;

    private String color;

     

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    public String getColor() {

        return color;

    }

    public void setColor(String color) {

        this.color = color;

    }  

}

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