Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old September 30th, 2008, 12:01 PM
Newbie
 
Join Date: Sep 2008
Posts: 2
Default calculator HELP!!

Expand|Select|Wrap|Line Numbers
  1. public class Calculator {
  2.  
  3.  private long input = 0;             // current input
  4.  private long result = 0;            // last input/result
  5.  private String lastOperator = "";  // keeps track of the last operator entered
  6.  
  7.  
  8.  /* Digit entered as integer value i
  9.   * Updates the value of input accordingly to (input * 10) + i
  10.   */
  11.  public void inDigit(long i) {
  12.    input = (input * 10) + i;
  13.    //System.out.println("** inDigit  **input " + input + "  **result " + result);
  14.  }
  15.  
  16.  
  17.  /* Operator entered  + - or *
  18.   */
  19.  public void inOperator(String op) {
  20.      inEquals();
  21.    // save the current input as result to get ready for next input
  22.    input = 0;
  23.    // remember which operator was entered
  24.    lastOperator = op;
  25.  }
  26.  
  27.  
  28.  /* Equals operation
  29.   * sets result to current result + - or * input (depending on
  30.   lastOperator)
  31.   */
  32.  public void inEquals() {
  33.    if (lastOperator.equals("+")) {
  34.      result += input;
  35.    } else if (lastOperator.equals("-")) {
  36.      result -= input;
  37.    } else if (lastOperator.equals("*"))  {
  38.      result *= input;
  39.    } else {
  40.      result = input;
  41.    }
  42.    // reset last operator to "nothing"
  43.    lastOperator = "";
  44.  }
  45.  
  46.  
  47.  /* Clear operation */
  48.  public void inClear() {
  49.    input = 0;
  50.    result = 0;
  51.    lastOperator = "";
  52.  }
  53.  
  54.  /* return the current result */
  55.  public long getResult() {
  56.    return result;
  57.  }
  58.  
  59.  /* return the current input value */
  60.  public long getCurrentInput() {
  61.    return input;
  62.  }
  63.  
  64.  public void clearInput(){
  65.    input = 0;
  66.  }
  67.  
  68.  
  69. }
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79. import javax.swing.*;
  80. import java.awt.event.*;
  81. import java.awt.*;
  82. import java.util.Scanner;
  83.  
  84. public class CalculatorPanel extends JPanel {
  85.  
  86.   // an array of buttons displayed on the calculator
  87.   private JButton[] digitButtons;
  88.  
  89.   // output display for the calculator
  90.   private JTextField display = new JTextField(10);
  91.  
  92.   private Calculator calc = new Calculator();
  93.  
  94.   /*main method - sets up JFrame*/
  95.   public static void main(String [] args){
  96.     JFrame frame = new JFrame("Calculator");
  97.     frame.setContentPane(new CalculatorPanel());
  98.     frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  99.     frame.pack();
  100.     frame.setVisible(true);
  101.   }
  102.  
  103.   /* Constructor -- builds a GUI for a calculator */
  104.   public CalculatorPanel() {
  105.  
  106.     /* create an array of button labels */
  107.     String[] buttonLabels =  {"1", "2", "3", "4", "5", "6",
  108.       "7", "8", "9", "C", "0", "=", "+", "-", "*"};
  109.  
  110.     /* Create an array of buttons. */
  111.     digitButtons = new JButton[buttonLabels.length];
  112.  
  113.     /* Create an actionListener */
  114.     ButtonListener  listener = new ButtonListener();
  115.  
  116.     /* Create a 5 x 3 grid for placement of buttons. */
  117.     JPanel buttonGrid = new JPanel();
  118.     buttonGrid.setLayout(new GridLayout(5, 3));
  119.  
  120.     /* Create a button with each button label, add it to buttonGrid,
  121.      * and register the button as a listener. */
  122.     for (int nextBut = 0; nextBut < digitButtons.length; nextBut++) {
  123.       digitButtons[nextBut] = new JButton(buttonLabels[nextBut]);
  124.       buttonGrid.add(digitButtons[nextBut]);
  125.       digitButtons[nextBut].addActionListener(listener);
  126.     }
  127.  
  128.     /* Create a message for the user*/
  129.     JLabel instruct = new JLabel("Press a button");
  130.  
  131.     /* add the components to this JPanel*/
  132.     setLayout(new BorderLayout());
  133.     add(instruct, BorderLayout.NORTH);
  134.     add(buttonGrid, BorderLayout.CENTER);
  135.     add(display, BorderLayout.SOUTH);
  136.   }
  137.  
  138.  
  139.   /* represents a listener for button presses */
  140.   private class ButtonListener implements ActionListener{
  141.  
  142.     /* what to do when a button has been pressed */
  143.     public void actionPerformed(ActionEvent aE) {
  144.       JButton whichButton = (JButton) aE.getSource();
  145.  
  146.       /* actions performed depending on which button is clicked */
  147.       if ("+".equals(whichButton.getText())){
  148.         calc.inOperator("+");
  149.         display.setText("" + calc.getResult());
  150.  
  151.       }
  152.       else if ("-".equals(whichButton.getText())) {
  153.         calc.inOperator("-");
  154.         display.setText("" + calc.getResult());
  155.  
  156.       }
  157.       else if ("*".equals(whichButton.getText())) {
  158.         calc.inOperator("*");
  159.         display.setText("" + calc.getResult());
  160.  
  161.       }
  162.       else if ("C".equals(whichButton.getText())) {
  163.         calc.inClear();
  164.         display.setText("");
  165.       }
  166.       else if ("=".equals(whichButton.getText())) {
  167.         calc.inEquals();
  168.         display.setText("" + calc.getResult());
  169.  
  170.  
  171.         calc.inClear();
  172.       }
  173.       else {
  174.         long t = 0;
  175.         Scanner scan = new Scanner(whichButton.getText());
  176.         t = scan.nextInt();
  177.         calc.inDigit(t);
  178.         display.setText("" + calc.getCurrentInput());
  179.       }
  180.  
  181.     }//end actionPerformed
  182.   }//end ButtonListener method
  183.  
  184. }//end class
  185.  






Expand|Select|Wrap|Line Numbers
  1. public class CalcText {  
  2.  
  3.  // the calculator we'll be using
  4.  private static Calculator c = new Calculator();
  5.  
  6.  /* Main method - performs a simple calculation on the calculator */ 
  7.  public static void main(String[] args) {
  8.   // A simple calculation, 50 - 26 =  
  9.   c.inDigit(5);
  10.   c.inDigit(0);
  11.   c.inOperator("-");
  12.   c.inDigit(2);
  13.   c.inDigit(6);
  14.   c.inEquals();
  15.   System.out.println( "50 - 26 = " + c.getResult()); 
  16.   c.inClear();
  17.  } 
  18.  
  19. }  
  20.  
  21.  


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
Reply
  #2  
Old September 30th, 2008, 12:20 PM
Administrator
 
Join Date: Sep 2006
Posts: 11,312
Default

1.) Please use code tags when posting code.
2.) Are you talking about implementing the memory function?
Reply
  #3  
Old September 30th, 2008, 10:17 PM
Newbie
 
Join Date: Sep 2008
Posts: 2
Default

Yes. Cheers! Sorry bout not using tags, am new in this.
Reply
  #4  
Old October 2nd, 2008, 06:29 PM
Moderator
 
Join Date: Mar 2007
Location: Voorschoten, the Netherlands
Age: 51
Posts: 8,455
Default

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
Reply
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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.
Post your question now . . .
It's fast and it's free

Popular Articles