Java Reference
In-Depth Information
Exercise 1.10
Select
Show Terminal
from the
View
menu. This shows another window that
BlueJ uses for text output. Then select
Record method calls
from the terminal's
Options
menu.
This function will cause all our method calls (in their textual form) to be written to the terminal.
Now create a few objects, call some of their methods, and observe the output in the terminal
window.
Using the terminal's
Record method calls
function, we can see that the sequence of creating a per-
son object and calling its
makeVisible
and
moveRight
methods looks like this in Java text form:
Person person1 = new Person();
person1.makeVisible();
person1.moveRight();
Here, we can observe several things:
■
We can see what creating an object and giving it a name looks like. Technically, what we are
doing here is
storing the Person object into a variable
; we will discuss this in detail in the
next chapter.
■
We see that, to call a method on an object, we write the name of the object, followed by
a dot, followed by the name of the method. The command ends with a parameter list—an
empty pair of parentheses if there are no parameters.
■
All Java statements end with a semicolon.
Instead of just looking at Java statements, we can also write them. To do this, we use the
Code
Pad
. (You can switch off the
Record method calls
function now and close the terminal.)
Exercise 1.11
Select
Show Code Pad
from the
View
menu. This should display a new pane
next to the object bench in your main BlueJ window. This pane is the
Code Pad
. You can type
Java code here.
In the Code Pad, we can write Java code that does the same things we did interactively before.
The Java code we need to type is exactly like that shown above.
Exercise 1.12
In the Code Pad, type the code shown above to create a person object and
call its
makeVisible
and
moveRight
methods. Then go on to create some other objects
and call their methods.
Typing these commands should have the same effect as invoking the same command from
the object's menu. If instead you see an error message, then you have mistyped the command.
Check your spelling. You will note that getting even a single character wrong will make the
command fail.
Tip
You can recall previously used commands in the Code Pad by using the up arrow.
Search WWH ::
Custom Search