Hi, im quite new to java. may i know what is wrong is my constructor. Theres an error stating that "operator + cannot be applied to int,MyInteger." Thanks

System.out.print("Enter first integer number: "); MyInteger a = new MyInteger(Integer.parseInt(kbd.readLine())); System.out.print("Enter second integer number: "); MyInteger b = new MyInteger(Integer.parseInt(kbd.readLine())); System.out.println(a + "+" + b + " is: " + a.add(b));

//Below is the constructor MyInteger

public class MyInteger {

private int number;

public MyInteger(int initialnumber) { number = initialnumber; }

public int add (MyInteger addednumber) { number = number + addednumber; return number; }

In the method add(), the argument is of type "MyInteger", and you're tying to apply to "+" operator to that argument; hence the message that operator "+" can't be applied to MyInteger.

You can't add an int to a class object. Maybe you mean

Thanks but although i get the desired results for add() but it does not work for sub(); For example for the 1st input i enter '9' and 2nd input i enter '5' for add() i get 14 ---> correct ans but for sub() i get 9(14-5); --->do not know why the number variable used is 14 instead of 9.

Try to use this.number to solve but it din help.

//method for addition public int add (MyInteger addednumber) { number = number + addednumber.number; return number; }

//method for subtraction public int sub(MyInteger addednumber) { number = number - addednumber.number; return number; }

Well, your sub() and add() method looks like they should work. The problem must be in the main method. How does your main look now? I assume that you have added some kind of loop to permit several calculations.

Lim Youjie
Greenhorn

Joined: Dec 27, 2004
Posts: 22

posted

0

Below is my main method. i cant find out wats wrong. Thanks

System.out.print("Enter first integer number: "); MyInteger a = new MyInteger(Integer.parseInt(kbd.readLine())); System.out.print("Enter second integer number: "); MyInteger b = new MyInteger(Integer.parseInt(kbd.readLine())); System.out.println(a + "+" + b + " is: " + a.add(b)); System.out.println(a + "-" + b + " is: " + a.sub(b));

You may not have read the Naming Policy on your way in. Basically, it requires a real sounding first and last name. a first initial is ok, but the name cannot be anything obviously ficticious.

Would you be so kind as to edit your profile here to comply?

Thanks!!!

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

Martin Wingert
Greenhorn

Joined: Oct 10, 2005
Posts: 16

posted

0

�hm..well..that's because your add() and sub() methods alter the internal variable in the MyInteger class. So if input 9 as input 1 and then 5 as input 2 the add method will add 5 to the int called number in MyInteger. Since the add method does this:

number = number + addednumber.number;

If you didn't want the add method to alter the value of MyInteger then just return the recalculated value and don't touch the internal variable:

public int add (MyInteger addednumber) { //number = number + addednumber.number; return number+addednumber.number; }