Instruction 6 Exercises

7
Instruction 2ip90 – Objects
7.1
Leesplankje
1. Download the file Letter.java from the course website and put it in the same folder as where
you will put your Java files of this instruction. You don’t have to read this file. It contains the class
Letter and its use will be explained below. Objects of the class Letter will appear as small windows
on your screen and contain one letter.
2. Load the file in DrJava and compile. Then test it by the following command in the Interactions panel:
Letter letter = new Letter(’a’);
3. Create another letter by
Letter letter2 = new Letter(’b’);
You can drag the windows with your mouse to form a row. Instead, have your program do this. To
position a letter window, use the method
void moveRight(), which moves the letter window 10 pixels to the right.
or
void moveRel(int x, int y), from Relative, which moves the letter window x pixels to the
right and y pixels down.
So, letter.moveRel(0,50);
moves the letter ‘a’ defined above 50 pixels down on your screen.
Note When you give a command in DrJava, the Letter windows may be hidden behind your DrJava
window. You may want to make the DrJava window smaller and move it away from where the action
is.
4. Create a neat row of letters forming the word aap. Note: “aap” is the first word to be learned in a
traditional Dutch reading school reading method. It means “monkey”. The next word in the method
is “nut”.
Figure 1: Example result of running exercise 4
5. Methods of the class Letter are:
void
void
void
void
void
void
void
void
void
setText(char c)
setColor(Color c)
setFontSize(int fontSize)
moveRight()
moveLeft()
moveUp()
moveDown()
moveRel(dx, dy)
center()
changes the text in the window
changes the background color of the window (See below)
changes the font size (in pixels) of the text
move the letter window 10 pixels to the right;
move the letter window 10 pixels to the left;
move the letter window 10 pixels up;
move the letter window 10 pixels down;
moves the letter window dx pixels to the right and dy pixels down;
move the letter window to the center of the screen;
1
Experiment with these methods on some letter windows. Color expressions that can be used here are
Color.GREEN, Color.PINK, etc. Example:
letter.setColor( Color.GREEN );
changes the background of the first letter (‘a’) to green. See http://docs.oracle.com/
javase/7/docs/api/java/awt/Color.html for a list of color names.
Before you apply the method setColor, give the command import java.awt.Color; in the
Interactions pane.
6. Create, all from the interactions pane and not using your mouse, the following configuration:
Figure 2: Target configuration (background image may differ)
7. Try out the following piece of code to see that two variables can hold the same address (aka aliasing).
Letter x;
Letter iks;
x = new Letter(’x’);
iks = x;
x.moveRight();
iks.moveLeft();
Explain the results.
8. Make an array of Letters that shows Letter objects in a row. Write code that moves the complete row
10 pixels down.
9. * Add a method to Letter that moves the object in a random direction. Use the method Math.random();
that returns a random value of type double between 0 and 1 (including 0, excluding 1). E.g.,
double r = Math.random();
if (r < 0.25) {
...// move right
} else if (r < 0.5) {
...// move left
etc.
10. Create an animation of dancing letters. Use, a.o., the pause method. The command Letter.pause(2.0);
pauses the execution of the program for 2 seconds.
2
7.2
Rectangle
Download the file Rectangle3.java form the course website. Open in DrJava and compile. Load in
CoffeeDregs. Execute the program. Observe how the objects get their data.
7.3
Hotel
Make a hotel with rooms and guests. A room can be occupied or empty.
1. Write three classes: Guest, Room, and Hotel.
• Guest contains a variable of type String to store the name of the guest. Write a constructor for
Guest with a parameter n to initialize the name.
• Room contains a variable guest of type Guest. When a room is empty, guest should have value
0.
• Hotel contains an array of rooms; make the array in the constructor. Let the hotel have 10
rooms.
2. Extend the hotel with a method printRooms. This method puts for each room the room number and,
if there is a guest, the name of the guest. Make a demo that instantiates three guests, Peter, Rob and
Jing, and puts them in their rooms. Then call printRooms.
3. Extend the hotel with a method moveGuest(Guest g, Room newroom). The method searches the
array for the room with guest g, empties that room and puts the guest in room newroom. Extend the
demo. Move Peter and Rob to rooms 5 and 9 respectively, using the moveGuest method. Again call
printRooms.
3