asked 157k views
0 votes
Consider the following class definition, which represents two scores using the instance variables score1 and score2. The methodreset is intended to set to 0 any score that is less than threshold. The method does not work as intended.

public class TestClass
{
private int score1;
private int score2;
public TestClass(int num1, int num2)
{
score1 = num1;
score2 = num2;
}
public void reset(int threshold)
{
if (score1 < threshold) // line 14
{
score1 = 0; // line 16
}
else if (score2 < threshold) // line 18
{
score2 = 0;
}
}
}
Which of the following changes can be made so that the reset method works as intended?
A. In lines 14 and 18, change < to >.
B. In lines 14 and 18, change < to <=.
C. In line 16, change score1 to num1 and in line 18, change score2 to num2.
D. In line 18, change else if to if.
E. In line 18, change else if to else.

asked
User Rich L
by
8.2k points

2 Answers

4 votes

Final answer:

The correct change that needs to be made in order for the reset method to work as intended is to change < to <= in lines 14 and 18.

Step-by-step explanation:

The correct change that needs to be made in order for the reset method to work as intended is option B: In lines 14 and 18, change < to <=.

By changing the comparison operators from < to <=, the reset method will correctly set the score to 0 if it is less than or equal to the threshold.

For example, if the threshold is 80 and score1 is 75, the current code would not reset score1 to 0. However, by changing < to <=, score1 will be set to 0 when it is less than or equal to the threshold, ensuring that the method works as intended.

answered
User Jbasko
by
8.7k points
4 votes

Final answer:

To make the reset method work as intended, we need to change < to >= in lines 14 and 18, and change else if to if in line 18.

Step-by-step explanation:

To make the reset method work as intended, we need to make two changes:

In lines 14 and 18, change < to >=. This will include scores that are equal to the threshold to be reset to 0 as well.

In line 18, change else if to if. This will ensure that both score1 and score2 are checked independently and can both be reset if they are below the threshold.

The corrected code would look like:

public void reset(int threshold)
{
if (score1 < threshold || score1 == threshold)
{
score1 = 0;
}
if (score2 < threshold || score2 == threshold)
{
score2 = 0;
}
}

answered
User Richard Hedges
by
7.5k points