0

requirement

Assume the availability of an existing class, ICalculator, that models an integer arithmetic calculator and contains:

  • an instance variable currentValue that stores the current int value
    of the calculator

  • methods add, sub, mul, and div

    Each method in ICalculator receives an int argument and applies its operation to currentValue and returns the new value of currentValue. So, if currentValue has the value 8 and sub(6) is invoked then currentValue ends up with the value 2, and 2 is returned.

So, you are to write the definition of a subclass, ICalculator1, based on ICalculator. The class ICalculator1 has one additional method, sign, that receives no arguments, and doesn't modify currentValue. Instead, it simply returns 1, 0 or -1 depending on the whether currentValue is positive, zero, or negative respectively.

code:

1 public class ICalculator1 extends ICalculator {
2 public int getCurrentValue() {return currentValue;} //**did not need this line**
3 
4 int sign() {
5 if(currentValue > -1) return 0; //changed to**int val = add(0);**
6 else if(currentValue < 0) return -1; //changed to **if (val > 0) return 1;**
7 else return 1; //changed to **else if (val < 0) return -1;**
8 }}

error message:

ICalculator1.java:2: error: currentValue has private access in ICalculator
public int getCurrentValue() {return currentValue;}
                                     ^
ICalculator1.java:5: error: currentValue has private access in ICalculator
if(currentValue > -1) return 0;
   ^
ICalculator1.java:6: error: currentValue has private access in ICalculator
else if(currentValue < 0) return -1;
        ^
3 errors

Not sure what I'm doing wrong?

Community
  • 1
  • 1
edmejia
  • 93
  • 1
  • 4
  • 15
  • For the second error, you should implement a getter. http://stackoverflow.com/questions/2036970/tutorial-on-getters-and-setters – irrelephant Oct 26 '12 at 02:10
  • What part of "ICalculator1.java:1: error: class iCalculator1 is public, should be declared in a file named iCalculator1.java" don't you understand? Same for the other errors. You are really expected to be able to figure out compiler errors for yourself. The only one that is even slightly complex is the "cannot find symbol" for iCalculator, which looks like an uppercase/lowercase problem according to your text. – user207421 Oct 26 '12 at 02:11
  • 1
    `ICalculator1.java:5: error: currentValue has private access in ICalculator`. Private members can be accessed only from within the class, change the visibility of the attribute to protected. – Luiggi Mendoza Oct 26 '12 at 02:11
  • The answer is in the error. currentValue is set to provate so you cant see it in your class. – case1352 Oct 26 '12 at 02:11
  • One fix is to change the problem field from `private` to `protected`. The other, as shown by others, is to simply use the accessor method. – Hot Licks Oct 26 '12 at 02:15
  • You should not prefix a class name with an `I`. You can do that with an interface. – Bhesh Gurung Oct 26 '12 at 02:19
  • @BheshGurung, this smells class assignment, he probably didn't write the `ICalculator` "class". But I may be mistaking. – Yanick Rochon Oct 26 '12 at 02:22

1 Answers1

3

So it seams that currentValue is private and thus cannot be accessed directly inside your subclass. Since your methods actually return the value of currentValue, you can exploit this inside your subclass, like this for example :

public int sign() {
   int val = add(0);   // add 0 and return currentValue
   if (val > 0) return 1;
   else if (val < 0) return -1;
   else return 0;
}

As others have suggested, if you can modify the visibility of currentValue, make it protected and you won't need that add(0) hack. However if you cannot modify ICalculator, this is one possibility.

Yanick Rochon
  • 51,409
  • 25
  • 133
  • 214