import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class RPN2 extends Applet implements ActionListener { Button one, two, three, four, five, six, seven, eight, nine, zero; //declare the buttons, and numerous variables Button enterButton, addButton, divButton, subButton, multButton, clrButton; // and the panels I used to pretty things int numberFlag, errFlag, opFlag, TogOpFlag = 1, enterFlag;//up. A lot of variables aren't used much, however double reg1, reg2, temp1, temp2; TextField value1; // but are their to help extend the program later one, ie, multiple registers Label Prompt1; String s1 = ""; Panel p1 = new Panel(); // da panels Panel p2 = new Panel(); Panel p3 = new Panel(); Panel p4 = new Panel(); Panel p5 = new Panel(); public void init() // now display the buttons and stuff, as well as set up the OS to look out for button presses. { Prompt1 = new Label("Result"); p1.add (Prompt1); value1 = new TextField(30); value1.setEditable(false); p1.add (value1); one = new Button("1"); p2.add (one); one.addActionListener(this); two = new Button("2"); p2.add (two); two.addActionListener(this); three = new Button("3"); p2.add (three); three.addActionListener(this); addButton = new Button("+"); p2.add (addButton); addButton.addActionListener(this); four = new Button("4"); p3.add (four); four.addActionListener(this); five = new Button("5"); p3.add (five); five.addActionListener(this); six = new Button("6"); p3.add (six); six.addActionListener(this); subButton = new Button("-"); subButton.addActionListener(this); p3.add (subButton); seven = new Button("7"); p4.add (seven); seven.addActionListener(this); eight = new Button("8"); p4.add (eight); eight.addActionListener(this); nine = new Button("9"); p4.add (nine); nine.addActionListener(this); multButton = new Button("*"); p4.add (multButton); multButton.addActionListener(this); clrButton = new Button("Clear"); p5.add (clrButton); clrButton.addActionListener(this); zero = new Button("0"); p5.add (zero); zero.addActionListener(this); enterButton = new Button("ENTER"); p5.add (enterButton); enterButton.addActionListener(this); divButton = new Button("/"); p5.add (divButton); divButton.addActionListener(this); add (p1); add (p2); add (p3); add (p4); add (p5); } public void actionPerformed (ActionEvent event) { if (event.getSource() == addButton) { //now when the operation buttons are presses (+, -, / ,*), it sets an opFlag variable to a value unique to that operation. if (opFlag == 1) { TogOpFlag++; } if (TogOpFlag != 1) { errFlag = 1; } opFlag = 1; repaint(); } else if (event.getSource() == subButton) { if (opFlag == 2) { TogOpFlag++; } if (TogOpFlag != 1) { errFlag = 1; } opFlag = 2; repaint(); } else if (event.getSource() == divButton) { if (opFlag == 3) { TogOpFlag++; } if (TogOpFlag != 1) { errFlag = 1; } opFlag = 3; repaint(); } else if (event.getSource() == multButton) { if (opFlag == 4) { TogOpFlag++; } if (TogOpFlag != 1) { errFlag = 1; } opFlag = 4; repaint(); } else if (event.getSource() == clrButton) { opFlag = 5; repaint(); } else if (event.getSource() == enterButton) { //if the enter button is pressed, we have to swap the register values around, and then zero reg1. enterFlag = 1; reg2 = reg1; reg1 = 0; s1 = ""; repaint(); } else if (event.getSource() == one) { //so that I can display the number that is pressed s1 = s1 + "1"; // I store what numbers have been pressed into a string repaint(); // so that I can easily have long numbers } else //then I simply convert the string to a integer, ready for use. if (event.getSource() == two) { s1 = s1 + "2"; repaint(); } else if (event.getSource() == three) { s1 = s1 + "3"; repaint(); } else if (event.getSource() == four) { s1 = s1 + "4"; repaint(); } else if (event.getSource() == five) { s1 = s1 + "5"; repaint(); } else if (event.getSource() == six) { s1 = s1 + "6"; repaint(); } else if (event.getSource() == seven) { s1 = s1 + "7"; repaint(); } else if (event.getSource() == eight) { s1 = s1 + "8"; repaint(); } else if (event.getSource() == nine) { s1 = s1 + "9"; repaint(); } else if (event.getSource() == zero) { s1 = s1 + "0"; repaint(); } } public void paint (Graphics g) { reg1 = Double.valueOf(s1).doubleValue(); //this is where the string is converted to a double and stored in the reg1, which was zeroed before value1.setText(s1); // and then the string s1 is displayed in the TextField, so the user knows what they have entered. if (errFlag == 1) { value1.setText("Only press an operation Flag once, dumb, dumb!"); } else if (opFlag == 1) { temp2 = reg2 + reg1; s1 = Double.toString(temp2); //then the result is shown in the Textfield after the calculation value1.setText(s1); reg2 = 0; } else if (opFlag == 2) { temp2 = reg2 - reg1; s1 = Double.toString(temp2); value1.setText(s1); reg2 = 0; } else if (opFlag == 3) { temp2 = reg2 / reg1; s1 = Double.toString(temp2); value1.setText(s1); reg2 = 0; } else if (opFlag == 4) { temp2 = reg2 * reg1; s1 = Double.toString(temp2); value1.setText(s1); reg2 = 0; } else if (opFlag == 5) { //when the clear function is pressed, then reset all the flags and variables and textfields. temp1 = 0; temp2 = 0; reg1 = 0; reg2 = 0; opFlag = 0; TogOpFlag = 1; errFlag = 0; s1 = ""; value1.setText(""); repaint(); } } }