The purpose of this exercise is:
|
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.
Find the meaning of any of these words that you don't know: class, interface, field, constructor, method, property and get/set methods. |
|
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
.
|
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.
|
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.
|
Some properties take parameters.
|
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.
|
Person.java and PersonTest.java are the final piece of coursework.
|
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.
|
Now, we need some people to sort and a test class.
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.
Add this code to the main method of the
System.out.println("natural sort");
t.naturalSort(persons);
t.printArray(persons);
And this method. public Person[] naturalSort(Person[] persons) { Arrays.sort(persons); return persons; } When you have done this, run |
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.
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.
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.
|
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.