This exercise is very similar to the one covered in the lecture notes. There are three keys aims for the exercise:
|
|
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.
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.
|
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.
|
|
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.
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.
public class Automate { public static void main(String [ ] args) { // write your method calls here Circle sun = new Circle(); sun.makeVisible(); sun.changeColour( Colour.YELLOW ); } }
|
|
|
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.