Solved

Calculator

Posted on 2004-03-31
4
363 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
Comment Utility
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
Comment Utility
many thanks


pete
0
 
LVL 13

Expert Comment

by:copyPasteGhost
Comment Utility
glad I can help you out.
Ghost
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now