Get and Set Methods

 

Background.  We have talked very little about what are considered to be "good" programming practices.  Many authors and programmers have very strong opinions on these issues.  Sometimes you may think you are really involved in a fairly irrational discussion of religion when you get involved in discussions about good programming practices. 

You should remember that all of the projects you are working on in this class and others are small enough to be done by one person.  Think about what it would be like to work on larger projects with more people.

One thing that always causes problems in developing computer programs is debugging.  Most programmers consider it to be good programming practice to develop things like methods because they can be debugged and put into use in a more isolated environment.  The developer can focus only on the issues associated with a particular method and ensure it is functioning properly.

Let's assume I gave you a problem where you needed to develop a GUI to accept input from a user to solve a system of linear equations.  You might not be able to find the code for the GUI elsewhere.  But you would be very likely to be able to either buy or find a Java code method for solving a system of linear equations.  If you trusted the source and tested it yourself you could be confident about this particular method.

How would this be different on larger and more complicated projects?

So it shouldn't surprise you to understand that most development project teams divvy up the overall programs requirements after having tried to figure out how these pieces should fit together.

You want some way for developers and users to work on data, be able to change things and retrieve things.  But you also want them to have to not do these things inadvertently, which is probably much more of a problem than you realize.

These sorts of issues are probably the main things motivating the use of something called get and set methods.  While get and set methods address only a small share of the overall variety of methods you are likely to write and/or make use of in your lifetime, they are still a significant portion.

Get and Set Methods.  Think about the few methods we have already developed for our TimeBasics class.  The following list gives a brief review.

  • TimeBasics( )
    • a constructor with no arguments
  • TimeBasics(int h, int m, int s)

    • a constructor with three arguments

  • setTime(int h, int m, int s)

    • a method to set the hours, minutes and seconds of the TimeBasics object

  • toAMPMString( )

    • a method to take a TimeBasics object and represent it as a string of the form    hh:mm:ss   with an AM or PM

  • toUniversalString( )

    • a method to take a TimeBasics object and represent it as a string of the form    hh:mm:ss   on a 24 hour clock

  • echoInput( )

    • a method to take a TimeBasics object directly based on the user's inputs and represent it as a string of the form    hh:mm:ss   on a 24 hour clock

I probably should have given the echoInput( ) method some sort of toString( ) classification since this is its "type".  But you should also notice that we have a setTime( ) method that takes user inputs and sets some aspect of one of our TimeBasics objects.  These are the sorts of things that get and set methods do.

  • Get methods get/retrieve some aspect of the object/class

  • Set methods set/specify some aspect of the object/class

It is very convenient to have a variety of toString( ) sorts of methods.  It is also convenient to have a variety of get and set methods.  Think of some of the built in methods we have had that are of these types.

  • getText( )

    • get the  contents of a JTextField

  • setText( )

    • set the contents of a JTextField

  • getSource( )

    • used in actionPerformed( ) methods to help determine the source of the user's click

  • setLayout( )

    • used to set the type of layout for a GUI

These are all I remember off the top of my head.  But you notice the convenience of the pattern.  In a well developed class with a collection of methods you might hope that a user could even stumble onto the name for a method they want to use!

Now we will modify the TimeBasics class to include a larger variety of get and set methods.  We will call this new class TimeGetSet.  But you should notice that we are really just extending the variety of methods we have.  The new methods are highlighted in blue.

  • TimeGetSet( )
    • a constructor with no arguments
  • TimeGetSet(int h, int m, int s)

    • a constructor with three arguments

  • setTime(int h, int m, int s)

    • a method to set the hours, minutes and seconds of the TimeGetSet object

  • setHour(int h)

    • a method to set the hour of a TimeGetSet object

  • setMinute(int m)

    • a method to set the minutes of a TimeGetSet object

  • setSecond(int s)

    • a method to set the seconds of a TimeGetSet object

  • getHour(int h)

    • a method to get the hour of a TimeGetSet object

  • getMinute(int m)

    • a method to get the minutes of a TimeGetSet object

  • getSecond(int s)

    • a method to get the seconds of a TimeGetSet object

  • tickHours(TimeGetSet t)

    • a method to add an hour to the present hour

  • tickMinutes(TimeGetSet t)

    • a method to add a minute to the present minutes

  • tickSeconds(TimeGetSet t)

    • a method to add a second to the present seconds

  • toUniversalString( )

    • a method to take a TimeGetSet object and represent it as a string of the form    hh:mm:ss   on a 24 hour clock

  • toAMPMString( )

    • a method to take a TimeGetSet object and represent it as a string of the form    hh:mm:ss   with an AM or PM

  • echoInput( )

    • a method to take a TimeGetSet object directly based on the user's inputs and represent it as a string of the form    hh:mm:ss   on a 24 hour clock

  • toTwoDigitString(int someNumber)

    • a method to take an integer, that presumably represents hours, minutes or seconds and make certain it has exactly two digits

      • doesn't drop any leading zeros

