In the Toy class below, the raisePrice method is intended to increase the value of the instance variable price by the value of the parameter surcharge. The method does not work as intended. AP Testo quiz11 public class Toy private String name; private double price; public Toy (String n, double p) name = n; price = p; } public void raisePrice (double surcharge) // Line 12 return price + surcharge; // Line 14
Which of the following changes should be made so that the class definition compiles without error and the method raise Price works as intended?
A. Replace line 14 with surcharge += price;.
B. Replace line 14 with price += surcharge;.
C. Replace line 14 with return price += surcharge;.

Respuesta :

Answer:

B

Explanation:

You are increasing the value of price, therefore, you should have an assignment statement in the code of the class to change the value of price.  The method is also of type void, so you shouldn't be returning a value.  Since you will be increasing the value of price with the value of surcharge, your answer is then:

    price+= surcharge;  

which is also the same as saying:  price = price + surcharge;

Otras preguntas

ACCESS MORE
EDU ACCESS
Universidad de Mexico