Java Reference
In-Depth Information
1.10
Object interaction
For the next section, we shall work with a different example project. Close the figures pro-
ject if you still have it open, and open the project called house.
Exercise 1.13 Open the house project. Create an instance of class Picture and invoke its
draw method. Also, try out the setBlackAndWhite and setColor methods.
Exercise 1.14 How do you think the Picture class draws the picture?
Five of the classes in the house project are identical to the classes in the figures project. But we
now have an additional class: Picture . This class is programmed to do exactly what we have
done by hand in Exercise 1.9.
In reality, if we want a sequence of tasks done in Java, we would not normally do it by hand as
in Exercise 1.9. Instead, we create a class that does it for us. This is the Picture class.
The Picture class is written so that, when you create an instance, the instance creates two square
objects (one for the wall, one for the window), a triangle, and a circle; moves them around; and
changes their color and size, until the canvas looks like the picture we see in Figure 1.8.
Concept:
The important point here is that objects can create other objects, and they can call each other's
methods. In a normal Java program, you may well have hundreds or thousands of objects. The
user of a program just starts the program (which typically creates a first object), and all other
objects are created—directly or indirectly—by that object.
Method calling .
Objects can com-
municate by call-
ing each other's
methods .
The big question now is this: How do we write the class for such an object?
1.11
Source code
Each class has some source code associated with it. The source code is text that defines the
details of the class. In BlueJ, the source code of a class can be viewed by selecting the Open
Editor function from the class's pop-up menu or by double-clicking the class icon.
Concept:
The source code
of a class deter-
mines the structure
and behavior (the
fields and methods)
of each of the ob-
jects of that class.
Exercise 1.15 Look at the pop-up menu of class Picture again. You will see an option la-
beled Open Editor. Select it. This will open a text editor displaying the source code of the class.
The source code is text written in the Java programming language. It defines what fields and
methods a class has, and precisely what happens when a method is invoked. In the next chapter,
we shall discuss exactly what the source code of a class contains and how it is structured.
A large part of learning the art of programming is learning how to write these class definitions.
To do this, we shall learn to use the Java language (although there are many other programming
languages that could be used to write code).
When you make a change to the source code and close the editor, 3 the icon for that class
appears striped in the diagram. The stripes indicate that the source has been changed. The class
3 In BlueJ, there is no need to explicitly save the text in the editor before closing. If you close the editor,
the source code will automatically be saved.
 
 
 
Search WWH ::




Custom Search