Solved

Calculator

Posted on 2004-03-31
4
366 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSum5 challenge 5 85
Java JRE greater than 1.6 5 63
javap not working 8 46
Configure a Bean in an XML file 4 33
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

831 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