Copyright © Cay S. Horstmann 2009 
This work is licensed under a Creative Commons
Attribution-Noncommercial-Share Alike 3.0 United States License.
In this lab, you will work in groups of two.
Put the answers to the questions in each step into the lab report. Copy/paste the programs that you write in the lab.
This icon indicates optional tasks. Do those if you have time.
ClocksComponent class. In its paintComponent
method, add a loop that draws all clocks in the clocks array
list. Run the program. What happens?Make a new class ColoredClock that extends
Clock. In the ClocksComponent, change one of the
two clocks to ColoredClock. Compile and run. (To make a new
class in Netbeans, go to in the Project tree at the left, expand the
project, then Source Packages, right-click on the "default package" icon,
then select New -> Java class.)
draw method. Add a draw method to the
ColoredClock class. To get started, let's draw a yellow
background:
public void draw(Graphics2D g2)
{
Color c = Color.YELLOW;
g2.setColor(c);
g2.fill(getBounds());
g2.setColor(Color.BLACK);
}
(The getBounds method returns the rectangle bounding the
clock.)
Run your program. What happens?
super.getHours to get the current hours. If they are between 6
(inclusive) and 18 (exclusive), then draw a yellow background. Otherwise,
draw a blue background. Compile and run. What is your code?update method updates
the current time. Let's override it so that it adds 12 to the hours:
public void update()
{
super.update();
hours = (hours + 12) % 24;
}
Compile. What happens? Why?
Clock class to fix this
method. Compile and run. What happens?
When you are done, remove that update method (or comment it out by
surrounding it with /* and */).
A WorldClock differs from a Clock in two ways:
Make a class WorldClock that extends Clock.
Add instance variables label and offset. Make a
constructor that sets them. Add a new WorldClock("New York",
-3) and new WorldClock("London", -8) to the
ClocksComponent. Compile and run.
draw method of WorldClock that contains a call
Rectangle bounds = getBounds(); double x = bounds.getX(); double y = bounds.getY(); g2.drawString(label, (int) x, (int) y);
Compile and run. What happens? (If one of the labels doesn't show up, move the clock down a bit.)
draw method so that it shows the time and
the label. update method, protect
against taking the % of a negative number. For example, if it
is currently 6:00 in San Francisco, the time in London is 21:00, but (6 -
9) % 24 would be -3. Fix this and test by calling the
toString method. WorldClock constructor.Observe that the beepers slowly fade.

Now look at the code. Note that both Robot and
Beeper extend the Actor class. Which method of
Actor do they override?
ActorScene class has an infinite loop like this:
while (true)
{
for (Actor a : actors)
a.act()
}
In other words, the scene calls the act method on all
actors that have been added to the scene, then does it again and again.
(Full disclosure: If you look into the run method of
ActorScene, the code looks a bit more complex because it runs
the act methods in parallel. That way, if you have two robots,
they move together. You should not have to worry about that—it just
makes the animation more realistic.)
Actors act in different ways. Read through the code of
Robot.act and Beeper.act and describe in plain
English how they act.
SquareDancer of Robot with no
methods, like this:
public SquareDancer extends Robot
{
}
What error message do you get?
Robot class. What constructors does it have?SquareDancer:
public SquareDancer extends Robot
{
SquareDancer(double x, double y)
{
}
}
Does the error message go away? If so, why? If not, why not?
x and y values to the
Robot constructor. How do you do that? (Hint:
super.) Complete the constructor. Your class should now
compile.
Add an instance variable to count the steps. Implement the
act method. What is your method?
setup method of MyScene so that you
add a SquareDancer instead of a Robot. Compile
and run. Attach a screen shot to your lab report.
Tip: If you add multiple robots to your scene, such as
a of Robot and SquareDancer, then
a1.getModel().setColor(Color.BLUE);. (You need to
import org.alice.apis.moveandturn.Color.)TimidRobot of
Robot that acts like a robot, except if a timid robot has
moved within 1 unit of an object other than a beeper, it makes a half turn.
Position a TimidRobot between two Toaster
objects. What is the code for your TimidRobot class?
Hint: Use the getNeighbors method to get neighbors within
one unit, then check whether there is at least one object that is not an
instance of a Beeper.
Dino of Actor
(not Robot). As you know, dinos love to eat toasters. And they
have great vision. In its act method, a Dino
first locates the nearest toaster within 10 units and changes its direction
towards it. If there is a toaster within 0.5 units, the dino eats it.
Otherwise it moves one unit. What is your code for the Dino
class? (Follow the Toaster class for guidance how to
initialize the model and change to a reasonable size. The model is a
Trex If you type Trex and Ctrl+Space, you should get
autocompletion for the package and class name in the Alice library.)