Simple Calcular Program, But not working!!!

Hi,

This is my first thread. I am a starter and started my Java+Android course today.

My teacher gave me a problem of simple calculator and I completed the coding. But the result I got, only just one function is working properly. Its a program to simple add, subtract, multiply and divide 2 numbers. I am working in ECLIPSE.

I am pasting the code as well as the Result here, please tell me why others are not working while division is!!!

__CODE__

Code :

public class calc {
int a=5,b=5,iadd,isub,imul,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("SUM ="+iadd);
System.out.println("Subtraction ="+isub);
System.out.println("Multiplication ="+imul);
System.out.println("Division ="+idiv);
}
public static void main(String[] args) {
calc iadd= new calc();
iadd.add();
iadd.disp();
calc isub= new calc();
isub.sub();
isub.disp();
calc imul= new calc();
imul.mul();
imul.disp();
calc idiv= new calc();
idiv.div();
idiv.disp();
}
}

__And the RESULT i am getting__

SUM =0

Subtraction =0

Multiplication =0

Division =1

Please Help Me!!

Re: Simple Calcular Program, But not working!!!

When posting code, please use the highlight tags to preserve formatting. Unformatted code is pretty hard to read.

Also, please use the standard Java naming conventions (methods and variables start with a lower-case letter, classes with an upper-case letter). More descriptive names for everything would help, too. Using "sss" as a class name doesn't tell us what that class should do, and having multiple variables named a single letter makes it harder to keep track of.

Fix those problems, and we'll go from there.

Re: Simple Calcular Program, But not working!!!

Quote:

Originally Posted by

**KevinWorkman**
When posting code, please use the highlight tags to preserve formatting. Unformatted code is pretty hard to read.

Also, please use the standard Java naming conventions (methods and variables start with a lower-case letter, classes with an upper-case letter). More descriptive names for everything would help, too. Using "sss" as a class name doesn't tell us what that class should do, and having multiple variables named a single letter makes it harder to keep track of.

Fix those problems, and we'll go from there.

Sir, Thanks for giving me that idea of writing a code neatly with proper descriptive names. Well I did the editions, now could you please run the program in eclipse and could u tell me why I am getting that result?

I noticed a small thing, I rearranged the order which means I changed the order of multiplication to the last and it worked, which means, I got the result of multiplication but not any others worked.

Hope you will help me

Re: Simple Calcular Program, But not working!!!

Quote:

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

Code :

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:

Code java:

//
// 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:

Code :

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