 | 
September 30th, 2008, 12:01 PM
| | Newbie | | Join Date: Sep 2008
Posts: 2
| | calculator HELP!! - public class Calculator {
-
-
private long input = 0; // current input
-
private long result = 0; // last input/result
-
private String lastOperator = ""; // keeps track of the last operator entered
-
-
-
/* Digit entered as integer value i
-
* Updates the value of input accordingly to (input * 10) + i
-
*/
-
public void inDigit(long i) {
-
input = (input * 10) + i;
-
//System.out.println("** inDigit **input " + input + " **result " + result);
-
}
-
-
-
/* Operator entered + - or *
-
*/
-
public void inOperator(String op) {
-
inEquals();
-
// save the current input as result to get ready for next input
-
input = 0;
-
// remember which operator was entered
-
lastOperator = op;
-
}
-
-
-
/* Equals operation
-
* sets result to current result + - or * input (depending on
-
lastOperator)
-
*/
-
public void inEquals() {
-
if (lastOperator.equals("+")) {
-
result += input;
-
} else if (lastOperator.equals("-")) {
-
result -= input;
-
} else if (lastOperator.equals("*")) {
-
result *= input;
-
} else {
-
result = input;
-
}
-
// reset last operator to "nothing"
-
lastOperator = "";
-
}
-
-
-
/* Clear operation */
-
public void inClear() {
-
input = 0;
-
result = 0;
-
lastOperator = "";
-
}
-
-
/* return the current result */
-
public long getResult() {
-
return result;
-
}
-
-
/* return the current input value */
-
public long getCurrentInput() {
-
return input;
-
}
-
-
public void clearInput(){
-
input = 0;
-
}
-
-
-
}
-
-
-
-
-
-
-
-
-
-
import javax.swing.*;
-
import java.awt.event.*;
-
import java.awt.*;
-
import java.util.Scanner;
-
-
public class CalculatorPanel extends JPanel {
-
-
// an array of buttons displayed on the calculator
-
private JButton[] digitButtons;
-
-
// output display for the calculator
-
private JTextField display = new JTextField(10);
-
-
private Calculator calc = new Calculator();
-
-
/*main method - sets up JFrame*/
-
public static void main(String [] args){
-
JFrame frame = new JFrame("Calculator");
-
frame.setContentPane(new CalculatorPanel());
-
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
-
frame.pack();
-
frame.setVisible(true);
-
}
-
-
/* Constructor -- builds a GUI for a calculator */
-
public CalculatorPanel() {
-
-
/* create an array of button labels */
-
String[] buttonLabels = {"1", "2", "3", "4", "5", "6",
-
"7", "8", "9", "C", "0", "=", "+", "-", "*"};
-
-
/* Create an array of buttons. */
-
digitButtons = new JButton[buttonLabels.length];
-
-
/* Create an actionListener */
-
ButtonListener listener = new ButtonListener();
-
-
/* Create a 5 x 3 grid for placement of buttons. */
-
JPanel buttonGrid = new JPanel();
-
buttonGrid.setLayout(new GridLayout(5, 3));
-
-
/* Create a button with each button label, add it to buttonGrid,
-
* and register the button as a listener. */
-
for (int nextBut = 0; nextBut < digitButtons.length; nextBut++) {
-
digitButtons[nextBut] = new JButton(buttonLabels[nextBut]);
-
buttonGrid.add(digitButtons[nextBut]);
-
digitButtons[nextBut].addActionListener(listener);
-
}
-
-
/* Create a message for the user*/
-
JLabel instruct = new JLabel("Press a button");
-
-
/* add the components to this JPanel*/
-
setLayout(new BorderLayout());
-
add(instruct, BorderLayout.NORTH);
-
add(buttonGrid, BorderLayout.CENTER);
-
add(display, BorderLayout.SOUTH);
-
}
-
-
-
/* represents a listener for button presses */
-
private class ButtonListener implements ActionListener{
-
-
/* what to do when a button has been pressed */
-
public void actionPerformed(ActionEvent aE) {
-
JButton whichButton = (JButton) aE.getSource();
-
-
/* actions performed depending on which button is clicked */
-
if ("+".equals(whichButton.getText())){
-
calc.inOperator("+");
-
display.setText("" + calc.getResult());
-
-
}
-
else if ("-".equals(whichButton.getText())) {
-
calc.inOperator("-");
-
display.setText("" + calc.getResult());
-
-
}
-
else if ("*".equals(whichButton.getText())) {
-
calc.inOperator("*");
-
display.setText("" + calc.getResult());
-
-
}
-
else if ("C".equals(whichButton.getText())) {
-
calc.inClear();
-
display.setText("");
-
}
-
else if ("=".equals(whichButton.getText())) {
-
calc.inEquals();
-
display.setText("" + calc.getResult());
-
-
-
calc.inClear();
-
}
-
else {
-
long t = 0;
-
Scanner scan = new Scanner(whichButton.getText());
-
t = scan.nextInt();
-
calc.inDigit(t);
-
display.setText("" + calc.getCurrentInput());
-
}
-
-
}//end actionPerformed
-
}//end ButtonListener method
-
-
}//end class
-
- public class CalcText {
-
-
// the calculator we'll be using
-
private static Calculator c = new Calculator();
-
-
/* Main method - performs a simple calculation on the calculator */
-
public static void main(String[] args) {
-
// A simple calculation, 50 - 26 =
-
c.inDigit(5);
-
c.inDigit(0);
-
c.inOperator("-");
-
c.inDigit(2);
-
c.inDigit(6);
-
c.inEquals();
-
System.out.println( "50 - 26 = " + c.getResult());
-
c.inClear();
-
}
-
-
}
-
-
basically i can operate the calculator however, how can i process one or more of the following input sequences:
3+10= -4 =
3+4=+7=
what should i add??? Cheers!
Last edited by r035198x; September 30th, 2008 at 12:22 PM.
Reason: added code tags
| 
September 30th, 2008, 12:20 PM
| | Administrator | | Join Date: Sep 2006
Posts: 11,312
| |
1.) Please use code tags when posting code.
2.) Are you talking about implementing the memory function?
| 
September 30th, 2008, 10:17 PM
| | Newbie | | Join Date: Sep 2008
Posts: 2
| |
Yes. Cheers! Sorry bout not using tags, am new in this.
| 
October 2nd, 2008, 06:29 PM
| | Moderator | | Join Date: Mar 2007 Location: Voorschoten, the Netherlands Age: 51
Posts: 8,455
| | Quote: |
Originally Posted by mandy335 basically i can operate the calculator however, how can i process one or more of the following input sequences:
3+10= -4 =
3+4=+7=
what should i add??? Cheers! | You can think of an artificial variable 'answer' which contains the result of the last
expression (when the = operator is evaluated). This turns your examples into:
3+10= answer-4=
3+4= answer+7=
The 'answer' disappears when a digit is pressed just after the answer is produced.
A boolean variable can handle that (old calculators used that little trick).
kind regards,
Jos
|  |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|