This skeleton program must be enhanced as follows: Your implementation of action
ID: 3806334 • Letter: T
Question
This skeleton program must be enhanced as follows:
Your implementation of actionListener will contain your code that responds to button clicks or text entries on the calculator's surface.
You muat allow entries using either the keys or by typing text into the expression text field.
The CE button must clear the last operand entered while the C button must clear the entire expression textfield
The equals button must calculate the result and write the resulting value into the result textfield. You must write the evaluation function. We will discuss this in class. Your evaluation must evaluate each operator for it's true precedence. This little bit of logic will take some work!
If the user's expression is gibberish or produced an undefined value like divide by zero - you may write #ERROR# or NaN or any other meaningful indication of a bad expression.
Simple Calc CEExplanation / Answer
PROGRAM CODE:
package GUI;
import java.awt.*;
import java.awt.event.*;
import java.util.Stack;
import javax.swing.*;
public class SimpleCalc
{
JFrame window; // the main window which contains everything
Container content ;
JButton[] digits = new JButton[12];
JButton[] ops = new JButton[4];
JTextField expression;
JButton equals;
JTextField result;
String[] opCodes = { "+", "-", "*", "/" };
public SimpleCalc()
{
window = new JFrame( "Simple Calc");
content = window.getContentPane();
content.setLayout(new GridLayout(2,1)); // 2 row, 1 col
ButtonListener listener = new ButtonListener();
// top panel holds expression field, equals sign and result field
// [4+3/2-(5/3.5)+3] = [3.456]
JPanel topPanel = new JPanel();
topPanel.setLayout(new GridLayout(1,3)); // 1 row, 3 col
expression = new JTextField();
expression.setFont(new Font("verdana", Font.BOLD, 16));
expression.setText("");
equals = new JButton("=");
equals.setFont(new Font("verdana", Font.BOLD, 20 ));
equals.addActionListener( listener );
result = new JTextField();
result.setFont(new Font("verdana", Font.BOLD, 16));
result.setText("");
topPanel.add(expression);
topPanel.add(equals);
topPanel.add(result);
// bottom panel holds the digit buttons in the left sub panel and the operators in the right sub panel
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new GridLayout(1,2)); // 1 row, 2 col
JPanel digitsPanel = new JPanel();
digitsPanel.setLayout(new GridLayout(4,3));
for (int i=0 ; i<10 ; i++ )
{
digits[i] = new JButton( ""+i );
digitsPanel.add( digits[i] );
digits[i].addActionListener( listener );
}
digits[10] = new JButton( "C" );
digitsPanel.add( digits[10] );
digits[10].addActionListener( listener );
digits[11] = new JButton( "CE" );
digitsPanel.add( digits[11] );
digits[11].addActionListener( listener );
JPanel opsPanel = new JPanel();
opsPanel.setLayout(new GridLayout(4,1));
for (int i=0 ; i<4 ; i++ )
{
ops[i] = new JButton( opCodes[i] );
opsPanel.add( ops[i] );
ops[i].addActionListener( listener );
}
bottomPanel.add( digitsPanel );
bottomPanel.add( opsPanel );
content.add( topPanel );
content.add( bottomPanel );
window.setSize( 640,480);
window.setVisible( true );
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
// We are again using an inner class here so that we can access
// components from within the listener. Note the different ways
// of getting the int counts into the String of the label
class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Component whichButton = (Component) e.getSource();
// how to test for which button?
// this is why our widgets are 'global' class members
// so we can refer to them in here
for (int i=0 ; i<10 ; i++ )
if (whichButton == digits[i])
expression.setText( expression.getText() + i );
if (whichButton == digits[10])
expression.setText(" ");
if (whichButton == digits[11])
expression.setText( expression.getText().substring(0, expression.getText().length()-1));
if(whichButton == equals)
evaluation(expression.getText());
for (int i=0 ; i<4 ; i++ )
if (whichButton == ops[i])
expression.setText( expression.getText() + opCodes[i] );
// need to add tests for other controls that may have been
// click that got us in here. Write code to handle those
}
}
public void evaluation(String data)
{
//Need more info here
}
public static void main(String [] args)
{
new SimpleCalc();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.