Top: Index Previous: Typography Up: Index Next: Hello and Resources

CSC8303 -- Bioinformatics Programming in Java

Objects and Classes

This exercise is very similar to the one covered in the lecture notes. There are three keys aims for the exercise:

exclaim
  • For you to gain familiarity with BlueJ.
  • For you to understand what an Object is.
  • For you to understand what a Class is.
act
  • Download the shapes package.
  • Start BlueJ.
  • Using BlueJ, open this shapes project. To do this, you need to unzip it first.

You should now see a screen with a number of boxes on it. Each of these represents a Class in Java. Broadly speaking, programming in Java consists of creating new classes; on the other hand, when you run or execute a program you are creating new Objects. To program in Java, it's essential that you know the difference between the two; in practice, this tends to take a little while, but make sure that you say "class" when you mean class and "object" when you mean object.

Although it may not seem so, you will already been familiar with Objects from general use of computers. You are currently reading this in a web browser; probably on one tab of a browser. This tab is an object; if you start a new tab, then you have another object. It's a different object from the first, it can have a different state (in this case, this means it can be pointing at a different web page). In Java terminology, they are different instances. The act of creating a new instance is called instantiation. Although there are two different objects, they are the same class or type of object. So all the web page tabs do the same sort of thing — they display web pages.

There are often many different objects interacting with each other. Your web browser uses a Window. If you open a file explorer, you get a new Window — you have created a new Object. All windows have some common behaviour: you can resize (most) windows, by dragging the edge; you can close the window; you can minimize it.

This way of thinking about computer programs can take a while to get used to, so don't worry if it doesn't make sense at first. Please read the Java Tutorial which has a nice section on objects and classes to make sure you can get the difference.

Objects and Classes in Java

So what do objects and classes look like in Java and in BlueJ? At its heart, a Java program is a set of source files — basically, lots of text; later in the session we will be creating these files. BlueJ makes things simpler for you and draws pictures representing them; bear in mind that these are not the real classes or objects, though.

act
  • Right Click on "Circle". You should get a set of operations, or methods that you can call.
  • You are looking at a Class here, not an object. There are only a few things that you can do with a class. The most important is to instantiate a new one. Choose new Circle().
  • A red rectangle should appear (confusingly labelled "Circle"). This is an object.
  • Right click on this. As you will see, there are many more things that you can do with an object than a class. Each of these operations is called a method. In this case, choose makeVisible.
  • A new window should pop up with a circle in it.
  • Try changing the size, colour and location of the circle, by calling other methods.
  • Likewise, create a Square and a Triangle.

So, how does this relate to the underlying Java code — which is a set of text files with instructions in it. One of the other things that you can do a Class is look at the source code.

act
  • Right Click on "Circle" and call "Open Editor". You will see the underlying code.
quest
  • Java source code is just a text file. Where would you find this text file? Open it up with WordPad to make sure that you have found it.
  • You have seen that it's possible to call a number of methods on, for example, the Circle class. In the source file, can you find the makeVisible method?
  • BlueJ offers you a set of methods when you right-click. Can you find all of these methods in the source file?
  • Conversely, does BlueJ offer you all of the methods that appear in the source file?
  • Perhaps you think that moveRight is a bit too slow. Can you make it move twice as far with each method call? For this, you will need to use the "Compile" operation.

Compiling

So what does "compiling" mean. As I mentioned earlier, a Java program consists of a set of source code. Source is meant for humans to read and write. But it's not very good for a computer. So, a tool is used to turn the source into something more appropriate. This process is known as compiling.

What actually happens during the compilation process differs between different languages. A language like Perl, for example, doesn't appear to need compiling. In fact, perl simply compiles the source every time it runs. Other languages, like Python, compile automatically when needed. With Java, you have to do this yourself.

The process of compiling is quite useful; the Java compiler performs lots of checks on your code when you compile. While it does not guarantee that your program will work correctly, it does guarantee that some types of error can't happen.

If you look in the shapes directory, you will set lots of files with a .class extension. These are producing by the compiler.

Building a House (automatically)

Next, you are going to build a picture of a house 1. We are also going to automate the whole process. At heart, computer programs are about doing simple things really quickly and without you having to do anything. It's better to teach the computer to build a house once and then let it do it in future.

act
  • Using a series of method calls, build a picture of a house. You will need quite a few method calls to do this. Record the methods that you call. You can do this by writing them down in a text file.
  • MAKE SURE that you show the demonstrator at this point.
  • Create a new class Automate.java. BlueJ will do this for you (right click on the main panel, "New Class").
  • Open the editor for Automate.java. BlueJ puts some sample code in for you, which you want to delete. Try putting the following code.
      public class Automate
      {
        public static void main(String [ ] args)
        {
          // write your method calls here
          Circle sun = new Circle();
          sun.makeVisible();
          sun.changeColour( Colour.YELLOW );
        }
      }
  • Compile the class. This is going to remove all the existing objects and your picture of a house; if you didn't show the demonstrator and record the methods then your work has been lost.
  • Run the main method for the class 2. A yellow circle should pop up.
  • Insert the method calls you have recorded in a text file into Automate.java. Compile again, and call the main method. Your house should magically appear.
  • There is no magic about this; BlueJ is just using the standard Java tools underneath. To show this, we will build a house without BlueJ.
  • Start->Programming Languages->Java->JDK.
  • This should open a command prompt. Move to the same directory as Automate.java.
  • You can run this with java Automate. If you change Automate.java then you can compile it with javac Automate.java.
log
  • Your version of Automate.java is your first piece of coursework for this module.
  • You need to get this signed off by your demonstrator.
exclaim
  • If you did not read the section on comments, then go back and read it now.

1. Sorry about this, I realise that it's a bit twee and may come across as patronising. When you are learning a new human (i.e. real) language, you spend ages feeling an idiot, saying things like "the chair is big" or "I am happy! Are you happy?". Computers are much the same. Have faith! Computers are idiots and their languages are actually much simpler.

2. The astute among you will notice that the main method appears on the class and not in the object. Hopefully, this should come a surprise to you; all the methods in Circle need an object to operate. It's the static keyword which makes the difference here. Don't worry too much about this at the moment.


Top: Index Previous: Typography Up: Index Next: Hello and Resources