Some Background on Classes
We have been making use of a lot of different classes built into Java and
imported in packages. These classes contain most of the methods and
properties we have been using. A few weeks ago we started developing
our own user defined methods. Now we are going to start developing
our own user defined classes.
Why develop user defined classes? Mainly, because it usually isn't possible to find all the functionality you need with the pre-built classes provided with Java. For examples of these we will work with the following situations to help motivate our excursion into developing user defined classes.
For example, Java has built in primitive data types that give numbers and letters in particular formats. But then we made use of the pre-built String class to work with strings of char data. Similarly, Java has pre-built Date and Time classes to work with these. All three of these classes with their methods and properties were based on the more primitive data types and methods.
So let's take some time to think about what it would be like to develop date and time classes on our own. Let's start with developing a time class.
A Time Class. Remember, on a computer time is usually represented by some set of numbers like the following.
Before we go further I'm quite confident you need to increase your awareness of the difficulties of taking mental concepts and getting them to work on computers. This will be one of the issues we will see repeat itself over and over as we develop our own classes.
Anyway, now think about some other things.
This also become important when you want to perform operations on time such as adding or subtracting times. For example, if you want to add 2 minutes and 11 seconds to the time
you need to make certain your "adding" method works appropriately to give
So now you can start to think of some of the methods we want for our time class. There are so many other things to deal with we will move onto something else so that we can eventually get to developing our own classes.
A Date Class. Now think of things represented with the following format.
Well, these are some of the most obvious issues with even just the basic representation.
But remember there are different representations such as the following
So you need to have methods to adapt to such things as well as doing other things such as adding and/or subtracting days, identifying weeks, days of the week and on and on. Again, there are so many other things to deal with we will move onto something else so that we can eventually get to developing our own classes.
A Class for Fractions. Now you will really encounter the disparity between what you do in your head/work on paper and what needs to be done to work on a computer! The notation
for a fraction, where a and b are integers, is not going to do what you want with a computer. This is particularly true of plenty of other aspects of fractions as simple as addition.
a/b + c/d
Think about what was required in order to perform even simple addition with fractions when you learned about it in school.
These sorts of things also hold for subtraction. While multiplication and division aren't as difficult, they still need to be reduced after the results are computed.
A few questions that you need to consider before developing related programs follow.
Developing a class and GUI for doing these sorts of things is really quite involved.
A Class for Complex Numbers. Remember complex numbers? You probably studied them when trying solve quadratic equations and you came across square roots of negative numbers. You should remember the following.
|Thus, i is called an imaginary
number. Thus complex numbers are of the following form.
a + bi
where a and b are any real numbers. The letter a is called the real part of the complex number. The letter b is called the imaginary part of the complex number.
Hopefully, you remember doing things like adding, subtracting and multiplying complex numbers. Multiplication of two complex numbers results in the following.
(a + bi)(c + di) = (ac - bd) + (bc + ad)i
Rather than go into a lot of detail should again be thinking about the following questions.
Developing a class and GUI for doing these sorts of things is really quite involved, though it is simpler than it is for fractions and is likely to be a homework problem.
A Class for Output. Think about how messy and somewhat primitive it is to develop output in Java. You might want to spend some time thinking about what sort of functionality you might really want to have for output. I won't talk about this more at present. This would really need to get way more sophisticated than we are at present. But, these are the sorts of things that can be worthwhile.
A Class for Employee Payroll. At least some variation of this is a fairly common example used to teach user development of classes, objects, methods and properties. I will develop such an example in this course.
Some of the issues that are important to consider when developing such a class are the following.
Obviously, this list can go on and on.
Some Syntactic Issues. Classes inherit from the Object class by default. A superclass of a class is indicated by use of the extends keyword. A class extends its superclass, that is specifies and develops its own methods and variables.
Classes are defined using the following type of syntax.
In Java, there are three types of variables