A Class for Working with Fractions

 

Background.  This next class is starting to get more involved and useful.  You should remember working with fractions from your earlier schooling.  Remember, fractions are represented by

a/b = numerator/denominator

You learned to do things such as add, subtract, multiply and divide fractions.  You also learned to reduce them to lowest terms so that the numerator and denominator did not have any common factors.  These are the operations that will concern us in this webpage.

But the other thing to remember, is that you learned a very visual approach to working with fractions.  Solutions were written on paper and blackboards.  Doing these same sorts of things on a computer requires some modifications to your thinking.  We are not going to represent fractions in their usual form within the computer.  This would require significantly more overhead and ability than we have at present.

Since we are presently limited to making use of methods and their notation, we will use a two-tuple to represent a fraction in our user defined class.

Rational(numerator, denominator)

Then we will construct methods for

  • adding
  • subtracting
  • multiplying
  • dividing
  • reducing
  • writing them to a string

Now you want to copy the following code into its own directory.  Notice we are going to create a package so you need to have a RationalClasses subdirectory of your javaPackages directory.  You should call the following file Rational.java.

 

package javaPackages.RationalClasses;

public class Rational extends Object
{

private int numerator;
private int denominator;

public Rational( )
{

setRational(0,0);

}

public Rational(int num, int denom)
{

setRational(num, denom);

}

public void setRational(int num, int denom)
{

numerator = num;
denominator = denom;

}

public void setNumerator(int num)
{

numerator = num;

}

public void setDenominator(int denom)
{

denominator = denom;

}

public int getNumerator( )
{

return numerator;

}

public int getDenominator( )
{

return denominator;

}

public void ReduceForm(int a, int b)
{

// set array with prime numbers
int primes[] = {2,3,5,7,11,13,17,19,23};

for(int counter = 0; counter < primes.length; counter++)
{

//  while a particular prime divides into both the numerator and
//  denominator divide it out of both
while ((a % primes[counter] == 0) && (b % primes[counter] == 0))
{

a = (a / primes[counter]);
b = (b / primes[counter]);

}

}
setRational(a, b);

}

public Rational Add( Rational firstRational, Rational secondRational)
{

Rational result = new Rational( );

result.setNumerator((firstRational.getNumerator() * secondRational.getDenominator()) + (secondRational.getNumerator() * firstRational.getDenominator()));
result.setDenominator(firstRational.getDenominator() * secondRational.getDenominator());

result.ReduceForm(result.getNumerator(), result.getDenominator());

// now set all the stuff
numerator = result.getNumerator();
denominator = result.getDenominator();

return result;

}

public Rational Subtract( Rational firstRational, Rational secondRational)
{

Rational result = new Rational( );

result.setNumerator((firstRational.getNumerator() * secondRational.getDenominator()) - (secondRational.getNumerator() * firstRational.getDenominator()));
result.setDenominator(firstRational.getDenominator() * secondRational.getDenominator());

result.ReduceForm(result.getNumerator(), result.getDenominator());

// now set all the stuff
numerator = result.getNumerator( );
denominator = result.getDenominator( );

return result;

}

public Rational Multiply( Rational firstRational, Rational secondRational)
{

Rational result = new Rational( );

result.setNumerator(firstRational.getNumerator( ) * secondRational.getNumerator( ));
result.setDenominator(firstRational.getDenominator( ) * secondRational.getDenominator( ));

result.ReduceForm(result.getNumerator(), result.getDenominator());

numerator = result.getNumerator( );
denominator = result.getDenominator( );

return result;

}

public Rational Divide( Rational firstRational, Rational secondRational)
{

Rational result = new Rational( );

result.setNumerator(firstRational.getNumerator( ) * secondRational.getDenominator( ));
result.setDenominator(firstRational.getDenominator( ) * secondRational.getNumerator( ));

result.ReduceForm(result.getNumerator( ), result.getDenominator( ));

numerator = result.getNumerator( );
denominator = result.getDenominator( );

return result;

}

public String toString( )
{

return numerator + "/" + denominator;

}

}

 

Since we are creating a package, first you need to create a RationalClasses folder within the javaPackages directory.  Then you should compile this program with

javac -d C:\j2sdk1.4.0\jre\classes Rational.java

We start with a discussion of the code and methods.

  • package javaPackages.RationalClasses

  • the overall inclusive class is called Rational that extends Object

    • declare an integer instance variable numerator

    • declare an integer instance variable denominator

 

  • define a constructor Rational( )

    • receives no arguments

    • uses a later method setRational( ) to initialize the numerator and denominator to zero

 

  • define a constructor Rational( )

    • receives two integer arguments num and denom

    • uses a later method setRational( ) to initialize the numerator of the object to num and the denominator of the object to denom

 

  • a setRational( ) method is declared and developed
    • it receives two integer arguments
      • num for the numerator
      • denom for the denominator
    • it assigns the instance variable numerator equal to the num that is passed and the denominator equal to the denom that is passed

 

  • a setNumerator( ) method is declared and developed
    • it receives a single integer argument
      • num for the numerator
    • it assigns the instance variable numerator equal to the num that is passed

 

  • a setDenominator( ) method is declared and developed
    • it receives a single integer argument
      • denom for the denominator
    • it assigns the instance variable denominator equal to the denom that is passed

 

  • a getNumerator( ) method is declared and developed
    • it receives no arguments
    • it returns the numerator of the object

 

  • a getDenominator( ) method is declared and developed
    • it receives no arguments
    • it returns the denominator of the object

 

  • a  ReduceForm( ) method is declared and developed
    • it receives two arguments
      • an integer a  (basically represents the numerator)
      • an integer b  (basically represents the denominator)
    • it declares an array of the first 9 prime numbers, though this could be easily increased without impact on the functioning of the code
    • a for loop is used to go incrementally through this array of primes
      • a while loop continues based on whether each prime divides both a and b (the numerator and denominator)
        • if it does then this prime is divided out of both a and b
    • after having looped through the entire array the Rational is set to the reduced a and b

 

  • an Add( ) method is declared and developed
    • it receives two arguments
      • an object of type Rational
      • another object of type rational
    • it returns a Rational representing their sum
    • it makes use of an approach called "cross multiplying" to get the sum and then it reduces this using the ReduceForm( ) method
    • make sure to notice the use of the get and set methods to retrieve and work with the numerators and denominators

 

  • a Subtract( ) method is declared and developed
    • it receives two arguments
      • an object of type Rational
      • another object of type rational
    • it returns a Rational representing their sum
    • it makes use of an approach called "cross multiplying" to get the sum and then it reduces this using the ReduceForm( ) method
    • make sure to notice the use of the get and set methods to retrieve and work with the numerators and denominators

 

  • a Multiply( ) method is declared and developed
    • it receives two arguments
      • an object of type Rational
      • another object of type rational
    • it returns a Rational representing their sum
    • it makes use of the usual approach of
      • multiplying numerators times each other to get the product's numerator
      • multiplying denominators times each other to get the product's denominator
      • reduces this using the ReduceForm( ) method
    • make sure to notice the use of the get and set methods to retrieve and work with the numerators and denominators

 

  • a Divide( ) method is declared and developed
    • it receives two arguments
      • an object of type Rational
      • another object of type rational
    • it returns a Rational representing their sum
    • it makes use of the usual approach of
      • multiplying the first numerator times the second denominator to get the quotient's numerator
      • multiplying the first denominator times the second numerator to get the quotient's denominator
      • reduces this using the ReduceForm( ) method
    • make sure to notice the use of the get and set methods to retrieve and work with the numerators and denominators

 

  • a toString( ) method is declared and developed
    • returns the numerator/denominator as a string

 

After compilation you should notice where the class is located.

Now you need the following RationalTestGUI.java to construct/instantiate Rational objects through a calculator type interface and allow the user to add, subtract, multiply and divide them.

 

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javaPackages.RationalClasses.Rational;

public class RationalTestGUI extends JApplet implements ActionListener
{

// 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;

}

}

 

You do not need to specify any special path in the compilation command.

javac RationalTestGUI.java

You should run this code and play with the GUI some before we dig into it.

The HTML file follows and should be called RationalTestGUI.html.

 

<html>
<applet code="RationalTestGUI.class" width=400 height=400>
</applet>
</html>

 

