Top: Index Previous: Arrays and Exceptions Up: Index Next: Assignment

CSC8303 -- Bioinformatics Programming in Java

Properties

The purpose of this exercise is:

exclaim
  • to understand properties
  • to implement an interface

Most Java objects have one or more properties. You will have seen these already, although we may not have used this name for them.

Every Java class can define one or more properties. These are features of the objects which describe the state of this class. In Java, you achieve this with "get/set methods"; these are just normal object methods whose names follow a standard naming convention.

So, for example:

class Person{

    private int age;
    public int getAge()
    {
       return age;
    }

    public void setAge( int age )
    {
       this.age = age;
    }
}

defines a very simple class called Person with a very simple property called Age. The property is implemented with three things: the age variable, the getAge getter method and the setAge setter method.

Java properties were first dealt with explicitly as part of JavaBeans; you can read about these in the java tutorial. But properties are used very generally. In this practical, we will move away from the shapes package, and build some class with properties.

quest Find the meaning of any of these words that you don't know: class, interface, field, constructor, method, property and get/set methods.
act
  1. Design and implement a class, Person, that has properties for Age, Height, Name and Profession. These properties should use appropriate Java types (for instance, Profession can be represented as a java.lang.String). Access to these properties should be only through get/set methods; ensure that you use privacy restrictions appropriately.
  2. Write a constructor for this class.

You have seen a test class before. In this case, we are going to extent the idea somewhat so that you get test all the methods of Person.

act
  1. Write a class called PersonTest. Write a method called testConstructor which constructs a Person object.
  2. Add to PersonTest methods called testAge, testHeight (and for all properties) which test whether the get methods correctly return the set value. These should be object not static methods.
  3. Write calls for these test methods from the main method.

Some times properties are dumb — you set them, you get them, and that's it. Often, however, there is additional logic to a class which can change the value of these properties. In this case, what happens to a person when they have a birthday.

act
  1. Write a method called birthday. This should increment a persons age by one. Add a test method to PersonTest to show that it works.

Some properties are read-only — you can get their value but not set it. There are many reasons to do this. In this case, we will introduce a read-only property called allowedDriver. This is read-only because it is derived from the age.

act
  1. Write a method called isAllowedDriver which returns true if the Person is over 17 years of age. Add a test method to PersonTest to demonstrate.

Some properties take parameters.

act
  1. Write a method isOlder which takes another Person as a parameter and returns true if the current Person is older than the Person passed to the method. Add an appropriate test method to PersonTest.

It is quite common in Java to define the appropriate properties for an Object in an interface. This is a special kind of class which defines a set of methods which have no implementation. Here, we will not go into great detail about how interfaces work, but just to start we will try and implement one. You can read more detail in the Java Tutorial.

act
  1. Make the Person class implement the java.lang.Comparable interface. You should be able to implement the required methods using the isOlder method. Add a test method to PersonTest.

Conclusion

Congratulations! You've got to the end.

You now have a good understanding of the basics of programming in general, and Java specifically. Bioinformaticians vary widely in their ability to program; don't worry if you have found the module difficult. It's important that you have a basic understanding of programming; it's not necessary for you to be able to produce 1000s of lines of code a day.

During the rest of the programme, you should think about ways to automate your work. Programming is a useful tool. While it can sometimes take a long time to produce a working piece of code, once it exists it generally takes very little time to run. Learning when to do work by hand, and when to code can save you an enormous amount of time.


Top: Index Previous: Arrays and Exceptions Up: Index Next: Assignment