Given:
public class ComputeSum {
public int x;
public int y;
public int sum;
public ComputeSum (int nx, int ny) {
x = nx; y =ny;
updateSum();
}
public void setX(int nx) { x = nx; updateSum();}
public void setY(int ny) { x = ny; updateSum();}
void updateSum() { sum = x + y;}
}
This class needs to protect an invariant on the sum field.
Which three members must have the private access modifier to ensure that this invariant is maintained?
- The x field
- The y field
- The sum field
- The ComputerSum ( ) constructor
- The setX ( ) method
- The setY ( ) method
Answer(s): C,E,F
Explanation:
The sum field and the two methods (setX and SetY) that updates the sum field.
Reveal Solution Next Question