Solved

Calculator

Posted on 2004-03-31
4
367 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:pete420
  • 2
4 Comments
 
LVL 13

Accepted Solution

by:
copyPasteGhost earned 20 total points
ID: 10728483
All fixed :)
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, newValue, 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
         {
         if (!inputBox.getText ().equals("")) {
              newValue = Double.valueOf (inputBox.getText ()).doubleValue ();
            }
            showStatus ("");
            if (arg.equals (" + "))                                      // add button clicked
               display_total = display_total + newValue;
            else if (arg.equals (" - "))                                  // subtract button clicked
               display_total = display_total - newValue;
            else if (arg.equals (" * "))                                  // multiply button clicked
               display_total = display_total * newValue;
            else if (arg.equals (" / "))                              // divide button clicked
               display_total = display_total / newValue;
            else if (arg.equals ("SQT"))                                  //SQRT Button
               display_total = Math.sqrt (newValue);
            else if (arg.equals ("SQR"))                                  //Square Button
               display_total = Math.pow (newValue, 2.0);
            else if (arg.equals ("1/X"))                                  //Reciprocal Button
               display_total = 1 / (newValue);
            else if (arg.equals ("+/-"))                                  //Invert Button
               display_total = newValue * (- 1);
            else if (arg.equals ("MS")){
               memSave = newValue;
            }  
            else if (arg.equals ("MR")) {
                display_total = memSave;
            }    
            else if (arg.equals ("M+"))
               memSave = (memSave + newValue);
            else if (arg.equals ("MC"))
               memSave = (0.0);
            displayBox.setText ("" + display_total);
            inputBox.setText ("");
         } //try
         catch (NumberFormatException entry)
         {
            showStatus ("Error: Invalid Input");
            inputBox.setText ("");
         } //catch

      } //else

   } //actionPerformed
} //Calculator

Cheers,
Ghost
0
 

Author Comment

by:pete420
ID: 10728960
many thanks


pete
0
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10729257
glad I can help you out.
Ghost
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question