Originally Posted by
achugr
...
And the RESULT i am getting
SUM =0
Subtraction =0
Multiplication =0
Division =1
That's funny (funny-peculiar, not funny-haha).
Here is the result I got with your code exactly as you posted
SUM =10
Subtraction =0
Multiplication =0
Division =0
SUM =0
Subtraction =0
Multiplication =0
Division =0
SUM =0
Subtraction =0
Multiplication =25
Division =0
SUM =0
Subtraction =0
Multiplication =0
Division =1
Now, the first four lines show values of the iadd, isub, imul, and idiv fields of the iadd object. Since the only thing that was done with the iadd object was to add 5+5 and store the result in that object's iadd field, the other fields are at their default-initialized values of zero.
The second four lines show values of iadd, isub, imul, and idiv fields of the isub object. The only thing done with the isub object was to subtract 5 from 5 and store the result in that object's isub field. The other fields are at their default-initialized values of zero.
Etc.
Bottom line: For your class, each object has its own set of field values for iadd, isub, imul, and idiv. They are all initialized to zero when an object is created, and the values change when an object method is called.
It might be less confusing to us mere humans if you didn't use the same name for objects that you use for fields of the class, but Java never gets confused.
It also might be less confusing to people trying to make heads or tails of your output if you made the messages verbose enough to show us humans exactly what is being printed on each line.
Judicious and consistent use of whitespace in the code as well as in the program output also might make things more readable.
For example:
//
// Mods by Zaphod_b
// Note: Class name begins with upper case
//
public class Calc {
int a = 5;
int b = 15;
int iadd;
int isub;
int imul;
int idiv;
void add()
{
iadd = a + b;
}
void sub()
{
isub = b - a;
}
void mul()
{
imul = a * b;
}
void div()
{
idiv = b / a;
}
void disp()
{
System.out.println(" a = " + a);
System.out.println(" b = " + b);
System.out.println(" iadd = " + iadd);
System.out.println(" isub = " + isub);
System.out.println(" imul = " + imul);
System.out.println(" idiv = " + idiv);
}
public static void main(String [] args) {
Calc calc1 = new Calc();
System.out.println("Initial calc1:");
calc1.disp();
calc1.add();
System.out.println("calc1 after add():");
calc1.disp();
System.out.println();
Calc calc2 = new Calc();
System.out.println("Initial calc2:");
calc2.disp();
calc2.sub();
System.out.println("calc2 after sub():");
calc2.disp();
System.out.println();
Calc calc3 = new Calc();
System.out.println("Initial calc3:");
calc3.disp();
calc3.mul();
System.out.println("calc3 after mul():");
calc3.disp();
System.out.println();
Calc calc4 = new Calc();
System.out.println("Initial calc4:");
calc4.disp();
calc4.div();
System.out.println("calc4 after div():");
calc4.disp();
System.out.println();
} // End main method
} // End class definition
Output:
Initial calc1:
a = 5
b = 15
iadd = 0
isub = 0
imul = 0
idiv = 0
calc1 after add():
a = 5
b = 15
iadd = 20
isub = 0
imul = 0
idiv = 0
Initial calc2:
a = 5
b = 15
iadd = 0
isub = 0
imul = 0
idiv = 0
calc2 after sub():
a = 5
b = 15
iadd = 0
isub = 10
imul = 0
idiv = 0
Initial calc3:
a = 5
b = 15
iadd = 0
isub = 0
imul = 0
idiv = 0
calc3 after mul():
a = 5
b = 15
iadd = 0
isub = 0
imul = 75
idiv = 0
Initial calc4:
a = 5
b = 15
iadd = 0
isub = 0
imul = 0
idiv = 0
calc4 after div():
a = 5
b = 15
iadd = 0
isub = 0
imul = 0
idiv = 3
Cheers!
Z