hi,
i have been asked to create a calculator applet which includes basic functions (i.e add, subtract, multiply, divide)
But aswell square root, recipricol, positive/negative number switch.
It also has to be able to save the contents of a display field to a memory location, retrieve this data and clear the memory.
The code below is what i have managed so far, but i have no clue about the rest of the functions that are to be included i.e.sqrt etc.
Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends Applet implements
ActionListener
{
Label displayLabel, enterLabel;
TextField displayBox, enterBox;
Button addButton, subtButton, multButton, divButton;
Button clearButton, exitButton;
Button sqrButton;
double display_total, new_value;
String displayString, inputString;
public void init()
{ displayLabel = new Label("Display");
displayBox = new TextField(12);
add(displayLabel);
add(displayBox);
displayBox.setEditable(false);
display_total = 0.0;
displayString = Double.toString(display_total);
displayBox.setText(displayString);
enterLabel = Label("Enter");
enterBox = new TextField(12);
add(enterLabel);
add(enterBox);
addButton = new Button("+");
add(addButton);
addButton.addActionListener(this);
subtButton = new Button("-");
add(subtButton);
subtButton.addActionListener(this);
multButton = new Button("*");
add(multButton);
multButton.addActionListener(this);
divButton = new Button("/");
add(divButton);
divButton.addActionListener(this);
clearButton = new Button("Clear");
add(clearButton);
clearButton.addActionListener(this);
exitButton = new Button("Exit");
add(exitButton);
exitButton.addActionListener(this);
} end init
public void actionPerformed(ActionEvent event)
{
if (arg.equals("Exit"))
// Exit button clicked
{ Graphics g = this.getGraphics();
g.dispose();
System.exit(0);
} //exit
else if (arg.equals("Clear"))
// Clear button clicked
{ display_total = 0.0;
displayString = Double.toString(display_total);
displayBox.setText(displayString);
} //clear
else
{ try
{ inputString = enterBox.getText();
new_value = Double.valueOf(inputString).doubleValue();
showStatus("");
if(arg.equals("+"))
display_total = display_total + new_value;
else if (arg.equals("-"))
display_total = display_total - new_value;
else if (arg.equals("*"))
display_total = display_total * new_value;
else if (arg.equals("/"))
display_total = display_total / new_value;
else if (arg.equals("sqr"))
display_total = display_total * display_total;
}//try
catch (NumberFormatException entry)
{ showStatus("Error: Invalid Input");
enterBox.setText("");
}//catch
}//else
}//ActionPerformed
}//Calculator
ANY help apreciated.
Thanks