Comp103 - Java
Calculator App
Calculator.java
/*
* Calculator.java
*
* Lab 20, COMP103, 2007
*
* Anthony Robins
*
* A calculator class - for SIMPLE calculations like 5 + 20 =
* Large inputs will overload int, should convert to long
*/
public class Calculator {
private int input = 0; // current input
private int 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(int i) {
input = (input * 10) + i;
//System.out.println("** inDigit **input " + input + " **result " + result);
}
/* Operator entered + - or *
*/
public void inOperator(String op) {
// save the current input as result to get ready for next input
result = 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 {
input = 0;
}
// reset last operator to "nothing"
lastOperator = "";
}
/* Clear operation */
public void inClear() {
input = 0;
result = 0;
lastOperator = "";
}
/* return the current result */
public int getResult() {
return result;
}
/* return the current input value */
public int getCurrentInput() {
return input;
}
}
CalculatorPanel.java
/*
* CalculatorPanel.java Adapted from Koffman & Wolz
*
* Lab 20, COMP103, 2007
*
* An alternative GUI front end for Calculator
*
*/
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.Scanner;
public class CalculatorPanel extends JPanel {
Calculator calc = new Calculator();
// an array of buttons displayed on the calculator
private JButton[] digitButtons;
// output display for the calculator
private JTextField display = new JTextField(10);
/*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 4 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();
if ("+".equals(whichButton.getText())){
calc.inOperator("+");
}
else if ("-".equals(whichButton.getText())){
calc.inOperator("-");
}
else if ("*".equals(whichButton.getText())){
calc.inOperator("*");
}
else if ("=".equals(whichButton.getText())){
calc.inEquals();
calc.getResult();
display.setText("" + calc.getResult());
}
else if ("C".equals(whichButton.getText())){
calc.inClear();
display.setText("" + calc.getCurrentInput());
} else {
int t = 0;
Scanner scan = new Scanner(whichButton.getText());
t = scan.nextInt();
calc.inDigit(t);
display.setText("" + calc.getCurrentInput());
}
}
}
}
CalcText.java
/*
* CalcText.java
*
* Lab20, COMP103, 2007
*
* A simple text based application front end to Calculator
*
* Anthony Robins
*/
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();
}
}