?
Solved

Calculator

Posted on 2004-03-31
4
Medium Priority
?
370 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 80 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month12 days, 13 hours left to enroll

777 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