// declare GUI input omponents
JLabel lblFirstFraction, lblSecondFraction, lblFirstSymbol,
lblSecondSymbol;
JTextField txtFirstNumerator, txtFirstDenominator, txtSecondNumerator,
txtSecondDenominator;
// command buttons for arithmetic operations
JButton cmdAdd, cmdSubtract, cmdMultiply, cmdDivide;
// declare GUI output components
JLabel lblOperationType, lblAnswer;
JTextField txtOperationType, txtAnswer;
// declare echo user inputs components
JLabel lblEchoFirst, lblEchoSecond;
JTextField txtEchoFirst, txtEchoSecond;
// declare panels used to develop GUI
JPanel panOverall;
JPanel panInput;
JPanel panEcho;
JPanel panOutput;
JPanel panButtons;
// declare four global variables related to inputs
boolean validInput;
int firstNumerator = 0;
int firstDenominator = 0;
int secondNumerator = 0;
int secondDenominator = 0;
public void init( )
{
// instantiate the GUI to
hold other panels
JPanel panOverall = new JPanel();
panOverall.setLayout(new GridLayout(4,1));
// create Input Panel
panInput = createInputPanel();
panOverall.add(panInput);
// create Button Panel
panButtons = createButtonPanel();
panOverall.add(panButtons);
// create Echo Panel
panEcho = createEchoPanel();
panOverall.add(panEcho);
// create Output Panel
panOutput = createOutputPanel();
panOverall.add(panOutput);
setContentPane(panOverall);
}
public void actionPerformed(ActionEvent userClick)
{
if (validateInput( ))
{
// instantiating the
first fraction
Rational firstFraction = new Rational(firstNumerator,
firstDenominator);
// echoing the first fraction
txtEchoFirst.setText(firstFraction.toString( ));
// instantiating the second fraction
Rational secondFraction = new Rational(secondNumerator,
secondDenominator);
// echoing the second fraction
txtEchoSecond.setText(secondFraction.toString( ));
// instantiating the Rational to contain
the answer
Rational answerFraction = new Rational( );
// block of code to determine which
operation button the user clicked
// and take the appropriate actions
// if the user clicks on the Add button
if (userClick.getSource( ) == cmdAdd)
{
answerFraction =
answerFraction.Add(firstFraction, secondFraction);
txtOperationType.setText("Addition");
txtAnswer.setText(answerFraction.toString( ));
}
// if the user clicks on the Subtract button
if (userClick.getSource( ) == cmdSubtract)
{
answerFraction =
answerFraction.Subtract(firstFraction, secondFraction);
txtOperationType.setText("Subtraction");
txtAnswer.setText(answerFraction.toString( ));
}
// if the user clicks on the Multiply button
if (userClick.getSource( ) == cmdMultiply)
{
answerFraction =
answerFraction.Multiply(firstFraction, secondFraction);
txtOperationType.setText("Multipication");
txtAnswer.setText(answerFraction.toString());
}
// if the user clicks on the Divide button
if (userClick.getSource( ) == cmdDivide)
{
if (secondFraction.getNumerator( ) != 0)
{
answerFraction =
answerFraction.Divide(firstFraction, secondFraction);
txtOperationType.setText("Division");
txtAnswer.setText(answerFraction.toString( ));
}
else
{
txtOperationType.setText("Division");
txtAnswer.setText("Second numerator cannot = 0");
}
}
}
}
public JPanel createInputPanel( )
{
JPanel panInputGUI = new JPanel( );
panInputGUI.setLayout( new GridLayout(2, 4, 5, 5));
// developing input row for first fraction
lblFirstFraction = new JLabel("First fraction: ",
SwingConstants.RIGHT);
panInputGUI.add(lblFirstFraction);
txtFirstNumerator = new JTextField(3);
panInputGUI.add(txtFirstNumerator);
lblFirstSymbol = new JLabel("/", SwingConstants.CENTER);
panInputGUI.add(lblFirstSymbol);
txtFirstDenominator = new JTextField(3);
panInputGUI.add(txtFirstDenominator);
// developing input row for second fraction
lblSecondFraction = new JLabel("Second fraction: ",
SwingConstants.RIGHT);
panInputGUI.add(lblSecondFraction);
txtSecondNumerator = new JTextField(3);
panInputGUI.add(txtSecondNumerator);
lblSecondSymbol = new JLabel("/", SwingConstants.CENTER);
panInputGUI.add(lblSecondSymbol);
txtSecondDenominator = new JTextField(3);
panInputGUI.add(txtSecondDenominator);
return panInputGUI;
}
public JPanel createButtonPanel( )
{
JPanel panButtonGUI = new JPanel( );
panButtonGUI.setLayout( new GridLayout(1, 4, 5, 5) );
// command button for addition
cmdAdd = new JButton("Add");
cmdAdd.addActionListener(this);
panButtonGUI.add(cmdAdd);
// command button for subtraction
cmdSubtract = new JButton("Subtract");
cmdSubtract.addActionListener(this);
panButtonGUI.add(cmdSubtract);
// command button for addition
cmdMultiply = new JButton("Multiply");
cmdMultiply.addActionListener(this);
panButtonGUI.add(cmdMultiply);
// command button for addition
cmdDivide = new JButton("Divide");
cmdDivide.addActionListener(this);
panButtonGUI.add(cmdDivide);
return panButtonGUI;
}
public JPanel createEchoPanel( )
{
JPanel panEchoGUI = new JPanel( );
panEchoGUI.setLayout( new GridLayout(2, 2, 5, 5) );
// developing output row for universal time
lblEchoFirst = new JLabel("First fraction: ",
SwingConstants.RIGHT);
panEchoGUI.add(lblEchoFirst);
txtEchoFirst = new JTextField(10);
txtEchoFirst.setEditable(false);
panEchoGUI.add(txtEchoFirst);
// developing output row for universal time
lblEchoSecond = new JLabel("Second fraction: ",
SwingConstants.RIGHT);
panEchoGUI.add(lblEchoSecond);
txtEchoSecond = new JTextField(10);
txtEchoSecond.setEditable(false);
panEchoGUI.add(txtEchoSecond);
return panEchoGUI;
}
public JPanel createOutputPanel( )
{
JPanel panOutputGUI = new JPanel( );
panOutputGUI.setLayout( new GridLayout(2, 2, 5, 5) );
// developing output row for universal time
lblOperationType = new JLabel("You pressed: ",
SwingConstants.RIGHT);
panOutputGUI.add(lblOperationType);
txtOperationType = new JTextField(10);
txtOperationType.setEditable(false);
panOutputGUI.add(txtOperationType);
// developing output row for universal time
lblAnswer = new JLabel("The solution: ", SwingConstants.RIGHT);
panOutputGUI.add(lblAnswer);
txtAnswer = new JTextField(10);
txtAnswer.setEditable(false);
panOutputGUI.add(txtAnswer);
return panOutputGUI;
}
public boolean validateInput( )
{
String firstErrorMessage = "";
String secondErrorMessage = "";
validInput = true;
try
{
firstNumerator =
Integer.parseInt(txtFirstNumerator.getText());
}
catch (NumberFormatException nfeInteger)
{
// give error message when
input can't be parsed to integer
firstErrorMessage = firstErrorMessage + "You need to enter an
integer for the first numerator\n";
// set boolean variable to false when
// received an invalid input
validInput = false;
// blanking out inputs and any previous
outputs
// when input is invalid
txtFirstNumerator.setText("");
}
try
{
firstDenominator =
Integer.parseInt(txtFirstDenominator.getText( ));
}
catch (NumberFormatException nfeInteger)
{
// give error message when
input can't be parsed to integer
firstErrorMessage = firstErrorMessage + "You need to enter an
integer for the first denominator\n";
// set boolean variable to false when
// received an invalid input
validInput = false;
// blanking out inputs and any previous
outputs
// when input is invalid
txtFirstDenominator.setText("");
}
if (firstDenominator == 0)
{
// give error message when
input can't be parsed to integer
firstErrorMessage = firstErrorMessage + "You cannot have a
zero for the first denominator\n";
// set boolean variable to false when
// received an invalid input
validInput = false;
// blanking out inputs and any previous
outputs
// when input is invalid
txtFirstDenominator.setText("");
}
try
{
secondNumerator =
Integer.parseInt(txtSecondNumerator.getText( ));
}
catch (NumberFormatException nfeInteger)
{
// give error message when
input can't be parsed to integer
secondErrorMessage = secondErrorMessage + "You need to enter
an integer for the second numerator\n";
// set boolean variable to false when
// received an invalid input
validInput = false;
// blanking out inputs and any previous
outputs
// when input is invalid
txtSecondNumerator.setText("");
}
try
{
secondDenominator =
Integer.parseInt(txtSecondDenominator.getText( ));
}
catch (NumberFormatException nfeInteger)
{
// give error message when
input can't be parsed to integer
secondErrorMessage = secondErrorMessage + "You need to enter
an integer for the second denominator\n";
// set boolean variable to false when
// received an invalid input
validInput = false;
// blanking out inputs and any previous
outputs
// when input is invalid
txtSecondDenominator.setText("");
}
if (secondDenominator == 0)
{
// give error message when
input can't be parsed to integer
secondErrorMessage = secondErrorMessage + "You cannot have a
zero for the second denominator\n";
// set boolean variable to false when
// received an invalid input
validInput = false;
// blanking out inputs and any previous
outputs
// when input is invalid
txtSecondDenominator.setText("");
}
if (!validInput)
{
JOptionPane.showMessageDialog(null,
firstErrorMessage + "\n" + secondErrorMessage,
"Input Error", JOptionPane.ERROR_MESSAGE);
txtEchoFirst.setText("");
txtEchoSecond.setText("");
txtOperationType.setText("");
txtAnswer.setText("");
}
return validInput;
}