Code :

public class Operations { private int number; public Operations(int num) { setTheNumber(num); } private void setTheNumber(int number) { this.number = number; } public int getNumber() { return number; } public Operations add(Operations op) { int num1, num2; num1 = this.getNumber(); // refers to the receiving objects data members num2 = op.getNumber(); // refers to the argument object Operations sum = new Operations(num1 + num2); return sum; } public Operations subtract(Operations op) { int num1, num2; num1 = this.getNumber(); num2 = op.getNumber(); Operations difference = new Operations(num1 - num2); return difference; } // when i add this method public String toString() { return getNumber() + "" ; } public Operations divide(Operations op) { int num1, num2; num1 = this.getNumber(); num2 = op.getNumber(); if (num1 == 0 || num2 == 0) { System.err.print("Cannot Divide By Zero"); System.exit(1); } Operations quotient = new Operations(num1 / num2); return quotient; } public Operations multiply(Operations op) { int num1, num2; num1 = this.getNumber(); num2 = op.getNumber(); Operations product = new Operations(num1 * num2); return product; } // Main public static void main(String[] args) { Operations num1, num2; Operations sum, diff, quot, prod; num1 = new Operations(10); num2 = new Operations(5); sum = num1.add(num2); diff = num1.subtract(num2); quot = num1.divide(num2); prod = num1.multiply(num2); System.out.println(sum); System.out.println(diff); System.out.println(quot); System.out.println(prod); } }

when im not yet Overriding the toString() method the output is

this:

Code :

xxTestxx.Operations@3e25a5 xxTestxx.Operations@19821f xxTestxx.Operations@addbf1 xxTestxx.Operations@42e816

but when i include the toString() method

the output became fine:

Code :

15 5 2 50

as you can see in my program, i didnt use the method toString().

but i dont really understand how does it affects the correct string representation,

i know a bit about memory address representation, so i know the first error,

thats the only thing i want to understand , why and how does the toString() method affects the output? when im not using it...

i know this program can be done in a primitive or rather natural way, but im currently reading a chapter about returning objects, this kind of object orientation suits for a fraction program( reducing fration) ,

i made an example like this so the concern can be noticeable (the fraction program is a bit confusing),

FOLLOW UP QUESTION:

question no 2:

the book says, when you return an object, you are actually returning a REFERENCE,

now my question is this.

take a look at this part

Code :

sum = num1.add(num2);

the object sum is receiving a reference from a returned object from the .add() method,

now what is the REFERENCE?

a). Is it the address?

b) Is it the value (15) - is the value 15 the reference?