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.
  2. This is a little hard to do. To get it to work you will need to use a new piece of syntax; either "generics" or "casting".
log Person.java and PersonTest.java are the final piece of coursework.

Sorting People out

Finally, let's try sorting some people out; don't worry if you can't get this to work, as it is not part of the assessed courework. Please give it a go. It will show you some of the purpose of interfaces, and properties.

act
  1. Copy your work, so that you have an old saved version.
  2. Ensure that you add a constructor for Person.java. Netbeans will do this for you (Right click in the editor—> insert code —> Constructor). It should have parameters of String name, int age.

Now, we need some people to sort and a test class.

act
  1. Create a new class called PersonSorter.java.
  2. Add the following main method:
  3. Check that the code compiles and run.
    public static void main(String[] args) {

        PersonSorter t = new PersonSorter();

        Person[] persons = new Person[5];
        persons[ 0] = new Person("Little Bear", 5);
        persons[ 1] = new Person("Mother Bear", 30);
        persons[ 2] = new Person("Father Bear", 40);
        persons[ 3] = new Person("Goldlocks", 7);
        persons[ 4] = new Person("Big Bad Wolf", 35);

        System.out.println("Unsorted");
        t.printArray(persons);

    }

    public void printArray(Person[] persons) {
        for (Person person : persons) {
            System.out.println("Person: " + person.getName() + " " + person.getAge());
        }
    }

It would be nice to be able to sort these people in some way. Actually, this isn't too hard. For this we need two pieces of code.

act

Add this code to the main method of the PersonSorter class.

        System.out.println("natural sort");
        t.naturalSort(persons);
        t.printArray(persons);

And this method. Arrays.sort() does pretty much what it says; it sorts the contents of an array. It is java.lang; you should know how to deal with this by now.

    public Person[] naturalSort(Person[] persons) {
        Arrays.sort(persons);
        return persons;
    }

When you have done this, run PersonSorter.

If you have implemented the java.lang.Comparable method, your code should now work. If you haven't implemented it at all, your code will crash. If you have implemented it wrongly, then you will get a list in the wrong order.

act Try changing the Person class so that this code now sorts Alphabetically, by name, rather than by age.

This is all very well, but we don't want to have to change the code each time we want to sort in a different way. Perhaps, we want to sort in two ways at the same time.

act Try changing the code so that you sort first by age, and then by name. You will need to use the java.util.Comparator interface to achieve this; you may need to use yet another new piece of syntax.

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