Some Background on Classes

 

Introduction.  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.

  • time
  • date
  • fractions
  • complex numbers
  • output
  • employee payroll

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.

hh:mm:ss

where each

  • hh is a positive integer from 00 to 23

  • mm is a positive integer from 00 to 59

  • ss is a positive integer from 00 to 59

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.

  • as the number of seconds increases and is about to become 60

    • the ss should become 00

    • the mm should become one more than it was

  • as the number of minutes increases and is about to become 60

    • the mm should become 00

    • the hh should become one more than it was

  • as the number of hours increases and is about to become 24

    • the hh should become 00

    • something may be required to ensure the computer "knows" that we have started a new day

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

21:57:55

you need to make certain your "adding" method works appropriately to give

22:00:06

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.

mm/dd/yy

where

  • mm is a positive integer from 01 to 12

  • dd is a positive integer from 01 to something higher which depends on the month

    • going to be 28 for February = month 02

    • going to be 29 for February in leap years

    • going to be 30 for

      • April where mm = 04

      • June where mm = 06

      • September where mm = 09

      • November where mm = 11

    • going to be 31 for

      • January where mm = 01

      • March where mm = 03

      • May where mm = 05

      • July where mm = 07

      • August where mm = 08

      • October where mm = 10

      • December where mm = 12

  • yy is a positive integer from 00 to 99

    • you likely notice how ambiguous this can be since it neglects to identify the century

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

  • yyyy for the year to clarify things like 1903 and 2003

  • dd/mm/yy for the British approach to short format for dates

  • using the names of the months within the representation

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

a/b

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.

  1. you need a common denominator  (LCM - least common multiple)

  2. then you can add the numerators

  3. then you need to reduce the fraction

    1. so that there are no common factors in both the numerator and denominator

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.

  • So how would you choose to represent fractions within a computer?

  • How would you represent fractions on a GUI?

  • What sort of GUI would you develop to ease the user's efforts?

  • How would you input and output fractions?

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.

  • So how would you choose to represent complex numbers within a computer?

  • How would you represent complex numbers on a GUI?

  • What sort of GUI would you develop to ease the user's efforts?

  • How would you input and output complex numbers?

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.

  • regular pay
  • overtime pay
  • pay by commission
  • hourly pay
  • pay based on salary
  •  

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.

class SomeClassName
{

body of class

}

In Java, there are three types of variables

  • local variables
    • declared and used only with a method
  • instance variables
    • usually defined just after the class definition statement so that they are available to all of the methods in the class
    • not defined within a method
    • not declared using the static keyword
    • typically used to refer to aspects of instances of the objects developed in the class
    • every object of this particular class will have these sorts of variables
    • I often use the term "global" variables since it is closest to the appropriate jargon I learned in the past ... I really need to change ... this is inaccurate
  • class variables
    • the static keyword is used in their declaration
    • these variables apply to the class as a whole
    • aren't stored with each instance of an object
    • these can be used to communicate between different objects of the same class
    • these can be used to keep track of class wide information