Google
 
Site navigation: [ Home | Theory | Java | Moodle courses | Resource wiki | About ]

Classes

 

 

Use these techniques to cover mastery of User defined Objects.

data members are usually declared private so that they cannot be accessed from outside the Class

 

Constructors are always called by the new keyword.

 

A Class may have many methods. public methods are called by:

instance_name.method()

unless they are static in which case

class_name.method()

is used. (see this link for an example)

 

 

 

 

 

 

 

The this keyword makes sure that the identifier referred to is the Class data member and not another variable of the same name. This isn't usually a problem but is just good programming practice.

Example boolean:

public boolean isFull()
{
   return this.full;
}

  On this page:  [ basic structure | accessor & mutator methods | helper methods ]

  Related links:  [ terminology | intro to Java Classes | handling exceptions ]

Structure of a Class

Here we try to identify the structural elements of a Class:

Class definition public class Fraction
{
Data members   private int numerator;  
  private int denominator;
Constructor(s)   public Fraction()
   {
     numerator = 0;
     denominator = 1;
   }
  public Fraction(int n, int d)
   {
     numerator = n;
     denominator = d;
   }
Method(s)   public static Fraction add(Fraction a, Fraction b)
   {
     int cd = (a.denominator * b.denominator);
     a.numerator = a.numerator * b.denominator;
     b.numerator = b.numerator * a.denominator;
     int nm = a.numerator + b.numerator;
     return new Fraction(nm, cd);
   }
   public String toString()
   {
     return("" + numerator + "/" + denominator);
   }
end of Class definition }

Back to top

Accessor and mutator methods

It is usual to keep the data members of a class private so that they cannot be accessed and changed by other Classes - a principal known as "information-hiding".

Classes are able to provide public methods to change the state of an object. This way, if the internal representation is changed, other Classes can still use the Class - a change in the Class does not "break" existing code using the Class.

A method which sets or changes the state of a data member is known as a mutator method and, by convention, uses the word set. For example, we should add the following methods to Class fraction:

public void setNumerator(int n)
{
   this.numerator = n;
}

public void setDenominator(int d)
{
   this.denominator = d;
}

A method which returns the state of a data member is known as an accessor method and, by convention, uses the word get (or in the case of a boolean primitive is ). So, we would also add the following methods to Fraction.

public int getNumerator()
{
   return this.numerator;
}

public void getDenominator()
{
   return this.denominator;
}

The mutator methods can also be used to do any validation (checking that the data passed in is acceptable or reasonable), so the constructor should probably use these methods to:

public Fraction(int n, int d)
{
  setNumerator(n);
   setDenominator(d);
}

Back to top

Helper methods

Our Fraction class does not reduce the added fractions to the lowest common denominator, so that 1/2 + 1/4 would yield the answer 6/8 instead of 3/4. If we were to implement such a method it would not need to be public but would probably only be needed within the Class itself. Such methods are know as helper methods.

It is an excellent idea to break complex tasks down into smaller ones as this helps testing, error checking (debugging) and makes programs more reusable. As a general guide, if you are writing methods which extend over more than one screen you probably need to break the task down into two or more helper methods.

Back to top

Link to a revised version of the Fraction Class .

We leave it as an exercise for you (ie we're too lazy) to modify TestFraction to use the new Class so you can test it (basically involves adding a bunch of 2's at strategic places in the code).

For the adventurous. If you try to add a negative fraction to a positive one you get the correct result but the simplify() method doesn't work properly when the result is negative. Explain this and see if you can fix it.

The next page looks at error-handling methods - what happens if an application tries to construct a "bad" fraction.

Related: [ Java Home | previous: OOP intro | next:Exceptions]

 


 
The site is partly financed by advertising revenue, partly by online teaching activities and partly by donations. If you or your organisation feel these resouces have been useful to you, please consider a donation, $9.95 is suggested. Please report any issues with the site, such as broken links, via the feedback page, thanks.

Questions or problems related to this web site should be addressed to Richard Jones who asserts his right to be identified as the author and owner of these materials - unless otherwise indicated. Please feel free to use the material presented here and to create links to it for non-commercial purposes; an acknowledgement of the source is required by the Creative Commons licence. Use of materials from this site is conditional upon your having read the additional terms of use on the about page and the Creative Commons Licence. View privacy policy.

Creative Commons License


This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. © 2001 - 2007 Richard Jones, PO BOX 246, Cambridge, New Zealand; This page was last modified: July 29, 200823, 2008