I'm assuming you see a few patterns emerging.  So you should make the following TimeGetSet.java.  You want to compile it using the following in order to create a package in javaPackages\TimeClasses.

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

command.

 

package javaPackages.TimeClasses;
import java.text.DecimalFormat;

// This class maintains the time in a 24 hour format
// and has a method to convert this to an AM/PM scale
// and several other methods to get and set important
// aspects of the class


public class TimeGetSet extends Object
{

//  instance variables for every object of the TimeGetSet class
private int hour; // from 0 to 23
private int minute; // from 0 to 59
private int second; // from 0 to 59

public TimeGetSet( )
{

setTime(0,0,0);

}

public TimeGetSet(int h, int m, int s)
{

setTime(h,m,s);

}

// Set a new time using military/universal representation.
// Perform validity checks on the data.
// Set invalid values to zero to ensure consistency.

public void setTime( int h, int m, int s)
{

// if hour number is outside the acceptable range set it to 0
if (h < 0 || h >= 24) hour = 0;
else hour = h;
// if minutes number is outside the acceptable range set it to 0
if (m < 0 || m >= 60) minute = 0;
else minute = m;
// if seconds number is outside the acceptable range set it to 0
if (s < 0 || s >= 60) second = 0;
else second = s;

}

// developing a grouping of set methods for each parameter
// Set the hour only

public void setHour( int h)
{

if (h < 0 || h >= 24) hour = 0;
else hour = h;

}

// Set the minute only
public void setMinute( int m)
{

if (m < 0 || m >= 60) minute = 0;
else minute = m;

}

// Set the second only
public void setSecond( int s)
{

if (s < 0 || s >= 60) second = 0;
else second = s;

}

// developing a grouping of get methods for each parameter
public int getHour( )
{

return hour;

}

// Get the minute
public int getMinute( )
{

return minute;

}

// Get the second
public int getSecond( )
{

return second;

}

// a method to increment the seconds counter
public void tickSeconds(TimeGetSet t)
{

t.setSecond( (t.getSecond( ) + 1) % 60);

//  if the seconds counter goes to zero
//  increment the minutes counter

if ( t.getSecond( ) == 0 )
{

t.setMinute( (t.getMinute( ) + 1) % 60);

//  if the minutes counter goes to zero
//  increment the hours counter
if ( t.getMinute( ) == 0 )

t.setHour( (t.getHour( ) + 1) % 24);

}

}

// a method to increment the minutes counter
public void tickMinutes(TimeGetSet t)
{

t.setMinute( (t.getMinute() + 1) % 60);
//  if the minutes counter goes to zero
//  increment the hours counter

if ( t.getMinute( ) == 0 )

t.setHour( (t.getHour( ) + 1) % 24);

}

// a method to increment the hours counter
public void tickHours(TimeGetSet t)
{

t.setHour( (t.getHour( ) + 1) % 24);

}

// This method converts a time to a string in military/universal format
public String toUniversalString( )
{

return toTwoDigitString(hour) + ":" + toTwoDigitString(minute) + ":" + toTwoDigitString(second);

}

// Convert string into AM/PM time format
public String toAMPMString( )
{

String AMPM;
int tempHour;
// checking for noon and midnight
if (hour == 12 || hour == 0)

tempHour = 12;

// if not noon or midnight then
// cast hour into correct number of hours
// since noon or midnight

else

tempHour = hour % 12;

// determine whether it is AM or PM by hour
if (hour < 12)

AMPM = "AM";

else

AMPM = "PM";

return return toTwoDigitString(tempHour) + ":" + toTwoDigitString(minute) + ":" + toTwoDigitString(second) + AMPM;

}

public String echoInput( )
{

return return toTwoDigitString(hour) + ":" + toTwoDigitString(minute) + ":" + toTwoDigitString(second);

}

//  a method to ensure leading zeros aren't dropped
//  when representing hours. minutes and or seconds

public String toTwoDigitString(int someNumber)
{

DecimalFormat twoDigits = new DecimalFormat("00");

return twoDigits.format(someNumber);

}

}

 

