Results 1 to 3 of 3
  1. #1
    cart1443 is offline Member
    Join Date
    Feb 2008
    Posts
    5
    Rep Power
    0

    Default Java calculator decimal

    I need to have my calculator be able to work with decimals. I've changed all the pertinent numbers to be doubles, but I can't figure out the code to make things fit together. As of now I have it formatted to work the same way as the calculations (+-*/...) but this is backwards of what I need it to do. I understand the general idea that it needs to divide the number by 10 to the power of how many digits it is away from the decimal. So, for instance. .001 would be 1/(10^3). I understand the concept, it's just the code I am having problems with. Any help would be appreciated.

    What I have thus far is a little way down the code under "public void decimal()".

    PHP Code:
    import java.math.*;
    import java.util.Scanner;
    /**
     * The main part of the calculator doing the calculations.
     */
    
    //Ok this is what I have so far.. Basically I have just finished the plus operation on the calculator, and tried to fix as many problems as I could find, let me know if you find any other errors in the calculator
    //One person can help out gettin the Strings to display, and the other person can do the minus operation.. Ill try to finish the divide and *(times)..  I hope this is ok..
    public class CalcEngine
    {
       double displayValue; // this is what is displayed on the screen
       double store; // Used to store values, that later will be added etc...
       int identify= -1;// this variable will be used to let the calculator know whether it should add or subtract etc..
       int keep; // This variable is used that when the user adds etc.. the initial value can stay on the screen.
       int clear; // this variable is used, so that after the calculator computes an equation, it will clear the screen if another number is pushed instead of clear..
        /**
         * Create a CalcEngine instance. Initialize its state so that it is ready 
         * for use.
         */
        public CalcEngine()// starts off 
        {
        // sets values to these at the start
       displayValue= 0;
       store= 0;
        identify = -1;
        clear = -1;
        keep = -1;
        }
    
        /**
         * Return the value that should currently be displayed on the calculator
         * display.
         */
        public double getDisplayValue()
        {
    return displayValue;// returns the display value if this method is called.....
        }
    
       
         /**
         * A number button was pressed. Do whatever you have to do to handle it.
         * The number value of the button is given as a parameter.
         */
        public void numberPressed(int number)// if a number is pressed then...
        {
         if (keep == 0)// if keep is zero when a button is pressed, it will now reset the display value to zero, so that their new value will show on the calculator
         // Example say 2+2, if keep was not zero when you tried to put the second 2, the first one would still be displayed and it would look like this 2+22, not 2+2.
         //Go ahead and cut these if statments out of the program and run it, to see what problems they fix, then you can paste them back in...
         {
       
        displayValue= 0;
         keep = -1;
        }
            if (clear == 0)// First it checks to see if clear is -1 if it is that means that the users last input was an equals, This makes it so that if the user does 2 + 2 = 4, and then decides to type in new numbers it will reset all the values for the user whithout having to hit clear...
         {
       
        displayValue= 0;
         identify = -1;
         clear = -1;
        }
        displayValue = displayValue *10 + number;// makes it so you can input multi digit values.
       
       
        }
    
          /**
         * The 'plus' button was pressed. 
         */
        public void decimal()
        {
        if(identify == 0)// this is so that if two numbers are being added before the equals sign is pushed,it will automatically add them, display the value, and reset store..
            {
             displayValue = store + displayValue;
             store = 0;
            }
            if(identify == 1)
            {
             displayValue = store - displayValue;
             store = 0;
            }
            if(identify == 2)// this is so that if two  numbers are being *'ed before the equals sign is pushed,it will automatically times them, display the value, and reset store..
            {
             displayValue = store * displayValue;
             store = 0;
            }
            if(identify == 3)// this is so that if two  numbers are being /ed before the equals sign is pushed,it will automatically divide them, display the value, and reset store..
            {
             displayValue = store / displayValue;
             store = 0;
            }
            if(identify == 4)// this is so that if two  numbers are being /ed before the equals sign is pushed,it will automatically divide them, display the value, and reset store..
            {
             displayValue = displayValue / 10;
             store = 0;
            }
            if(keep == 0)// if this is zero it means that plus was already pressed and was the last thing pressed
            {
             displayValue = displayValue;//This if statement makes it so that if the user presses the plus button a bunch of times in a row, it wont do any calculations, it will just keep the display value the same...
            }
            else
            {
           clear = -1;// this is needed to set clear to -1 in case the plus button is hit after the equals button, now it wont reset the values once a number is pressed
           // example 2+2=4+4=8 it will now give us 8 correctly instead of an incorrect value 4...
         
         displayValue = displayValue / 10;//
         
          keep = 0;// sets keep to zero,this makes it so we can keep their input on the screen for them to see until another button is pressed...
          identify = 0; // sets identify to zero this lets the equals sign know we want to add....
         
        }
        }
        
        public void plus()// plus button is pressed
        {
            if(identify == 0)// this is so that if two numbers are being added before the equals sign is pushed,it will automatically add them, display the value, and reset store..
            {
             displayValue = store + displayValue;
             store = 0;
            }
            if(identify == 1)
            {
             displayValue = store - displayValue;
             store = 0;
            }
            if(identify == 2)// this is so that if two  numbers are being *'ed before the equals sign is pushed,it will automatically times them, display the value, and reset store..
            {
             displayValue = store * displayValue;
             store = 0;
            }
            if(identify == 3)// this is so that if two  numbers are being /ed before the equals sign is pushed,it will automatically divide them, display the value, and reset store..
            {
             displayValue = store / displayValue;
             store = 0;
            }
            if(identify == 4)// this is so that if two  numbers are being /ed before the equals sign is pushed,it will automatically divide them, display the value, and reset store..
            {
             displayValue = displayValue / 10;
             store = 0;
            }
            if(keep == 0)// if this is zero it means that plus was already pressed and was the last thing pressed
            {
             displayValue = displayValue;//This if statement makes it so that if the user presses the plus button a bunch of times in a row, it wont do any calculations, it will just keep the display value the same...
            }
            else
            {
           clear = -1;// this is needed to set clear to -1 in case the plus button is hit after the equals button, now it wont reset the values once a number is pressed
           // example 2+2=4+4=8 it will now give us 8 correctly instead of an incorrect value 4...
         store =store + displayValue;//this will take the display value and add it to the store value and set it to store..
        
         
          keep = 0;// sets keep to zero,this makes it so we can keep their input on the screen for them to see until another button is pressed...
          identify = 0; // sets identify to zero this lets the equals sign know we want to add....
         
        }
        }
        /**
         * The 'minus' button was pressed.
         */
    public void minus()// ONE Person could try to do the Minus 
        {
            if(identify == 0)// this is so that if two numbers are being added before the equals sign is pushed,it will automatically add them, display the value, and reset store..
            {
             displayValue = store + displayValue;
             store = 0;
            }
            if(identify == 1)
            {
             displayValue = store - displayValue;
             store = 0;
            }
            if(identify == 2)// this is so that if two  numbers are being *'ed before the equals sign is pushed,it will automatically times them, display the value, and reset store..
            {
             displayValue = store * displayValue;
             store = 0;
            }
            if(identify == 3)// this is so that if two  numbers are being /ed before the equals sign is pushed,it will automatically divide them, display the value, and reset store..
            {
             displayValue = store / displayValue;
             store = 0;
            }
            if(identify == 4)// this is so that if two  numbers are being /ed before the equals sign is pushed,it will automatically divide them, display the value, and reset store..
            {
             displayValue = displayValue / 10;
             store = 0;
            }
            if(keep == 0)// if this is zero it means that plus was already pressed and was the last thing pressed
            {
             displayValue = displayValue;//This if statement makes it so that if the user presses the plus button a bunch of times in a row, it wont do any calculations, it will just keep the display value the same...
            }
            else
            {
           clear = -1;// this is needed to set clear to -1 in case the plus button is hit after the equals button, now it wont reset the values once a number is pressed
           // example 2+2=4+4=8 it will now give us 8 correctly instead of an incorrect value 4...
         store = displayValue - store;//this will take the display value and add it to the store value and set it to store..
        
         
          keep = 0;// sets keep to zero,this makes it so we can keep their input on the screen for them to see until another button is pressed...
          identify = 1; // sets identify to zero this lets the equals sign know we want to add....
         
        }
        }
        
        public void product()//  and the *
        {
            if(identify == 4)// this is so that if two  numbers are being /ed before the equals sign is pushed,it will automatically divide them, display the value, and reset store..
            {
             displayValue = displayValue / 10;
             store = 0;
            }
            if(identify == 3)// this is so that if two  numbers are being /ed before the equals sign is pushed,it will automatically divide them, display the value, and reset store..
            {
             displayValue = store / displayValue;
             store = 0;
            }
             if(identify == 1)
            {
             displayValue = store - displayValue;
             store = 0;
            }
            if(identify == 2)// this is so that if two numbers are being *'d before the equals sign is pushed,it will automatically times them, display the value, and reset store..
            {
             displayValue = store * displayValue;
             store = 0;
            }
            if(identify == 0)// this is so that if two numbers are being added before the equals sign is pushed,it will automatically add them, display the value, and reset store..
            {
             displayValue = store + displayValue;
             store = 0;
            }
            if(keep == 0)// if this is zero it means that plus was already pressed and was the last thing pressed
            {
             displayValue = displayValue;//This if statement makes it so that if the user presses the plus button a bunch of times in a row, it wont do any calculations, it will just keep the display value the same...
            }
            else
            {
           clear = -1;
          if(store == 0)// if store is at zero then it will just add it to the display value, that way we dont come up with a zero by timesing it...
          {
           store =store + displayValue;//this will take the display value and add it to the store value and set it to store..
          }
         else
         {
           store = store * displayValue; //if store is not zero then we can times it no problem. 
            }
          keep = 0;// sets keep to zero,this makes it so we can keep their input on the screen for them to see until another button is pressed...
          identify = 2; // sets identify to two this lets the equals sign know we want to times....
          
        }
        }
        
        public void divide()//  divide...
        {
           if(identify == 4)// this is so that if two  numbers are being /ed before the equals sign is pushed,it will automatically divide them, display the value, and reset store..
            {
             displayValue = displayValue / 10;
             store = 0;
            }
            if(identify == 3)
            {
              displayValue = store / displayValue;  
            }
             if(identify == 2)// this is so that if two numbers are being *'d before the equals sign is pushed,it will automatically times them, display the value, and reset store..
            {
             displayValue = store * displayValue;
             store = 0;
            }
             if(identify == 1)
            {
             displayValue = store - displayValue;
             store = 0;
            }
            if(identify == 0)// this is so that if two numbers are being added before the equals sign is pushed,it will automatically add them, display the value, and reset store..
            {
             displayValue = store + displayValue;
             store = 0;
            }
            if(keep == 0)// if this is zero it means that plus was already pressed and was the last thing pressed
            {
             displayValue = displayValue;//This if statement makes it so that if the user presses the plus button a bunch of times in a row, it wont do any calculations, it will just keep the display value the same...
            }
            else
            {
           clear = -1;
          if(store == 0)// if store is at zero then it will just add it to the display value, that way we dont come up with a zero..
          {
           store =store + displayValue;//this will take the display value and add it to the store value and set it to store..
          }
         else
         {
           store = store / displayValue; //if store is not zero then we can divide no problem. 
            }
          keep = 0;// sets keep to zero,this makes it so we can keep their input on the screen for them to see until another button is pressed...
          identify = 3; // sets identify to two this lets the equals sign know we want to times....
        }
    }
        /**
         * The '=' button was pressed.
         */
        public void equals()
        {
            if(identify == 0)// if Identify is zero,(meaning the plus button was pressed) we want the calculator to add the two values they inputed..
            {
            displayValue = displayValue + store;// add those values and displays the new awnser..
            }
            if(identify == 1)
            {
             displayValue = store - displayValue;
             store = 0;
            }
            if(identify == 2)
            {
                displayValue = (displayValue * store);
            }
             if(identify == 3)
            {
                displayValue = (store / displayValue);
            }
            if(identify == 4)// this is so that if two  numbers are being /ed before the equals sign is pushed,it will automatically divide them, display the value, and reset store..
            {
             displayValue = displayValue / 10;
             store = 0;
            }
            store=0;//sets store to zero, because we are done with that value now..
             clear = 0; //sets clear to zero, so that if the user inputs new numbers it will clear the previous entry...
            identify = -1;// its done adding so we want this set back to -1...
        }
    
        /**
         * The 'C' (clear) button was pressed.
         */
        public void clear()
        {
         store = 0;// clears everything.. 
         displayValue= 0;
         identify = -1;
         clear = -1;
         keep = -1;
        }
    
        /**
         * Return the title of this calculation engine.
         */
        //ONE PERSON COULD WORK ON THE These returning the strings....  Im not sure how we are supposed to do them.....  IF we need to make buttons for returning the strings or what.....
        //Or it looks like from the other code, these may be displayed apon opening the program.. or the ? mark button...
        public String getTitle()
        {
            return "Calculator";
        }
    
        /**
         * Return the author of this engine. This string is displayed as it is,
         * so it should say something like "Written by H. Simpson".
         */
        public String getAuthor()
        {
            return "Groucho, Harpo, Chico" ;
        }
    
        /**
         * Return the version number of this engine. This string is displayed as 
         * it is, so it should say something like "Version 1.1".
         */
        public String getVersion()
        {
            return "Version 1.0";
        }
    }

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    can you post an example input and the expected correct output?

    regards,
    sukatoa

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    And also, when you post a code next time please use tags. And also used multiple lines for comment as much as possible. It's really difficult to read your thread.

    It can be caused to people avoid replaying to your question at all ;)

Similar Threads

  1. Java Calculator
    By aapanju in forum New To Java
    Replies: 3
    Last Post: 04-17-2008, 05:33 AM
  2. calculator not working
    By Renegade85 in forum New To Java
    Replies: 5
    Last Post: 03-10-2008, 03:27 PM
  3. Capping decimal places
    By Rageagainst20 in forum New To Java
    Replies: 1
    Last Post: 12-20-2007, 09:28 PM
  4. Create a Calculator in Java
    By Albert in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 08:01 AM
  5. Swing Calculator
    By nemo in forum AWT / Swing
    Replies: 1
    Last Post: 05-28-2007, 11:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •