9.2k views
4 votes
What is output when the CarTest application is run? Why?

public class Car {



private String color;

private int numWheels;



public Car(String color, int numWheels) {

this.color = color;

this.numWheels = numWheels;

}



public String getColor() {

return color;

}



public void setColor(String color) {

this.color = color;

}



public int getNumWheels() {

return numWheels;

}



public void setNumWheels(int numWheels) {

this.numWheels = numWheels;

}

}

public class CarTest {

public static void main(String[] argvs) {



CarTest carTest = new CarTest();

carTest.runDemo();

}



public void runDemo() {

Car c = new Car("blue", 4);

changeColor(c, "red");

System.out.println(c.getColor());

}



public void changeColor(Car car, String newColor) {

car.setColor(newColor);

}

}

What is output when the CarTest application is run? Why?

User Elric
by
4.9k points

1 Answer

2 votes

Answer:

red

Step-by-step explanation:

public class CarTest {

public static void main(String[] argvs) {

//below line will create an object of CarTest class Object

CarTest carTest = new CarTest();

//This will call runDemo method

carTest.runDemo();

}

public void runDemo() {

//Below line will create an object of Car class with color blue and 4 wheel

Car c = new Car("blue", 4);

//Bellow Line will change the color from blue to red, see the logic writteen in chnageColor method definition

changeColor(c, "red");

//Below line will print the color as red

System.out.println(c.getColor());

}

public void changeColor(Car car, String newColor) {

//This line will set the color as passed color in the car object

car.setColor(newColor);

}

}

User Marialisa
by
5.1k points