So this TimeGetSet becomes our new class of time objects with a bit more sophistication in terms of our abilities to manipulate.

At present we give a short discussion of this class before using it.  Though, hopefully you understand the overall purposes of this class due to our previous outline presentation of its methods.

  • we start by creating a package in the javaPackages\TimeClasses subdirectory of C:\j2sdk1.4.0\jre\classes where the beginning part of the path isn't specified until that compile command

  • import java.text.DecimalFormat

  • the overall inclusive class is called TimeGetSet that extends Object

    • declare an instance variable private integer hour

    • declare an instance variable private integer minute

    • declare an instance variable private integer second

 

  • create a constructor TimeGetSet( )

    • receives no arguments

    • uses a later method setTime to initialize all times to zeros

 

  • create a constructor TimeGetSet(int h, int m, int s)

    • receives three arguments

    • uses a later method setTime to initialize all times using the h, m and s

 

  • a setTime( ) method is declared and developed
    • it receives three integer arguments
      • h  for the hours
      • m  for the minutes
      • s  for the seconds
    • an if statement is used to ensure the hours are within an acceptable range from 0 up to 23
      • if they aren't then the hours are set to zero
    • an if statement is used to ensure the minutes are within an acceptable range from 0 up to 59
      • if they aren't then the minutes are set to zero
    • an if statement is used to ensure the seconds are within an acceptable range from 0 up to 59
      • if they aren't then the seconds are set to zero

 

  • a setHour(int h) method is declared and developed
    • it receives one integer argument
      • h  for the hours
    • an if statement is used to ensure the hours are within an acceptable range from 0 up to 23
      • if they aren't then the hours are set to zero

 

  • a setMinute(int m) method is declared and developed
    • it receives one integer argument
      • m  for the minutes
    • an if statement is used to ensure the minutes are within an acceptable range from 0 up to 59
      • if they aren't then the minutes are set to zero

 

  • a setSecond(int s) method is declared and developed
    • it receives one integer argument
      • s  for the seconds
    • an if statement is used to ensure the seconds are within an acceptable range from 0 up to 59
      • if they aren't then the seconds are set to zero

 

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

 

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

 

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

 

  • a tickSeconds( ) method is declared and developed
    • it receives one arguments
      • an object of type TimeGetSet
    • it returns nothing
    • it increments the number of seconds of the object by one
    • this incremented value is then operated on with the modulus operator using a 60 to make certain the number stays within the range 0 to 59
    • if the number of seconds has just incremented to 60 it will become a zero
    • if the number of seconds is a zero
      • the number of minutes of the object is incremented by one
      • this incremented value is then operated on with the modulus operator using a 60 to make certain the number stays within the range 0 to 59
      • if the number of minutes has just incremented to 60 it will become a zero
      • if the number of minutes is a zero
        • the number of hours is incremented by one
        • this incremented value is then operated on with the modulus operator using a 24 to make certain the number stays within the range 0 to 23

 

  • a tickMinutes( ) method is declared and developed
    • it receives one arguments
      • an object of type TimeGetSet
    • it returns nothing
    • the number of minutes of the object is incremented by one
    • this incremented value is then operated on with the modulus operator using a 60 to make certain the number stays within the range 0 to 59
    • if the number of minutes has just incremented to 60 it will become a zero
    • if the number of minutes is a zero
      • the number of hours is incremented by one
      • this incremented value is then operated on with the modulus operator using a 24 to make certain the number stays within the range 0 to 23

 

  • a tickHours( ) method is declared and developed
    • it receives one arguments
      • an object of type TimeGetSet
    • it returns nothing
    • the number of hours is incremented by one
    • this incremented value is then operated on with the modulus operator using a 24 to make certain the number stays within the range 0 to 23

 

  • a toUniversalString( ) method is declared and developed
    • it uses the class method toTwoDigitString( ) to ensure that the output terms have no decimal places and don't drop leading zeros
    • the format is developed for a 24 hour clock using hours:minutes:seconds  =  hh:mm:ss

 

  • a toAMPMString( ) method is declared and developed
    • it uses the class method toTwoDigitString( ) to ensure that the output terms have no decimal places and don't drop leading zeros
    • the format is set up to report the clock time using an AM or PM denotation
    • an if statement on the hours is used to determine whether it is noon or midnight and adjust a local tempHour variable appropriately
    • if it isn't noon or midnight then the modulus operator is used to determine the number of hours since noon or midnight and assigned to tempHours
    • finally, based on the initial value for the hours an AMPM variable is
      • set to AM if hours is less than 12
      • set to PM if hours is 12 or greater

 

  • an echoInput( ) method is declared and developed
    • it uses the class method toTwoDigitString( ) to ensure that the output terms have no decimal places and don't drop leading zeros
    • it returns what the user inputted except those that have been switched to zeros in setTime( ) if the inputted values are out of bounds
    • the format is developed for hours:minutes:seconds  =  hh:mm:ss

 

  • a toTwoDigitsString( ) method is declared and developed
    • it receives one integer argument
    • it returns a String
    • it makes use of the DecimalFormat class in the text package to create a format that always has exactly two places and no decimal or anything after the decimal
    • it returns whatever integer was passed to it in this format as a string

 

