calculator HELP!!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mandy335
    New Member
    • Sep 2008
    • 2

    calculator HELP!!

    [CODE=java]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.prin tln("** inDigit **input " + input + " **result " + result);
    }


    /* Operator entered + - or *
    */
    public void inOperator(Stri ng 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.e quals("+")) {
    result += input;
    } else if (lastOperator.e quals("-")) {
    result -= input;
    } else if (lastOperator.e quals("*")) {
    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.Scann er;

    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("Calcula tor");
    frame.setConten tPane(new CalculatorPanel ());
    frame.setDefaul tCloseOperation (JFrame.EXIT_ON _CLOSE);
    frame.pack();
    frame.setVisibl e(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.le ngth];

    /* Create an actionListener */
    ButtonListener listener = new ButtonListener( );

    /* Create a 5 x 3 grid for placement of buttons. */
    JPanel buttonGrid = new JPanel();
    buttonGrid.setL ayout(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.le ngth; nextBut++) {
    digitButtons[nextBut] = new JButton(buttonL abels[nextBut]);
    buttonGrid.add( digitButtons[nextBut]);
    digitButtons[nextBut].addActionListe ner(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.NO RTH);
    add(buttonGrid, BorderLayout.CE NTER);
    add(display, BorderLayout.SO UTH);
    }


    /* 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(whi chButton.getTex t())){
    calc.inOperator ("+");
    display.setText ("" + calc.getResult( ));

    }
    else if ("-".equals(whichB utton.getText() )) {
    calc.inOperator ("-");
    display.setText ("" + calc.getResult( ));

    }
    else if ("*".equals(whi chButton.getTex t())) {
    calc.inOperator ("*");
    display.setText ("" + calc.getResult( ));

    }
    else if ("C".equals(whi chButton.getTex t())) {
    calc.inClear();
    display.setText ("");
    }
    else if ("=".equals(whi chButton.getTex t())) {
    calc.inEquals() ;
    display.setText ("" + calc.getResult( ));


    calc.inClear();
    }
    else {
    long t = 0;
    Scanner scan = new Scanner(whichBu tton.getText()) ;
    t = scan.nextInt();
    calc.inDigit(t) ;
    display.setText ("" + calc.getCurrent Input());
    }

    }//end actionPerformed
    }//end ButtonListener method

    }//end class
    [/CODE]







    [CODE=java]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.prin tln( "50 - 26 = " + c.getResult());
    c.inClear();
    }

    }

    [/CODE]



    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; Sep 30 '08, 11:22 AM. Reason: added code tags
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    1.) Please use code tags when posting code.
    2.) Are you talking about implementing the memory function?

    Comment

    • mandy335
      New Member
      • Sep 2008
      • 2

      #3
      Yes. Cheers! Sorry bout not using tags, am new in this.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        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

        Comment

        Working...