Solved

Calculator

Posted on 2004-03-31
4
368 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse console opening separately 2 45
java imports not found 4 67
Java array 10 84
activeMQ Queue Messages Enqueued and Messages Dequeued 1 96
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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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.
Suggested Courses

738 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