Now you need the following TimeGetSetTest.java to construct/instantiate a TimeGetSet object and work with it.  Primarily, we will be incrementing hours, minutes and seconds and updating the display as this happens.  We will also develop the overall GUI based on three smaller panels, one for input, one for output and one for command buttons.  Each of these smaller panels is developed in its own method.  Then these panels are assembled into an overall GridLayout within the init( ) method.

 

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javaPackages.TimeClasses.TimeGetSet;

public class TimeGetSetTest extends JApplet implements ActionListener
{

// declare GUI components
JLabel lblHoursInput;
JTextField txtHoursInput;
JLabel lblMinutesInput;
JTextField txtMinutesInput;
JLabel lblSecondsInput;
JTextField txtSecondsInput;
JLabel lblUniversalTime;
JTextField txtUniversalTime;
JLabel lblAMPMTime;
JTextField txtAMPMTime;
JButton cmdIncrementHours;
JButton cmdIncrementMinutes;
JButton cmdIncrementSeconds;
JPanel panOverall;
JPanel panInput;
JPanel panOutput;
JPanel panButtons;

// declare four global variables related to inputs
boolean validInput;
int hoursInput = 0;
int minutesInput = 0;
int secondsInput = 0;

public void init( )
{

// instantiate the GUI to hold other panels
JPanel panOverall = new JPanel();
panOverall.setLayout(new GridLayout(3,1));

// create Input Panel
panInput = createInputPanel();
panOverall.add(panInput);

// create Output Panel
panOutput = createOutputPanel();
panOverall.add(panOutput);

// create Increment Button Panel
panButtons = createButtonPanel();
panOverall.add(panButtons);

setContentPane(panOverall);

}

public void actionPerformed(ActionEvent userClick)
{

// determine if the user's inputs are valid
if (validateInput( ))
{

// if they are then instantiate the TimeGetSet object
// using the inputs

TimeGetSet thisTime = new TimeGetSet(hoursInput, minutesInput, secondsInput);
// this next block of code determines the source of the
// user's click and takes the appropriate action
// if the user clicks on the incrememntHours button
if (userClick.getSource( ) == cmdIncrementHours)
{

thisTime.tickHours(thisTime);
txtHoursInput.setText(thisTime.toTwoDigitString(thisTime.getHour( )));
txtMinutesInput.setText(thisTime.toTwoDigitString(thisTime.getMinute( )));
txtSecondsInput.setText(thisTime.toTwoDigitString(thisTime.getSecond( )));
txtUniversalTime.setText(thisTime.toUniversalString( ));
txtAMPMTime.setText(thisTime.toAMPMString( ));

}

// if the user clicks on the incrememntMinutes button
if (userClick.getSource( ) == cmdIncrementMinutes)
{

thisTime.tickMinutes(thisTime);
txtHoursInput.setText(thisTime.toTwoDigitString(thisTime.getHour( )));
txtMinutesInput.setText(thisTime.toTwoDigitString(thisTime.getMinute( )));
txtSecondsInput.setText(thisTime.toTwoDigitString(thisTime.getSecond( )));
txtUniversalTime.setText(thisTime.toUniversalString( ));
txtAMPMTime.setText(thisTime.toAMPMString( ));

}


// if the user clicks on the incrememntSeconds button
if (userClick.getSource( ) == cmdIncrementSeconds)
{

thisTime.tickSeconds(thisTime);
txtHoursInput.setText(thisTime.toTwoDigitString(thisTime.getHour( )));
txtMinutesInput.setText(thisTime.toTwoDigitString(thisTime.getMinute( )));
txtSecondsInput.setText(thisTime.toTwoDigitString(thisTime.getSecond( )));
txtUniversalTime.setText(thisTime.toUniversalString( ));
txtAMPMTime.setText(thisTime.toAMPMString( ));

}

}

}

public JPanel createInputPanel( )
{

JPanel panInputGUI = new JPanel( );
panInputGUI.setLayout( new GridLayout(3, 2, 5, 5) );

// developing input row for hours
lblHoursInput = new JLabel("Please input the hours: ", SwingConstants.RIGHT);
panInputGUI.add(lblHoursInput);

txtHoursInput = new JTextField(3);
panInputGUI.add(txtHoursInput);

// developing input row for minutes
lblMinutesInput = new JLabel("Please input the minutes: ", SwingConstants.RIGHT);
panInputGUI.add(lblMinutesInput);

txtMinutesInput = new JTextField(3);
panInputGUI.add(txtMinutesInput);

// developing input row for seconds
lblSecondsInput = new JLabel("Please input the seconds: ", SwingConstants.RIGHT);
panInputGUI.add(lblSecondsInput);

txtSecondsInput = new JTextField(3);
panInputGUI.add(txtSecondsInput);

return panInputGUI;

}


public JPanel createOutputPanel( )
{

JPanel panOutputGUI = new JPanel();
panOutputGUI.setLayout( new GridLayout(2, 2, 5, 5) );

// developing output row for universal time
lblUniversalTime = new JLabel("Increments in universal time: ", SwingConstants.RIGHT);
panOutputGUI.add(lblUniversalTime);

txtUniversalTime = new JTextField(10);
txtUniversalTime.setEditable(false);
panOutputGUI.add(txtUniversalTime);

// developing output row for universal time
lblAMPMTime = new JLabel("Increments in AM/PM time: ", SwingConstants.RIGHT);
panOutputGUI.add(lblAMPMTime);

txtAMPMTime = new JTextField(10);
txtAMPMTime.setEditable(false);
panOutputGUI.add(txtAMPMTime);

return panOutputGUI;

}

public JPanel createButtonPanel( )
{

JPanel panButtonGUI = new JPanel( );
panButtonGUI.setLayout( new GridLayout(1, 3, 5, 5) );

// command button for incrementing hours
cmdIncrementHours = new JButton("Increment Hours");
cmdIncrementHours.addActionListener(this);
panButtonGUI.add(cmdIncrementHours);

// command button for incrementing minutes
cmdIncrementMinutes = new JButton("Increment Minutes");
cmdIncrementMinutes.addActionListener(this);
panButtonGUI.add(cmdIncrementMinutes);

// command button for incrementing hours
cmdIncrementSeconds = new JButton("Increment Seconds");
cmdIncrementSeconds.addActionListener(this);
panButtonGUI.add(cmdIncrementSeconds);

return panButtonGUI;

}

public boolean validateInput( )
{

String errorMessage = "";
validInput = true;

try
{

hoursInput = Integer.parseInt(txtHoursInput.getText( ));

}

catch (NumberFormatException nfeInteger)
{

// give error message when input can't be parsed to integer
errorMessage = errorMessage + "You need to enter an integer for the hour\n";
// set boolean variable to false when
// received an invalid input

validInput = false;
// blanking out inputs and any previous outputs
// when input is invalid

txtHoursInput.setText("");

}

try
{

minutesInput = Integer.parseInt(txtMinutesInput.getText( ));

}

catch (NumberFormatException nfeInteger)
{

// give error message when input can't be parsed to integer
errorMessage = errorMessage + "You need to enter an integer for the minutes\n";
// set boolean variable to false when
// received an invalid input

validInput = false;
// blanking out inputs and any previous outputs
// when input is invalid

txtMinutesInput.setText("");

}

try
{

secondsInput = Integer.parseInt(txtSecondsInput.getText());

}

catch (NumberFormatException nfeInteger)
{

// give error message when input can't be parsed to integer
errorMessage = errorMessage + "You need to enter an integer for the seconds\n";
// set boolean variable to false when
// received an invalid input

validInput = false;
// blanking out inputs and any previous outputs
// when input is invalid

txtSecondsInput.setText("");

}

if (!validInput)
{

JOptionPane.showMessageDialog(null, errorMessage, "Input Error", JOptionPane.ERROR_MESSAGE);
txtUniversalTime.setText("");
txtAMPMTime.setText("");

}

return validInput;

}

}

 

