Question: public class Game { private int score = 0; // LINE A // No constructors defined public void setScore(int score) { score = score; //
public class Game { private int score = 0; // LINE A // No constructors defined public void setScore(int score) { score = score; // LINE B } public int getScore() { return score; } public static void main(String[] args) { Game g = new Game(); g.setScore(100); System.out.println("Score is " + g.getScore()); // LINE C } } This code has an error. When this code is run, the statement "Score is 0" is printed, even though the setScore method was called on the object g with the argument 100. Which of the following best describes why this error occurs?
Because the score member variable is private, it cannot be modified from the main method.
Because Java interprets score as the parameter score and not the member variable score in the body of setScore.
Because getScore() is used to access the score in main when it should be accessed directly using g.score.
Based on your answer to the last problem, write a line of code that should replace either LINE A, LINE B, or LINE C in the code above in order to fix the error. You do not need to specify which line you are replacing. It will be obvious from your answer. Just provide the complete line of code.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
