Get and Set Methods
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.
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.
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.
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.
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
// 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
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.
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.
|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.
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.
<applet code="TimeGetSetTest.class" width=450 height=300>
|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.
The validateInput( ) method is the same as it was in the previous TimeBasicsTest.java
Now we finally get into a discussion of what this "this" we keep using really is.