For the questions below, consider the following class definition: public class A
ID: 3779071 • Letter: F
Question
For the questions below, consider the following class definition: public class AClass { protected int x; protected int y; public AClass(int a, int b) { x = a; y = b; } public int addEm( ) { return x + y; } public void changeEm( ) { x++; y--; } public String toString( ) { return "" + x + " " + y; } } You want addEm to now add all three values and return the sum and changeEm to change x and y, but leave z alone. Which should you do? 1. Redefine changeEm to call super.changeEm( ) and then set z = x + y, but leave addEm alone 2. Redefine changeEm to call super.changeEm( ) without doing anything to z, and redefine addEm to return super.addEm( ) 3. Redefine addEm to return the value of z + super.addEm( ), but leave changeEm alone 4. Redefine addEm to return the value of z + super.addEm( ) and redefine changeEm to call super.changeEm( ) and then set z = x + y 5. Redefine addEm and changeEm without referencing super.addEm( ) or super.changeEm( )
Explanation / Answer
Given the different options.
Our requirement is:
1. We want addEm to add all three values and return the sum.
2. We want changeEm to change x and y, but leave z alone.
In this class, changeEm() method is only modifying x, and y. And therefore, our 2nd point is achieved with this function itself.
But in addEm() funciton of this class, it only adds the value of x and y, but our requirement is to add all the 3 values, x, y, and z. Therefore, we have to modify addEm() as per the new requirement.
So, the only possible option is:
3. Redefine addEm to return the value of z + super.addEm( ), but leave changeEm alone
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.