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.
![]() |
|
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.