If you try to click on the command button and don't have an appropriate input you should see something like the following.  For this example I put a/4 and 3/0 in for the first and second fractions.

 

 

With acceptable numbers in the length and width text fields you should see something more like the following.

 

 

Notice how the solution is in reduced form even though the input fractions are not.

Now we will discuss the code.  NEED TO REWRITE FROM HERE!!!

  • we need to import the javax.swing.* package
  • import java.awt.*
  • import java.awt.event.*
  • import the package in C:\j2sdk1.4.0\jre\classes\javaPackages\RationalClasses for the Rational class.

 

  • the overall inclusive class is called RationalTestGUI which extends JApplet and implements ActionListener
    • declare the input GUI components
      • a label for inputting the first fraction = lblFirstFraction
      • a text field for inputting the first fraction = txtFirstFraction
      • a label for inputting the second fraction = lblSecondFraction
      • a text field for inputting the second fraction = txtSecondFraction
      • a label for the division/fraction symbol lblFirstSymbol
      • a label for the division/fraction symbol lblSecondSymbol
      • a text field for inputting the first numerator = txtFirstNumerator
      • a text field for inputting the first denominator = txtFirstDenominator
      • a text field for inputting the second numerator = txtSecondNumerator
      • a text field for inputting the second denominator = txtSecondDenominator
      • a button to cause the program to add the fractions = cmdAdd
      • a button to cause the program to subtract the fractions = cmdSubtract
      • a button to cause the program to multiply the fractions = cmdMultiply
      • a button to cause the program to divide the fractions = cmdDivide
    • declare the output GUI components
      • a label to denote that the user's input for the first fraction are being echoed = lblEchoFirst
      • a text field to echo the first fraction = txtEchoFirst
      • a label to denote that the user's input for the second fraction are being echoed = lblEchoSecond
      • a text field to echo the second fraction = txtEchoSecond
    • declare the panels to be used to construct the GUI
      • a panel to contain the GUI called panOverall
      • a panel for developing the input components of the GUI called panInput
      • a panel for echoing the user's inputs called panEcho
      • a panel for the buttons called panButtons
      • a panel for developing the output components of the GUI called panOutput
    • declare the variables associated with user inputs
      • a boolean validInput to indicate whether the user's inputs pass the program's validity tests
      • initialize and declare a variable for the user's first numerator = firstNumerator
      • initialize and declare a variable for the user's first denominator = firstDenominator
      • initialize and declare a variable for the user's second numerator = secondNumerator
      • initialize and declare a variable for the user's second denominator = secondDenominator

 

  • the init( ) method
    • instantiates a panel to be the panel that contains all the other panels called panGUI
    • gives it a GridLayout with four rows and one column
    • calls the method to create the input panel called createInputPanel( )
    • adds this panel to panOverall
    • calls the method to create the button panel called createButtonPanel( )
    • adds this panel to panOverall
    • calls the method to create the echo panel called createEchoPanel( )
    • adds this panel to panOverall
    • calls the method to create the output panel called createOutputPanel( )
    • adds this panel to panOverall
    • sets the content pane for the applet to be panOverall

 

  • the actionPerformed( ) method
    • calls the validateInput( ) method to retrieve and validate the user's input
    • if the input is acceptable
      • creates a new instance of a Rational object called firstFraction set by the user's inputs
      • echoes the input in the txtEchoFirst text field
        • casts it to a string using the Rational class's toString( ) method
        • sets the text using the built in setText( ) method
      • creates a new instance of a Rational object called secondFraction set by the user's inputs
      • echoes the input in the txtEchoSecond text field
        • casts it to a string using the Rational class's toString( ) method
        • sets the text using the built in setText( ) method
      • creates a new instance of a Rational object called answerFraction
      • determines the source of the user's click
      • if the user has clicked the cmdAdd button
        • invokes the Add method in the Rational class
        • sets the text in the txtOperationType text field to Addition
        • displays the answerFraction
          • uses the Rational class's toString( ) method to cast the answerFraction to a string
          • displays it in the txtAnswer text field using the setText( ) method
      • if the user has clicked the cmdSubtract button
        • invokes the Subtract method in the Rational class
        • sets the text in the txtOperationType text field to Subtraction
        • displays the answerFraction
          • uses the Rational class's toString( ) method to cast the answerFraction to a string
          • displays it in the txtAnswer text field using the setText( ) method
      • if the user has clicked the cmdMultiply button
        • invokes the Multiply method in the Rational class
        • sets the text in the txtOperationType text field to Multiplication
        • displays the answerFraction
          • uses the Rational class's toString( ) method to cast the answerFraction to a string
          • displays it in the txtAnswer text field using the setText( ) method
      • if the user has clicked the cmdDivide button
        • checks to see if the numerator of the second fraction is zero
        • if it's not
          • invokes the Divide method in the Rational class
          • sets the text in the txtOperationType text field to Division
          • displays the answerFraction
            • uses the Rational class's toString( ) method to cast the answerFraction to a string
            • displays it in the txtAnswer text field using the setText( ) method
        • if it is zero
          • sets the text in the txtOperationType text field to Division
          • displays the answerFraction
            • displays "Second numerator cannot = 0" in the txtAnswer text field using the setText( ) method

 

  • createInputPanel( )
    • creates the panel to contain the input components
    • sets the layout to a GridLayout with two rows and four columns
    • instantiates the appropriate JLabels and JTextFields
    • adds them to the panel in the appropriate order
    • attempts to put a  /  in the appropriate space to help represent a fraction
    • returns this panel to the calling location

 

  • createButtonPanel( )
    • creates the panel to contain the button components
    • sets the layout to a GridLayout with one rows and four columns
    • instantiates the appropriate JButtons
    • adds them to the panel in the appropriate order
    • returns this panel to the calling location

 

  • createEchoPanel( )
    • creates the panel to contain the input components
    • sets the layout to a GridLayout with two rows and two columns
    • instantiates the appropriate JLabels and JTextFields
    • adds them to the panel in the appropriate order
    • returns this panel to the calling location

 

  • createOutputPanel( )
    • creates the panel to contain the input components
    • sets the layout to a GridLayout with two rows and four columns
    • instantiates the appropriate JLabels and JTextFields
    • sets the text field's editability property to false so the user can't change them directly
    • adds them to the panel in the appropriate order
    • returns this panel to the calling location

 

  • the validateInput( ) method
    • initializes an error message for the inputs of the first fraction to a blank = firstErrorMessage
    • initializes an error message for the inputs of the second fraction to a blank = secondErrorMessage
    • initializes validInput to true
    • uses a try block to
      • get the firstNumerator from the user
      • parse it as an integer
    • uses a catch block if it can't be parsed as an integer to
      • appends an error to the firstErrorMessage
      • set validInput to false
      • set the txtFirstNumerator to blank
    • uses a try block to
      • get the firstDenominator from the user
      • parse it as an integer
    • uses a catch block if it can't be parsed as an integer to
      • appends an error to the firstErrorMessage
      • set validInput to false
      • set the txtFirstDenominator to blank
    • uses an if block to test if the firstDenominator is zero
      • if it is then it adds on an appropriate message to the firstErrorMessage
      • set validInput to false
      • set txtFirstDenominator to blank
    • uses a try block to
      • get the secondNumerator from the user
      • parse it as an integer
    • uses a catch block if it can't be parsed as an integer to
      • appends an error to the secondErrorMessage
      • set validInput to false
      • set the txtSecondNumerator to blank
    • uses a try block to
      • get the secondDenominator from the user
      • parse it as an integer
    • uses a catch block if it can't be parsed as an integer to
      • appends an error to the secondErrorMessage
      • set validInput to false
      • set the txtSecondDenominator to blank
    • uses an if block to test if the secondDenominator is zero
      • if it is then it adds on an appropriate message to the secondErrorMessage
      • set validInput to false
      • set txtSecondDenominator to blank
    • if some input is invalid
      • firstErrorMessage on a line above the secondErrorMessage is displayed using the showMessageDialog( ) method of the JOptionPane class
      • sets the txtEchoFirst contents to blank
      • sets the txtEchoSecond contents to blank
      • sets the txtOperationType contents to blank
      • sets the txtAnswer contents to blank
    • returns validInput

 

Now we will move on to one of the classic examples for user class development, working with fractions.