Again, due to the package the code will run wherever you want it to on the same machine.  You do not need to specify any special path in the compilation command.

javac TimeGetSetTest.java

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

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

 

<html>
<applet code="TimeGetSetTest.class" width=450 height=300>
</applet>
</html>

 

If you try to click on any of the increment buttons without the necessary inputs being present you should get something like the following image.  Though the message should depend on your inputs.

 

 

With acceptable integers within the input text fields you should see something more like the following.

 

 

Take a bit of time to play with this.

Now we will discuss the code.

  • we need to import the javax.swing.* package
  • import java.awt.*
  • import java.awt.event
  • import our TimeGetSet class in located in C:\j2sdk1.4.0\jre\classes\javaPackages\TimeClassses

 

  • the overall inclusive class is called TimeGetSetTest which extends JApplet and implements ActionListener
    • declare the GUI components
      • a label for the hours input = lblHoursInput
      • a text field for the hours input = txtHoursInput
      • a label for the minutes input = lblMinutesInput
      • a text field for the minutes input = txtMinutesInput
      • a label for the seconds input = lblSecondsInput
      • a text field for the seconds input = txtSecondsInput
      • a label for output in universal time = lblUniversalTime
      • a text field for output in universal time = txtUniversalTime
      • a label for output in AM/PM time = lblAMPMTime
      • a text field for output in AM/PM time = txtAMPMTime
      • a button to cause the program to increment the hours = cmdIncrementHours
      • a button to cause the program to increment the minutes = cmdIncrementMinutes
      • a button to cause the program to increment the seconds = cmdIncrementSeconds
      • a panel to contain the input components of the GUI called panInput
      • a panel to contain the output components of the GUI called panOutput
      • a panel to contain the buttons of the GUI called panButtons
    • 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 hours input = hoursInput
      • initialize and declare a variable for the user's minutes input = minutesInput
      • initialize and declare a variable for the user's seconds input = secondsInput

 

  • the init( ) method
    • instantiates a panel to be the panel that contains all the other panels called panOverall
    • sets its layout to be a grid with three rows and one column
    • calls the methods to create the
      • input panel
      • output panel
      • button panel
    • adds each of these panels 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 inputs
    • if the inputs are acceptable
      • creates a new instance of a TimeGetSet object called thisTime set by the user's inputs
      • determines the source of the user's click
      • if the user clicks the cmdIncrementHours button
        • calls the tickHours( ) method in class TimeGetSet for thisTime
        • updates the display in the txtHoursInput text field
          • gets the hour
          • uses the class method toTwoDigitString( ) to format it for display
          • sets the text using the built in setText( ) method
        • updates the display in the txtMinutesInput text field
          • gets the hour
          • uses the class method toTwoDigitString( ) to format it for display
          • sets the text using the built in setText( ) method
        • updates the display in the txtSecondsInput text field
          • gets the hour
          • uses the class method toTwoDigitString( ) to format it for display
          • sets the text using the built in setText( ) method
        • updates the GUI display for the universal time
          • invokes the toUniversalTime( ) method of the TimeGetSet class
          • sets the text using the built in setText( ) method
        • updates the GUI display for the AM/PM time
          • invokes the toAMPMTime( ) method of the TimeGetSet class
          • sets the text using the built in setText( ) method
      • if the user clicks the cmdIncrementMinutes button
        • calls the tickMinutes( ) method in class TimeGetSet for thisTime
        • updates the display in the txtHoursInput text field
          • gets the hour
          • uses the class method toTwoDigitString( ) to format it for display
          • sets the text using the built in setText( ) method
        • updates the display in the txtMinutesInput text field
          • gets the hour
          • uses the class method toTwoDigitString( ) to format it for display
          • sets the text using the built in setText( ) method
        • updates the display in the txtSecondsInput text field
          • gets the hour
          • uses the class method toTwoDigitString( ) to format it for display
          • sets the text using the built in setText( ) method
        • updates the GUI display for the universal time
          • invokes the toUniversalTime( ) method of the TimeGetSet class
          • sets the text using the built in setText( ) method
        • updates the GUI display for the AM/PM time
          • invokes the toAMPMTime( ) method of the TimeGetSet class
          • sets the text using the built in setText( ) method
      • if the user clicks the cmdIncrementSeconds button
        • calls the tickSeconds( ) method in class TimeGetSet for thisTime
        • updates the display in the txtHoursInput text field
          • gets the hour
          • uses the class method toTwoDigitString( ) to format it for display
          • sets the text using the built in setText( ) method
        • updates the display in the txtMinutesInput text field
          • gets the hour
          • uses the class method toTwoDigitString( ) to format it for display
          • sets the text using the built in setText( ) method
        • updates the display in the txtSecondsInput text field
          • gets the hour
          • uses the class method toTwoDigitString( ) to format it for display
          • sets the text using the built in setText( ) method
        • updates the GUI display for the universal time
          • invokes the toUniversalTime( ) method of the TimeGetSet class
          • sets the text using the built in setText( ) method
        • updates the GUI display for the AM/PM time
          • invokes the toAMPMTime( ) method of the TimeGetSet class
          • sets the text using the built in setText( ) method

 

  • createInputPanel( )
    • creates the panel to contain the input components
    • sets the layout to a GridLayout with three rows and two columns
    • instantiates the appropriate JLabels and JTextFields
    • returns this panel to the calling location

 

  • createOutputPanel( )
    • creates the panel to contain the output components
    • sets the layout to a GridLayout with two rows and two columns
    • instantiates the appropriate JLabels and JTextFields
    • returns this panel to the calling location

 

  • createButtonPanel( )
    • creates the panel to contain the command buttons
    • sets the layout to a GridLayout with one rows and three columns
    • instantiates the appropriate JButtons
    • returns this panel to the calling location

The validateInput( ) method is the same as it was in the previous TimeBasicsTest.java

  • the validateInput( ) method
    • initializes a blank errorMessage
    • initializes validInput to true
    • uses a try block to
      • get the text from txtHourInput
      • parse it as an integer
    • uses a catch block if it can't be parsed as an integer to
      • append a string to the errorMessage
      • set validInput to false
      • set the txtHourInput to blank
    • uses a try block to
      • get the text from txtMinutesInput
      • parse it as an integer
    • uses a catch block if it can't be parsed as an integer to
      • append a string to the errorMessage
      • set validInput to false
      • set the txtMinutesInput to blank
    • uses a try block to
      • get the text from txtSecondsInput
      • parse it as an integer
    • uses a catch block if it can't be parsed as an integer to
      • append a string to the errorMessage
      • set validInput to false
      • set the txtSecondsInput to blank
    • if validInput is false due to some input problem
      • the errorMessage is displayed using the showMessageDialog( ) method of the JOptionPane class
      • set the text of txtUniversalTime to blank
      • set the text of txtAMPMTime to blank

 

Now we finally get into a discussion of what this "this" we keep using really is.