Lim Youjie

Greenhorn

Posts: 22

posted 12 years ago

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

//Below are the codings from the main method

InputStreamReader isr = new InputStreamReader (System.in);

BufferedReader kbd = new BufferedReader (isr);

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;

}

}

//Below are the codings from the main method

InputStreamReader isr = new InputStreamReader (System.in);

BufferedReader kbd = new BufferedReader (isr);

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;

}

}

posted 12 years ago

The + operator is for primative types, not Objects (unless that Object is a String, which is a special case). So:

is fine, but:

is not. Now look at your code - do you see what you need to do to fix it?

is fine, but:

is not. Now look at your code - do you see what you need to do to fix it?

Lim Youjie

Greenhorn

Posts: 22

posted 12 years ago

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;

}

Thanks

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;

}

Thanks

Martin Wingert

Greenhorn

Posts: 16

Lim Youjie

Greenhorn

Posts: 22

posted 12 years ago

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

InputStreamReader isr = new InputStreamReader (System.in);

BufferedReader kbd = new BufferedReader (isr);

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));

InputStreamReader isr = new InputStreamReader (System.in);

BufferedReader kbd = new BufferedReader (isr);

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));

posted 12 years ago

sagolo,

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!!!

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

Posts: 16

posted 12 years ago

�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;

}

Do you get my explanation?

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;

}

Do you get my explanation?