import java.awt.*;
import java.awt.event.*;
import javax.swing.* ;
import javax.swing.JOptionPane ;
import java.io.*;
public class Calc extends JFrame
implements ActionListener {
private JButton One, Two, Three, Four, Five,
Six, Seven, Eight, Nine, Zero, bExit,
bMult, bDiv, bSub, bPlus, bEquals, bHelp,
bAbout, bDecPt, bClear ;
private JTextField enterBox ;
private JPanel textPanel, exitPanel, buttonPanel, functionPanel ;
private Container c ;
private Font font;
private String firstNum = new String(""), secondNum = new String(""), tempStr ;
private boolean myDiag = false, result = true ,
firstNumSet = false, secondNumSet = false,
pendingOp = false ;
private double aNum, dNum1 = 0.0, dNum2 = 0.0 , answer = 0.0,
tempD = 0.0 , minusOne = -1.0 ;
private double dArray[] = new double[ 10 ] ;
private int opCode = -1, tempInt = 0, tempInt2 = 0,
dArrayPtr = 0, pendingOpCode = -1 ;
Calc() {
super( "Requested Calculator!!" );
setup() ;
setSize( 280, 230 );
setLocation( 200, 200 ) ;
show();
}
private void setup() {
c = getContentPane() ;
setUpTextField() ;
setUpButtons() ;
setUpFunctionPanel() ;
;
setUpActionListener() ;
}
public void setUpTextField() {
enterBox = new JTextField( 15 ) ;
enterBox.setText( "??" );
enterBox.setEditable( false );
enterBox.setBackground( Color.white );
enterBox.setHorizontalAlignment( JTextField.RIGHT );
enterBox.addKeyListener(
new KeyAdapter() {
public void keyPressed ( KeyEvent e ) {
if ( result ) {
MyPrint( "The value of result is " + result +
"this is from the enterbox keylistener." );
result = false ;
enterBox.setText( "" );
}
}
}
);
textPanel = new JPanel() ;
textPanel.add(enterBox ) ;
c.add( textPanel , BorderLayout.NORTH ) ;
}
public void setUpButtons() {
buttonPanel = new JPanel() ;
c.add( buttonPanel , BorderLayout.CENTER ) ;
Zero = new JButton( "0" ) ;
Zero.setFont( new Font("Sanserif", Font.BOLD, 16 ) );
One = new JButton( "1" ) ;
Two = new JButton( "2" ) ;
Three = new JButton( "3" ) ;
Four = new JButton( "4" ) ;
Five = new JButton( "5" ) ;
Six = new JButton( "6" ) ;
Seven = new JButton( "7" ) ;
Eight = new JButton( "8" ) ;
Nine = new JButton( "9" ) ;
bClear = new JButton( "Clear" ) ;
bClear.setBackground( Color.blue ) ;
bClear.setForeground( Color.white );
bMult = new JButton( "*" ) ;
bMult.setFont( new Font("Sanserif", Font.BOLD, 20 ) );
bMult.setBackground( Color.blue ) ;
bMult.setForeground( Color.white );
bDiv = new JButton( "/" ) ;
bDiv.setFont( new Font("Sanserif", Font.BOLD, 20 ) );
bDiv.setBackground( Color.blue ) ;
bDiv.setForeground( Color.white );
bSub = new JButton( "-" ) ;
bSub.setFont( new Font("Sanserif", Font.BOLD, 24 ) );
bSub.setBackground( Color.blue ) ;
bSub.setForeground( Color.white );
bPlus = new JButton( "+" ) ;
bPlus.setBackground( Color.blue ) ;
bPlus.setForeground( Color.white );
bEquals = new JButton( "=" ) ;
bEquals.setFont( new Font("Sanserif", Font.BOLD, 16 ) );
bEquals.setBackground( Color.red ) ;
bEquals.setForeground( Color.white );
bDecPt = new JButton( "." ) ;
bDecPt.setFont( new Font("Sanserif", Font.BOLD, 22 ) );
buttonPanel.add( Seven ) ;
buttonPanel.add( Eight ) ;
buttonPanel.add( Nine ) ;
buttonPanel.add( Four ) ;
buttonPanel.add( Five ) ;
buttonPanel.add( Six ) ;
buttonPanel.add( One ) ;
buttonPanel.add( Two ) ;
buttonPanel.add( Three ) ;
buttonPanel.add( Zero ) ;
buttonPanel.add( bDecPt ) ;
buttonPanel.add( bEquals ) ;
buttonPanel.setLayout( new GridLayout( 5, 3, 5, 5 ) );
}
public void setUpActionListener() {
bDecPt.addActionListener( this );
Zero.addActionListener( this );
One.addActionListener( this );
Two.addActionListener( this );
Three.addActionListener( this );
Four.addActionListener( this );
Five.addActionListener( this );
Six.addActionListener( this );
Seven.addActionListener( this );
Eight.addActionListener( this );
Nine.addActionListener( this );
bMult.addActionListener( this );
bDiv.addActionListener( this );
bSub.addActionListener( this );
bPlus.addActionListener( this );
bEquals.addActionListener( this );
bClear.addActionListener( this );
}
public void setUpFunctionPanel() {
functionPanel = new JPanel() ;
c.add( functionPanel , BorderLayout.EAST ) ;
functionPanel.setLayout( new GridLayout( 6, 1, 5, 3 ) );
functionPanel.add( bMult ) ;
functionPanel.add( bDiv ) ;
functionPanel.add( bSub ) ;
functionPanel.add( bPlus ) ;
functionPanel.add( bClear ) ;
}
public void setUpExitPanel() {
exitPanel = new JPanel() ;
c.add( exitPanel , BorderLayout.SOUTH ) ;
bExit = new JButton( "Exit" ) ;
bExit.setFont( new Font("Sanserif", Font.BOLD, 16 ) );
bExit.setBackground( Color.red ) ;
bExit.setForeground( Color.white );
bHelp = new JButton( "Help" ) ;
bHelp.setFont( new Font("Sanserif", Font.BOLD, 16 ) );
bHelp.setBackground( Color.blue ) ;
bHelp.setForeground( Color.white );
bAbout = new JButton( "About" ) ;
bAbout.setFont( new Font("Sanserif", Font.BOLD, 14 ) );
bAbout.setBackground( Color.darkGray ) ;
bAbout.setForeground( Color.white );
exitPanel.add( bExit ) ;
exitPanel.add( bHelp ) ;
exitPanel.add( bAbout ) ;
}
public void actionPerformed( ActionEvent e ) {
if ( result ) {
MyPrint( "The value of result is " + result );
result = false ;
enterBox.setText( "" );
}
if ( e.getSource() == bDecPt ) {
MyPrint( "The decimal point button was pressed." );
enterBox.setText( enterBox.getText() + "." ) ;
}
else if ( e.getSource() == Zero ) {
MyPrint( "The zero button was pressed. And pendingOp is " + pendingOp );
enterBox.setText( enterBox.getText() + "0" ) ;
}
else if ( e.getSource() == One ) {
MyPrint( "The one button was pressed. And pendingOp is " + pendingOp );
enterBox.setText( enterBox.getText() + "1" ) ;
}
else if ( e.getSource() == Two ) {
MyPrint( "The two button was pressed. And pendingOp is " + pendingOp );
enterBox.setText( enterBox.getText() + "2" ) ;
}
else if ( e.getSource() == Three ) {
MyPrint( "The Three button was pressed. And pendingOp is " + pendingOp);
enterBox.setText( enterBox.getText() + "3" ) ;
}
else if ( e.getSource() == Four ) {
MyPrint( "The Four button was pressed. And pendingOp is " + pendingOp );
enterBox.setText( enterBox.getText() + "4" ) ;
}
else if ( e.getSource() == Five ) {
MyPrint( "The Five button was pressed. And pendingOp is " + pendingOp );
enterBox.setText( enterBox.getText() + "5" ) ;
}
else if ( e.getSource() == Six ) {
MyPrint( "The Six button was pressed. And pendingOp is " + pendingOp );
enterBox.setText( enterBox.getText() + "6" ) ;
}
else if ( e.getSource() == Seven ) {
MyPrint( "The Seven button was pressed. And pendingOp is " + pendingOp );
enterBox.setText( enterBox.getText() + "7" ) ;
}
else if ( e.getSource() == Eight ) {
MyPrint( "The Eight button was pressed. And pendingOp is " + pendingOp );
enterBox.setText( enterBox.getText() + "8" ) ;
}
else if ( e.getSource() == Nine ) {
MyPrint( "The Nine button was pressed. And pendingOp is " + pendingOp );
enterBox.setText( enterBox.getText() + "9" ) ;
}
else if ( e.getSource() == bMult ) {
if ( !pendingOp ) {
if ( !getFirstNum( ) || !firstNumSet ) {
JOptionPane.showMessageDialog( this, "You have not entered a first number." ,
"First number blank",
JOptionPane.ERROR_MESSAGE );
}
else {
opCode = 0 ;
pendingOpCode = 0 ;
pendingOp = true ;
firstNumSet = true ;
result = true ;
MyPrint( "The pendingOp is " + pendingOp );
}
}
else {
JOptionPane.showMessageDialog( this, "You have a pending operation." ,
"Pending Operation",
JOptionPane.ERROR_MESSAGE );
opCode = pendingOpCode ;
pendingOp = true ;
firstNumSet = true ;
result = true ;
}
MyPrint( "The Mult button was pressed. And pendingOp is " + pendingOp );
}
else if ( e.getSource() == bDiv ) {
if ( !pendingOp ) {
if ( !getFirstNum( ) || !firstNumSet ) {
JOptionPane.showMessageDialog( this, "You have not entered a first number." ,
"First number blank",
JOptionPane.ERROR_MESSAGE );
}
else if ( getFirstNum( ) ) {
opCode = 1 ;
pendingOpCode = 1 ;
pendingOp = true ;
firstNumSet = true ;
result = true ;
}
}
else {
JOptionPane.showMessageDialog( this, "You have a pending operation." ,
"Pending Operation",
JOptionPane.ERROR_MESSAGE );
opCode = pendingOpCode ;
pendingOp = true ;
firstNumSet = true ;
result = true ;
}
MyPrint( "The Div button was pressed. And pendingOp is " + pendingOp );
}
else if ( e.getSource() == bSub ) {
if ( !pendingOp ) {
MyPrint( "The Sub button was pressed. And pendingOp is " + pendingOp );
if ( !getFirstNum( ) || !firstNumSet ) {
JOptionPane.showMessageDialog( this, "You have not entered a first number." ,
"First number blank",
JOptionPane.ERROR_MESSAGE );
}
else if ( getFirstNum( ) ) {
opCode = 2 ;
pendingOpCode = 2 ;
pendingOp = true ;
firstNumSet = true ;
result = true ;
}
}
else {
JOptionPane.showMessageDialog( this, "You have a pending operation." ,
"Pending Operation",
JOptionPane.ERROR_MESSAGE );
opCode = pendingOpCode ;
pendingOp = true ;
firstNumSet = true ;
result = true ;
}
}
else if ( e.getSource() == bPlus ) {
if ( !pendingOp ) {
MyPrint( "The Plus button was pressed. And pendingOp is " + pendingOp );
if ( !getFirstNum( ) || !firstNumSet ) {
JOptionPane.showMessageDialog( this, "You have not entered a first number." ,
"First number blank",
JOptionPane.ERROR_MESSAGE );
}
else if ( getFirstNum( ) ) {
opCode = 3 ;
pendingOpCode = 3 ;
pendingOp = true ;
result = true ;
}
}
else {
JOptionPane.showMessageDialog( this, "You have a pending operation." ,
"Pending Operation",
JOptionPane.ERROR_MESSAGE );
opCode = pendingOpCode ;
pendingOp = true ;
firstNumSet = true ;
result = true ;
}
}
else if ( e.getSource() == bEquals ) {
MyPrint( "The Equals button was pressed. And pendingOp is " + pendingOp +
" The opCode value is " + opCode );
if ( enterBox.getText().equals( "" ) || !firstNumSet ) {
JOptionPane.showMessageDialog( this, "Equals a: You have not entered a first number." ,
"First number blank",
JOptionPane.ERROR_MESSAGE );
}
else if ( !pendingOp ) {
enterBox.setText( "" ) ;
JOptionPane.showMessageDialog( this, "Equals b: You have not entered a first number and/or an operation." ,
"No operation entered",
JOptionPane.ERROR_MESSAGE );
}
else if ( pendingOp ) {
opCodeMethod() ;
}
firstNum = "" + answer ;
firstNumSet = true ;
}
else if ( e.getSource() == bClear ) {
MyPrint( "The Clear button was pressed." );
firstNum = "" ;
secondNum = "" ;
firstNumSet = false ;
opCode = -1 ;
pendingOpCode = -1 ;
pendingOp = false ;
}
}
private boolean getFirstNum( ) {
boolean retCode = false ;
if ( !firstNumSet ) {
if ( !enterBox.getText().equals( "" ) ) {
firstNum = enterBox.getText() ;
firstNumSet = true ;
result = true ;
retCode = true ;
MyPrint( "1a- In checkFirstNum( ) -- firstNum =s " + firstNum);
}
else if ( enterBox.getText().equals( "" ) ) {
MyPrint( "1b- In checkFirstNum( ) firstNumSet is " + enterBox.getText() );
JOptionPane.showMessageDialog( this, "The first number cannot be blank." ,
"Invalid Number",
JOptionPane.ERROR_MESSAGE );
retCode = false ;
}
}
else if ( firstNumSet ) {
firstNumSet = true ;
result = true ;
retCode = true ;
}
else if ( enterBox.getText().equals( "" ) ) {
MyPrint( "1c- In checkFirstNum( ) firstNumSet is " + enterBox.getText() );
JOptionPane.showMessageDialog( this, "The first number =s " +
enterBox.getText() + " cannot be blank." ,
"Invalid Number",
JOptionPane.ERROR_MESSAGE );
retCode = false ;
}
MyPrint( "1d- In checkFirstNum( ): retCode is " + retCode ) ;
return retCode ;
}
private void setResults() {
enterBox.setText( "" + answer );
firstNum = "" + answer ;
firstNumSet = true ;
result = true ;
pendingOp = false ;
}
private void opCodeMethod() {
int currentOpCode = -1 ;
MyPrint( "In opCodeMethod() with an opCode of " + opCode );
if ( pendingOp ) {
currentOpCode = pendingOpCode ;
pendingOp = false ;
}
else {
currentOpCode = opCode ;
}
switch ( currentOpCode ) {
case 0 :
multOp() ;
break ;
case 1 :
divOp() ;
break ;
case 2 :
MyPrint( "opCodeMethod(): opCode =s " + currentOpCode ) ;
subOp() ;
break ;
case 3 :
plusOp() ;
break ;
case 4 :
break ;
case 5 :
break ;
default :
setResults() ;
secondNum = "0.0" ;
dNum2 = 0.0 ;
MyPrint( "Case default: firstNum =s " + firstNum);
break ;
}
opCode = -1 ;
}
private void multOp() {
if ( opCode == 0 ) {
pendingOp = false ;
secondNum = enterBox.getText() ;
pendingOp = true ;
MyPrint( "2a- multOp(): opCode is " + opCode + " The value of secondNum is " + secondNum );
if ( secondNum.equals( "" ) ) {
secondNum = "1.0" ;
MyPrint( "2b- multOp(): opCode is " + opCode + " The value of secondNum is " + secondNum );
}
MyPrint( "3a- multOp(): opCode is " + opCode + " secondNum is " + secondNum ) ;
convertNumsToDouble() ;
answer = dNum1 * dNum2 ;
setResults() ;
MyPrint( "3b- multOp(): opCode is " + opCode + " The value of answer is " + answer );
dNum1 = answer ;
secondNum = "0.0" ;
secondNumSet = true ;
}
}
private void divOp() {
secondNum = enterBox.getText() ;
MyPrint( "2a- divOp(): opCode is " + opCode + " firstNum is " +
firstNum + " secondNum is " + secondNum ) ;
if ( secondNum.equals( "" ) || opCode < 0 ) {
MyPrint( "2b- divOp(): opCode is " + opCode + " firstNum is " +
firstNum + " secondNum is " + secondNum ) ;
}
else {
convertNumsToDouble() ;
if (dNum2 != 0.0 ) {
answer = dNum1 / dNum2 ;
setResults() ;
MyPrint( "2- divOp(): opCode is " + opCode + " firstNum is " +
firstNum + " secondNum is " + secondNum ) ;
}
else {
JOptionPane.showMessageDialog( this, "The number " +
dNum2 + " cannot be used as a denometer" ,
"Attempted to Divide by Zero",
JOptionPane.ERROR_MESSAGE );
firstNumSet = false ;
firstNum = "" ;
opCode = -1 ;
}
}
opCode = 1 ;
result = true ;
}
private void subOp() {
if ( opCode == 2 ) {
pendingOp = false ;
secondNum = enterBox.getText() ;
if ( secondNum.equals( "" ) ) {
secondNum = "0.0" ;
}
MyPrint( "2a- subOp(): opCode is " + opCode + " secondNum is " + secondNum ) ;
convertNumsToDouble() ;
answer = dNum1 - dNum2 ;
setResults() ;
MyPrint( "2b- subOp(): opCode is " + opCode + " The value of answer is " + answer );
secondNum = "0.0" ;
opCode = -1 ;
secondNumSet = true ;
}
}
private void plusOp() {
if ( pendingOp ) {
opCodeMethod() ;
opCode = 3 ;
result = true ;
pendingOp = true ;
MyPrint( "plusOp() 1a: In pendingOp code. the opCode value is " + opCode) ;
}
else if ( !firstNumSet && getFirstNum() ) {
MyPrint( "1- plusOp(): opCode is " + opCode + " The value of answer is " + answer );
pendingOp = true ;
pendingOpCode = 3 ;
result = true ;
opCode = 3 ;
}
else if ( opCode == 3 ) {
secondNum = enterBox.getText() ;
if ( secondNum.equals( "" ) ) {
secondNum = "0.0" ;
}
MyPrint( "2a- plusOp(): opCode is " + opCode + " secondNum is " + secondNum ) ;
convertNumsToDouble() ;
answer = dNum1 + dNum2 ;
setResults() ;
MyPrint( "2b- plusOp(): opCode is " + opCode + " The value of answer is " + answer );
secondNum = "" ;
opCode = -1 ;
dNum2 = 0.0 ;
dNum1 = answer ;
secondNumSet = false ;
result = true ;
firstNum = "" + answer ;
enterBox.setText( firstNum ) ;
firstNumSet = true ;
pendingOp = false ;
}
else {
pendingOp = true ;
pendingOpCode = 3 ;
opCode = 3 ;
result = true ;
MyPrint( "3a- plusOp(): opCode is " + opCode + " The value of answer is " + answer );
}
}
private void convertNumsToDouble() {
if ( !firstNum.equals( "" ) )
dNum1 = Double.parseDouble( firstNum ) ;
if ( !secondNum.equals( "" ) )
dNum2 = Double.parseDouble( secondNum ) ;
MyPrint( "convertNumsToDouble(): The value of dNum1 is " + dNum1 );
MyPrint( "convertNumsToDouble(): The value of dNum2 is " + dNum2 );
}
private void sysExit() {
System.exit( 0 );
}
private void MyPrint( String str ) {
if ( myDiag )
System.out.println( str );
}
public static void main( String args[] ) {
final Calc app = new Calc () ;
app.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
app.sysExit() ;
}
}
);
}
} |