473,473 Members | 1,491 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

calculator HELP!!

2 New Member
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!
Sep 30 '08 #1
3 2876
r035198x
13,262 MVP
1.) Please use code tags when posting code.
2.) Are you talking about implementing the memory function?
Sep 30 '08 #2
mandy335
2 New Member
Yes. Cheers! Sorry bout not using tags, am new in this.
Sep 30 '08 #3
JosAH
11,448 Recognized Expert MVP
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
Oct 2 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Jason | last post by:
Hello. I would like to create a little asp.net web form that will forecast a calendar date based on a user's input. Here's what the input would be like... 1. Select your product - A drop...
3
by: Sean McCourt | last post by:
Hi I am doing a JavaScript course and learning from the recommed book (JavaScript 3rd Edition by Don Gosslin) Below is one of the exercises from the book. I get this error message when I try to...
4
by: mwh | last post by:
Hi. If you remember, I posted Expressons Help. Now I am making a calculator with javascript. I can't get this to work: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"...
6
by: Rafael | last post by:
Hi Everyone, I need some help with my calculator program. I need my program to do 2 arguments and a 3rd, but the 3rd with different operators. Any help would be great. Here is my code.... ...
3
by: PieMan2004 | last post by:
Hi, ive been looking for a solid java community to help me when im tearing out my hair :) Basically ive constructed a GUI that has to represent the same look and functions of the typical windows...
19
by: TexasNewbie | last post by:
This was originally just a calculator without a decimal point. After I added the decimal, it now tells me invalid second number. //GUI Calculator Program import javax.swing.*; import...
2
by: akzim2200 | last post by:
i would like some help in filling the backspace command for my calculator. help really appreciated
4
by: mak1084 | last post by:
import javax.swing.*; import java.awt.event.*; public class Calculator extends JFrame { public Calculator() { JButton jBtn1=new JButton("1");
3
by: itsmichelle | last post by:
This is a very primative code of a java swing calculator. I have assigned all the number buttons and the operator buttons and I can add, subtract, multiply, and divide two numbers together. However,...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.