3

Requirements

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 and can be accessed and modified by any subclass.

  • 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, ICalculator2, based on ICalculator. The class ICalculator2 has one additional method, negate, that receives no arguments. The effect of negate is to reverse the sign of currentValue. For example, if currentValue is zero, there is no change, if it is -22 then it becomes 22, if it is 100 it becomes -100. Furthermore negate returns the new value of currentValue.

Source Code

public class ICalculator2 extends ICalculator {
public int negate() { 
int val = add(0);      
if (val == -22) return val * -1;   
else if (val == 100) return val * -1;   
else return 0;}}

Remarks:

  • Your code had an error during execution

More Hints:

  • You might want to use a number other than 100

  • You might want to use a number other than 22

  • Are you sure you want to use: val

Hints:

  • You might want to use: <
  • Are you sure you want to use: =
Community
  • 1
  • 1
edmejia
  • 93
  • 1
  • 4
  • 15
  • 1
    you mentioned -22 will be converted as 22 but your code says 22 to -22 please make sure your code and statements are correct. – sunleo Oct 26 '12 at 06:49
  • 4
    why don't you simply multiply the value with `-1` – rbhawsar Oct 26 '12 at 06:49
  • yes..you should simply multiply the value by -1 – Sumit Desai Oct 26 '12 at 06:51
  • I don't think you are supposed to hardcode the values `22` and `100` in your code. Those were merely examples. You are supposed to write a general `negate()` method that works on any number, not just zero, 22 and 100. – Jim Garrison Oct 26 '12 at 06:51
  • I changed it the code around still same logic error: ⇒     The value of negatedValue is incorrect. – edmejia Oct 26 '12 at 06:52
  • why do you add `0` to the value? what's the point? – RGO Oct 26 '12 at 06:55
  • you want to change the negative sign integer to positive integer , am I right? – sunleo Oct 26 '12 at 07:01
  • @sunleo Wrong, and the question is perfectly clear already. – user207421 Oct 26 '12 at 07:37
  • possible duplicate of [Java math function to convert positive int to negative and negative to positive?](http://stackoverflow.com/questions/7869911/java-math-function-to-convert-positive-int-to-negative-and-negative-to-positive) – Nateowami Nov 26 '14 at 10:34

6 Answers6

18

You didn't need to do any of those strange calculations. Try this instead:

public class ICalculator2 extends ICalculator {
    public int negate() { 
        return (currentValue = -currentValue);
    }
}
RGO
  • 4,586
  • 3
  • 26
  • 40
2

You do this by manipulating one the methods provided for you in the ICalculate class. Remember that you cannot use the instance variable currentValue directly because it is private variable, and the subclass will only have access to private variables through methods. In this case the best method to manipulate is mul(), with the argument of -1.

public int negate(){

return mul(-1); }

1

I've just noticed that you're trying to extend what should conventionally be an interface. That won't work. You implement an interface.

Michele La Ferla
  • 6,775
  • 11
  • 53
  • 79
Mark
  • 31
  • 3
0

Please try this will help you to solve this problem

        int i = -22;
        i = i*-1;

        System.out.println(i);
            return i;
sunleo
  • 10,589
  • 35
  • 116
  • 196
  • If `i` is positive it still needs to be negated, and you don't need either an `if` or a multiplication to do that. Wrong answer. – user207421 Oct 26 '12 at 07:36
  • 1
    So you've addressed 50% of the problems I mentioned. Have you read the other answers? – user207421 Oct 26 '12 at 08:55
-1

The numbers you were given were an example. If there was a truth table using any other numbers, your example would fail. That's why there was an issue with the code. You need to use code that shows what happens when currentValue IS NOT 0.

Check out the following:

    public class ICalculator2 extends ICalculator {
        public int negate() {
          if (currentValue != 0) {
            return currentValue * -1;}
          else {
            return 0; 
            }
        }
    }
-2

Dude you've got this all wrong.

public class ICalculator2 extends ICalculator{

public int negate(){
    int val =mul(-1);
    return val;}}

you see? -22 and 100 are examples, they don't matter. The point is just that you need to negate whatever it is.