|
-
Need help with calculator...
...okay, ive 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...
Code:
//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.*;
public class Calculator extends JApplet implements 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;
static final int msint=0;
static final int mrint=1;
static final int mpint=2;
static final int 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.
public void 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
public void 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;
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 ")) // square root button clicked
display_total = Math.sqrt(display_total);
else if (arg.equals(" SQR ")) // square button clicked
display_total = display_total * display_total;
else if (arg.equals(" 1/X ")) // reciprocal button clicked
display_total = 1/display_total;
else if (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.
public void 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
Similar Threads
-
By cutey_kira in forum .NET
Replies: 1
Last Post: 10-08-2005, 02:51 PM
-
Replies: 7
Last Post: 09-06-2005, 05:06 PM
-
Replies: 4
Last Post: 05-16-2005, 05:19 PM
-
By James Abbott in forum Web
Replies: 0
Last Post: 08-18-2001, 12:23 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks