/* Applet Arithmelet implements a simple GUI calculator, providing three LCDs (one for each operand and one for the result of one of 4 operations: add, subtract, multiply, and divide. There is an non-Applet version called Arithmelator that implements the same feature set. Program written by Michael Grobe...April 2001. */ import javax.swing.*; import java.awt.*; import java.awt.Frame.*; import javax.swing.border.*; import java.awt.event.*; public class Arithmelet extends javax.swing.JApplet implements ActionListener { public static String firstOperandString = new String(""); public static double firstOperandValue=0; public static String secondOperandString = new String(""); public static double secondOperandValue=0; public static String resultString = new String("0"); public static double result=0; public static String resultSaveString = new String(""); public static String operator = new String(""); public static JTextField firstOperandLCD = new JTextField(12); public static JTextField secondOperandLCD = new JTextField(12); public static JTextField resultLCD = new JTextField(12); public static Font LCDfont = new Font("Geneva", Font.BOLD, 40); public void init() // set up the GUI here. { JFrame arithmeletFrame = new JFrame("Arithmelet"); Container c = arithmeletFrame.getContentPane(); // replace this line with //Container c = this.getContentPane(); // this line to display applet // within the html page. c.setLayout(new BorderLayout(10,10)); arithmeletFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // remove this line to display in page. JPanel operands = new JPanel(); TitledBorder operandsBorder = new TitledBorder("Enter operands here"); operandsBorder.setTitlePosition(TitledBorder.TOP); operandsBorder.setTitleJustification(TitledBorder.LEFT); operands.setBorder(operandsBorder); operands.setLayout(new GridLayout(3,1)); firstOperandLCD.setFont(LCDfont); firstOperandLCD.setEditable(true); firstOperandLCD.setText(firstOperandString); operands.add(firstOperandLCD); JPanel opkeys = new JPanel(); TitledBorder opkeyBorder = new TitledBorder("Choose an operator"); opkeyBorder.setTitlePosition(TitledBorder.TOP); opkeyBorder.setTitleJustification(TitledBorder.LEFT); opkeys.setBorder(opkeyBorder); Font opkeyfont = new Font("Geneva", Font.BOLD, 40); opkeys.setLayout(new GridLayout(1,5,0,0) ); JButton butplus = new JButton("+"); butplus.setActionCommand("plus"); butplus.addActionListener(this); butplus.setFont(opkeyfont); JButton butminus= new JButton("-"); butminus.setActionCommand("minus"); butminus.addActionListener(this); butminus.setFont(opkeyfont); JButton butmultiply = new JButton("*"); butmultiply.setActionCommand("multiply"); butmultiply.addActionListener(this); butmultiply.setFont(opkeyfont); JButton butdivide = new JButton("/"); butdivide.setActionCommand("divide"); butdivide.addActionListener(this); butdivide.setFont(opkeyfont); opkeys.add(butplus); opkeys.add(butminus); opkeys.add(butmultiply); opkeys.add(butdivide); operands.add(opkeys); secondOperandLCD.setFont(LCDfont); secondOperandLCD.setEditable(true); secondOperandLCD.setText(secondOperandString); operands.add(secondOperandLCD); JPanel assignkeys = new JPanel(); TitledBorder assignkeyBorder = new TitledBorder("Move the result or calculate"); assignkeyBorder.setTitlePosition(TitledBorder.TOP); assignkeyBorder.setTitleJustification(TitledBorder.LEFT); assignkeys.setBorder(assignkeyBorder); Font assignkeyfont = new Font("Geneva", Font.BOLD, 40); assignkeys.setLayout(new GridLayout(1,3,0,0) ); JButton butequal = new JButton("="); butequal.setActionCommand("equal"); butequal.addActionListener(this); butequal.setFont(assignkeyfont); JButton butshift = new JButton("shift"); butshift.setActionCommand("shift"); butshift.addActionListener(this); butshift.setFont(assignkeyfont); assignkeys.add(butshift); assignkeys.add(butequal); //operands.add(assignkeys); c.add(operands, "North"); c.add(assignkeys, "Center"); JPanel result = new JPanel(); TitledBorder resultBorder = new TitledBorder("Here is the result"); resultBorder.setTitlePosition(TitledBorder.TOP); resultBorder.setTitleJustification(TitledBorder.LEFT); result.setBorder(resultBorder); result.setLayout(new GridLayout(1,1,5,10) ); resultLCD.setFont(LCDfont); resultLCD.setEditable(true); resultLCD.setText(resultString); result.add(resultLCD); c.add(result, "South"); arithmeletFrame.setBounds(100,100,330,500); // replace this line with //this.setBounds(100,100,330,500); // this line to stay in page. arithmeletFrame.setVisible(true); // replace this line with //this.setVisible(true); // this line to stay in page. } public Arithmelet() { } public double ConverttoDouble(String astring) throws NumberFormatException { double quack; System.out.println("Converting:"+astring); try { quack = Double.parseDouble(astring); } catch ( NumberFormatException exception1) { System.out.println("Processing exception."); quack = 0.0d; } return quack; } public String ConverttoNaN(String astring) throws NumberFormatException { double quack; System.out.println("Converting:"+astring); try { quack = Double.parseDouble(astring); } catch ( NumberFormatException exception1) { System.out.println("Processing exception."); return "NaN"; } return astring; } public void actionPerformed(ActionEvent event) // Process GUI input here. { String command = event.getActionCommand(); System.out.println("The command (button) pressed was:"+command); if ( ( command == "plus" ) || ( command == "multiply" ) || ( command == "divide" ) || ( command == "minus" ) ) { operator = command; System.out.println("Operator="+operator); } else if ( command == "equal" ) { firstOperandString = firstOperandLCD.getText(); firstOperandValue = ConverttoDouble( firstOperandString ); firstOperandString = ConverttoNaN( firstOperandString ); System.out.println("First op="+firstOperandValue); System.out.println("First op string="+firstOperandString); secondOperandString = secondOperandLCD.getText(); secondOperandValue = ConverttoDouble( secondOperandString ); secondOperandString = ConverttoNaN( secondOperandString ); System.out.println("Second op="+secondOperandValue); System.out.println("Second op string="+secondOperandString); if ( firstOperandString.equals("NaN") || secondOperandString.equals("NaN") ) { resultString = "NaN"; } else if ( operator.equals( "plus" ) ) { result = firstOperandValue + secondOperandValue; } else if (operator.equals( "minus" ) ) { result = firstOperandValue - secondOperandValue; } else if (operator.equals( "multiply" ) ) { result = firstOperandValue * secondOperandValue; } else if (operator.equals( "divide" ) ) { if ( 1 == 1 ) //secondOperandValue != 0.0d ) { result = firstOperandValue / secondOperandValue; } else { result = 0.0d; // this should be epsilon compare! resultString = "NaN"; } } else { System.out.println( "No operator specified." ); resultString = "No operator specified."; } System.out.println ("Result is:"+result); if ( ! resultString.equals ("NaN") && ! resultString.equals ("No operator specified.") ) { resultString = Double.toString(result); } int fieldwidth = resultLCD.getSize().width; int fontsize = (int) (1.9 * (fieldwidth / (resultString.length() + 1))); if (fontsize >40 ) { fontsize = 40; } // Adjust LCD text font so that the contents always fit. Font LCDadjustablefont = new Font("Geneva", Font.BOLD, fontsize); resultLCD.setFont(LCDadjustablefont); resultLCD.setText(resultString); operator = ""; resultSaveString = resultString; resultString = ""; } else if ( command == "shift" ) { firstOperandValue = result; firstOperandString = resultSaveString; resultString = ""; result = 0.0d; // Adjust LCD text font so that the contents always fit. int fieldwidth = firstOperandLCD.getSize().width; int fontsize = (int) (1.9 * (fieldwidth / (firstOperandString.length() + 1))); if (fontsize >40 ) { fontsize = 40; } Font LCDadjustablefont = new Font("Geneva", Font.BOLD, fontsize); firstOperandLCD.setFont(LCDadjustablefont); firstOperandLCD.setText(firstOperandString); fieldwidth = resultLCD.getSize().width; fontsize = (int) (1.9 * (fieldwidth / (resultString.length() + 1))); if (fontsize >40 ) { fontsize = 40; } // Adjust LCD text font so that the contents always fit. Font LCDadjustablefont2 = new Font("Geneva", Font.BOLD, fontsize); resultLCD.setFont(LCDadjustablefont2); resultLCD.setText(resultString); } else { resultLCD.setText("Operation unknown."); System.out.println("Operation unknown."); stop(); } } }