HELP!
okay, i fixed the memory buttons from last time. now, i just realised that the basic functions e.g. square root, operate on teh value in the display box, not the input box. i had it working fine when it operated from the input box, but now, because it operates from the display box, the code doesnt work, i just get invalid input. please help me...
//This is the calculator applet. It allows users to calcuate different values based
//on the values the user enters. Functions the calculator can perform include addition,
//subtraction, division and multiplication. It also includes a memory function.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
publicclass Calculatorextends JAppletimplements ActionListener
{// declare all instances and variables
JLabel displayLabel,inputLabel;
JTextField displayBox,inputBox;
JButton clearButton, addButton,subtButton,multButton,divButton,sqrtButton,sqrButton,recipButton,posnegButton,MSButton,MRButton,MPButton,MCButton;
double display_total, new_value, mem_value, temp;
staticfinalint msint=0;
staticfinalint mrint=1;
staticfinalint mpint=2;
staticfinalint mcint=3;
//The init() method creates the instances of the components and places them inside
//the window. It also sets up components so the user can interact with them.
publicvoid init()
{// create and add display box
setLayout(new FlowLayout());
displayLabel =new JLabel("Display");
displayBox =new JTextField(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 JLabel("Input");
inputBox =new JTextField(12);
add(inputLabel);
add(inputBox);
//create and add add button
addButton =new JButton(" + ");
add(addButton);
addButton.addActionListener(this);
//create and add subtract button
subtButton =new JButton(" - ");
add(subtButton);
subtButton.addActionListener(this);
//create and add multiply button
multButton =new JButton(" * ");
add(multButton);
multButton.addActionListener(this);
//create and add divide button
divButton =new JButton(" / ");
add(divButton);
divButton.addActionListener(this);
//create and add clear button
clearButton =new JButton("Clear");
add(clearButton);
clearButton.addActionListener(this);
//create and add square root button
sqrtButton =new JButton(" SQT ");
add(sqrtButton);
sqrtButton.addActionListener(this);
//create and add square button
sqrButton =new JButton(" SQR ");
add(sqrButton);
sqrButton.addActionListener(this);
//create and add reciprocate button
recipButton =new JButton(" 1/X ");
add(recipButton);
recipButton.addActionListener(this);
//create and add reciprocate button
posnegButton =new JButton(" +/- ");
add(posnegButton);
posnegButton.addActionListener(this);
//create and add memory save button
MSButton =new JButton(" MS ");
add(MSButton);
MSButton.addActionListener(this);
//create and add memory retreive button
MRButton =new JButton(" MR ");
add(MRButton);
MRButton.addActionListener(this);
//create and add memory addition button
MPButton =new JButton(" M+ ");
add(MPButton);
MPButton.addActionListener(this);
//create and add memory clear button
MCButton =new JButton(" MC ");
add(MCButton);
MCButton.addActionListener(this);
}// init
publicvoid actionPerformed(ActionEvent event)
// to respond to clicking on buttons
{// to determine which button was clicked
String arg = event.getActionCommand();
double temp;
temp = Double.valueOf(displayBox.getText()).doubleValue();
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;
elseif (arg.equals(" - "))// subtract button clicked
display_total = display_total - new_value;
elseif (arg.equals(" * "))// multiply button clicked
display_total = display_total * new_value;
elseif (arg.equals(" / "))// divide button clicked
display_total = display_total / new_value;
elseif (arg.equals(" SQT "))// square root button clicked
display_total = Math.sqrt(display_total);
elseif (arg.equals(" SQR "))// square button clicked
display_total = display_total * display_total;
elseif (arg.equals(" 1/X "))// reciprocal button clicked
display_total = 1/display_total;
elseif (arg.equals(" +/- "))// positive negative button clicked
display_total = -display_total;
displayBox.setText(Double.toString(display_total));
inputBox.setText("");
}//try
catch (NumberFormatException entry)
{showStatus("Error: Invalid Input");
inputBox.setText("");
}//catch
//For some reason, the memory buttons do not function whilst inside the
//try statement. Therefore, I experimented with positioning the buttons
//in different places in the code. The buttons worked when placed
//outside the else statement. When each button is clicked, the same function
//is called but a different variable is passed. Depending on the variable
//passed, a different sequence of events is carried out.
if (arg.equals(" MS "))// memory save button clicked
memFunction(msint);
if (arg.equals(" MR "))// memory retreive button clicked
memFunction(mrint);
if (arg.equals(" M+ "))// memory addition button clicked
memFunction(mpint);
if (arg.equals(" MC "))// memory clear button clicked
memFunction(mcint);
}//else
}//actionPerformed
//In this function, a switch statement checks the value passed into the function
//against variables declared at the beginning of the code, and depending on the
//variable passed, a different sequence of events is carried out.
publicvoid memFunction(int i){
String memS;
showStatus("");
switch(i){
case msint:memS=displayBox.getText();
mem_value=Double.parseDouble(memS);
break;
case mrint:displayBox.setText(String.valueOf(mem_value));
break;
case mpint:temp=(new Double(displayBox.getText())).doubleValue();
mem_value=mem_value+temp;
break;
case mcint:mem_value=0;
break;
}
}
}//Calculator

