Java Reference
In-Depth Information
now needs to be compiled by clicking the Compile button. (You may like to read the “About
compilation” note for more information on what is happening when you compile a class.) Once
a class has been compiled, objects can be created again and you can try out your change.
About compilation
When people write computer programs, they typically use a “higher-level” programming language
such as Java. A problem with that is that a computer cannot execute Java source code directly.
Java was designed to be reasonably easy to read for humans, not for computers. Computers, in-
ternally, work with a binary representation of a machine code, which looks quite different from Java.
The problem for us is that it looks so complex that we do not want to write it directly. We prefer to
write Java. What can we do about this?
The solution is a program called the compiler. The compiler translates the Java code into machine
code. We can write Java and run the compiler—which generates the machine code—and the
computer can then read the machine code. As a result, every time we change the source code, we
must first run the compiler before we can use the class again to create an object. Otherwise, the
machine code version that the computer needs will not exist.
Exercise 1.16 In the source code of class Picture , find the part that actually draws the
picture. Change it so that the sun will be blue rather than yellow.
Exercise 1.17 Add a second sun to the picture. To do this, pay attention to the field defini-
tions close to the top of the class. You will find this code:
private Square wall;
private Square window;
private Triangle roof;
private Circle sun;
You need to add a line here for the second sun. For example:
private Circle sun2;
Then write the appropriate code for creating the second sun.
Exercise 1.18 Challenge exercise (This means that this exercise might not be solved
quickly. We do not expect everyone to be able to solve this at the moment. If you do, great.
If you don't, then don't worry. Things will become clearer as you read on. Come back to this
exercise later.) Add a sunset to the single-sun version of Picture . That is, make the sun go
down slowly. Remember: The circle has a method slowMoveVertical that you can use to
do this.
Exercise 1.19 Challenge exercise If you added your sunset to the end of the draw method
(so that the sun goes down automatically when the picture is drawn), change this now. We
now want the sunset in a separate method, so that we can call draw and see the picture with
the sun up, and then call sunset (a separate method!) to make the sun go down.
Exercise 1.20 Challenge exercise Make a person walk up to the house after the sunset.
 
Search WWH ::




Custom Search