Some Basics for a Time Class
Introduction.
In the previous webpage I discussed some issues associated with classes contain most of the methods
working with times. This webpage will focus on developing some basic
methods for outputting and transferring between different time formats.
This is a modification of what Deitel and Deitel do in their book.
We will take their work a step further by developing a GUI for user
interactions. We will be representing time in a format hh:mm:ss where each
Our GUI will allow a user to enter the hours in one JTextField, minutes in another, and seconds in another. Then the user's inputs will be echoed, displayed in AM/PM format and in universal time format on a 24 hour clock. We will also develop a method to do some input validation. For the next week we will enhance this class and its implementation in a variety of ways. The TimeBasics Class. The first set of code is used to create our class. It will not be executed directly. It will allow a developer to import its pre-existing capabilities in other programs. You should save the following program in ots own directory and call it TimeBasics.java. |
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 public class TimeBasics extends Object {
} |
This program needs to be compiled into a class using
the statement
this creates TimeBasics.class which can be used by other programs as long as these programs are in the same directory. The next webpage will present how to create classes and place them into another directory and then access such classes from any other java program. But we will leave this for the future. The first thing you might notice about the code is there is no method main( ) or init( ). Nothing in the program will cause it to execute. But its compilation does create a class that is usable by other programs. The code does the following
So, this is a fairly meager set of methods in a class, but it is a start and we will be able to use it to focus on other issues. We will augment its functionality as we proceed through the next week. A GUI for Testing TimeBasics. Rather than create what seems to be a bare minimum testing program as is done in Deitel and Deitel, we will implement a GUI to allow for user inputs. This GUI will be an applet. The applet will primarily involve developing the GUI, validating inputs, using the TimeBasics class to operate on the user's inputs and displaying the results on the GUI. It's methods are constructed with these basic functionalities in mind. You should call the file TimeBasicsTest.java. Make sure it is in the same directory as the TimeBasics.class. |
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class TimeBasicsTest extends JApplet implements ActionListener {
} |
Since I want you to run the program before we discuss it you should create the TimeBasicsTest.html. |
<html> <applet code="TimeBasicsTest.class" width=400 height=300> </applet> </html> |
Now you should run the program and try different inputs. If some input is non-integer you should get an error message appropriate to which are invalid like the following. |
Assuming you enter acceptable numbers you should see something like the following. |
You should be able to change the inputs and still get a
stable set of interactions through this GUI based on your inputs since we
have put in some error trapping. Now we discuss the code which isn't much different than other GUI development and input handling you've already seen. The only truly new code occurs in the actionPerformed( ) method where we instantiate a TimeBasics object, set its time based on the user's inputs and make use of the TimeBasics methods to display different time formats. The discussion of the code follows.
You should play with this program some more and make certain you understand what class the methods are in. |