Link to home
Start Free TrialLog in
Avatar of pete420
pete420

asked on

Calculator

i designed a calculator but the memory save, restore, clear and add functions arent working
i think it is to do with converting from string to double and back.

code is below.
thanks


import java.applet.*;
import java.awt.*;
import java.awt.event.*;


public class Calculator extends Applet implements ActionListener
{      // declare all instances and variables
      Label displayLabel,inputLabel;
      TextField displayBox,inputBox;
      Button clearButton, addButton,subtButton,multButton,divButton, sqtButton, sqrButton, recipButton, invertButton;
      Button msButton, mrButton, maddButton, mcButton;
      double display_total, new_value, memSave;
            
      
      public void init()
      {      // creat and add display box
            displayLabel = new Label("Display");
              displayBox = new TextField(12);
            add(displayLabel);
            add(displayBox);
            displayBox.setEditable(false);
            display_total = 0.0;
            displayBox.setText(Double.toString(display_total));
            
            // create and add input box
            inputLabel = new Label("Input");
              inputBox = new TextField(12);
            add(inputLabel);
            add(inputBox);

            //create and add add button
            addButton = new Button(" + ");
            add(addButton);
            addButton.addActionListener(this);
            
            //create and add subtract button
            subtButton = new Button(" - ");
            add(subtButton);
            subtButton.addActionListener(this);
            
            //create and add multiply button
            multButton = new Button(" * ");
            add(multButton);
            multButton.addActionListener(this);
            
            //create and add divide button
            divButton = new Button(" / ");
            add(divButton);
            divButton.addActionListener(this);

            //create and add SQT Button
            sqtButton = new Button("SQT");
            add(sqtButton);
            sqtButton.addActionListener(this);
            
            //create and add SQR button
            sqrButton = new Button("SQR");
            add(sqrButton);
            sqrButton.addActionListener(this);
            
            //create and add 1/x button
            recipButton = new Button ("1/X");
            add(recipButton);
            recipButton.addActionListener(this);
            
            //create and add +/- button
            invertButton = new Button ("+/-");
            add(invertButton);
            invertButton.addActionListener(this);
            
            //create and add MS button
            msButton = new Button ("MS");
            add(msButton);
            msButton.addActionListener(this);
            
            //create and add MR button
            mrButton = new Button ("MR");
            add(mrButton);
            mrButton.addActionListener(this);
            
            //create and add M+ button
            maddButton = new Button ("M+");
            add(maddButton);
            maddButton.addActionListener(this);
            
            //create and add MC button
            mcButton = new Button ("MC");
            add(mcButton);
            mcButton.addActionListener(this);
            
            //create and add clear button
            clearButton = new Button("Clear");
            add(clearButton);
            clearButton.addActionListener(this);
            
            }// init

            
            public void actionPerformed(ActionEvent event)
            // to respond to clicking on buttons      
            {      // to determine which button was clicked
                  String arg = event.getActionCommand();
            
                  if (arg.equals("Clear"))      // Clear button clicked
                  {            display_total = 0.0;
                              displayBox.setText(Double.toString(display_total));
                  }//clear
                  else                                                                              // a new value has been entered                                                
                  {      try
                        {       new_value = Double.valueOf(inputBox.getText()).doubleValue();
                              showStatus("");
                              if(arg.equals(" + "))                                                // add button clicked
                                    display_total = display_total + new_value;
                              else if (arg.equals(" - "))                                          // subtract button clicked
                                    display_total = display_total - new_value;
                              else if (arg.equals(" * "))                                          // multiply button clicked
                                    display_total = display_total * new_value;
                              else if (arg.equals(" / "))                                     // divide button clicked
                                    display_total = display_total / new_value;
                              else if (arg.equals("SQT"))                                          //SQRT Button
                                    display_total = Math.sqrt(new_value);
                              else if (arg.equals("SQR"))                                          //Square Button
                                    display_total = Math.pow(new_value,2.0);                              
                              else if (arg.equals("1/X"))                                          //Reciprocal Button
                                    display_total = 1/(new_value);                  
                              else if (arg.equals("+/-"))                                          //Invert Button
                                    display_total = new_value * (-1);                              
                              else if (arg.equals("MS"))
                                    memSave = display_total;                              
                              else if (arg.equals("MR"))
                                    displayBox.setText(Double.toString(memSave));
                              //      display_total = memSave;                              
                              else if (arg.equals("M+"))
                                    memSave = (memSave + display_total);                              
                              else if (arg.equals("MC"))
                                    memSave = (0.0);                        
                              displayBox.setText(Double.toString(display_total));
                              inputBox.setText("");
                        }//try            
                        catch (NumberFormatException entry)
                        {      showStatus("Error: Invalid Input");
                              inputBox.setText("");
                        }//catch
                                    
                  }//else
            
            }//actionPerformed
      
}//Calculator
ASKER CERTIFIED SOLUTION
Avatar of copyPasteGhost
copyPasteGhost
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pete420
pete420

ASKER

many thanks


pete
glad I can help you out.
Ghost