Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

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

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post 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

    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!!
    Last edited by achugr; November 1st, 2012 at 02:20 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,606
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    Quote Originally Posted by KevinWorkman View Post
    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

  4. #4
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

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

    Quote Originally Posted by achugr View Post
    ...
    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
    Last edited by Zaphod_b; November 2nd, 2012 at 01:46 AM.

Similar Threads

  1. I have tried making a simple random calculator and its not working?
    By StackOfCookies in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 30th, 2012, 05:00 PM
  2. Simple Division not working heeeeelp
    By ashboi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 9th, 2012, 06:48 PM
  3. Simple code, cant get it working.
    By Malmen in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 8th, 2011, 03:28 PM
  4. Replies: 4
    Last Post: August 10th, 2011, 12:16 PM
  5. Need Help Getting A Simple Stop-Watch Working?
    By logmein in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 2nd, 2011, 08:57 AM

Tags for this Thread