First Summer 2006

Homework Eight: Objects revisited.
Due date
Thursday June 8, 11:59pm
Late policy
Please turn everything on time, late solutions will not be accepted.

Work policy
Working in groups is encouraged but please turn in your own version of the assignment.

• understand, and
• comply

with the Computer Science Department's Statement on Academic Integrity before turning in your assignment.

Write programs that solve the problems below).

Please note that the examples below are in Java and you are to write Python programs.

I will rewrite the examples in Python as soon as possible.

1. Write a program to solve the `Point` problem.

Define a class of objects called `Point` (in the two-dimensional plane).

A `Point` is thus a pair of two coordinates (`x` and `y`).

Every `Point` should be able to calculate its distance to any other `Point` once the second point is specified.

Here's an example of using the class:

```public static void main(String[] args) {
Point a = new Point(3, 0);
Point b = new Point(0, 4);
System.out.println(a.distanceTo(b));
System.out.println((new Point(1, 1)).distanceTo(new Point()));
}```
should produce:
```5.0
1.4142135623730951```

2. Write a program to solve the `Line` problem.

Define a class of objects called `Line`. Every `Line` is a pair of two `Point`s. A `Point` is a pair of two numbers (the `Line`s are also in the plane in these exercises). `Point`s should be able to determine their distance to other `Point`s (see above). Lines are created by passing two `Point`s to the `Line` constructor. A `Line` object must be able to report its `length`, which is the distance between its two end points. Make `length` a method and write a test program in which you create a few `Line`s and ask them to report their lengths. Here's an example of such a test program and its output.

```frilled.cs.indiana.edu%cat One.java
class One {
public static void main(String[] args) {
Line a = new Line(new Point(0, 0), new Point (1, 1));
System.out.println(a.length());
}

}
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
1.4142135623730951
frilled.cs.indiana.edu%```

3. Write a program to solve the `Triangle` problem.

Define a class of objects called `Triangle`. A `Triangle` should be a set of three `Line`s (which for the purpose of this problem should be a very adequate representation). However, a `Triangle` is created by specifying three `Points` (which are located in the plane as discussed above). Using the formula in the book every `Triangle` should be able to calculate and report its area. (If the three `Point`s are collinear the `Triangle` is extremely flat, its area is 0 (zero), and that should be acceptable.)

Here's an example:

```class Experiment {
public static void main(String[] args) {
Triangle a = new Triangle(new Point(0, 0),
new Point(0, 3),
new Point(4, 0));
System.out.println(a.area()); // prints 3 * 4 / 2 (that is: 6 (six))
}
}```
should produce:
`6.0`

4. Write a program to solve the `Clock` problem.

Define a class of objects called `Clock`. An object of type `Clock` stores time (hours and minutes) in military time format, in two instance variables of type `int`. Objects of type `Clock` have two instance methods: `report` which is simply returning the time, and `tick` which advances the clock by one minute. The constructor for class `Clock` takes one argument, a `String` that represents the time the clock is originally set to. Write a test program too, that illustrates how your objects are working. Here's an example of a possible test program and its corresponding output:

```frilled.cs.indiana.edu%cat One.java
class One {
public static void main(String[] args) {
Clock one = new Clock("2350");
one.tick();
System.out.println(one.report());
one.tick();
System.out.println(one.report());
one.tick();
System.out.println(one.report());
one.tick();
System.out.println(one.report());
one.tick();
System.out.println(one.report());
one.tick();
System.out.println(one.report());
one.tick();
System.out.println(one.report());
one.tick();
System.out.println(one.report());
one.tick();
System.out.println(one.report());
one.tick();
System.out.println(one.report());
one.tick();
System.out.println(one.report());
one.tick();
System.out.println(one.report());
one.tick();
System.out.println(one.report());
one.tick();
System.out.println(one.report());
one.tick();
System.out.println(one.report());
one.tick();
System.out.println(one.report());
one.tick();
System.out.println(one.report());
one.tick();
System.out.println(one.report());
one.tick();
System.out.println(one.report());
one.tick();
System.out.println(one.report());

}
}
frilled.cs.indiana.edu%java One
23:51
23:52
23:53
23:54
23:55
23:56
23:57
23:58
23:59
00:00
00:01
00:02
00:03
00:04
00:05
00:06
00:07
00:08
00:09
00:10
frilled.cs.indiana.edu% ```

5. Write a program to solve the `Paper` (and also Rock/Scissors) problem.

Define a class of objects called `Player` that could be used in a Paper Scissors Rock game. Such a `Player` object should have a method, called `makeGuess` that could be used to generate (randomly) one of the following guesses: `"paper"`, `"rock"`, or `"scissors"`. The guess made by this method should be stored in an instance variable as well (a `String`, called `guess`). Another method of class `Player` should be able to compare the choice of the player it belongs to with the choice of any other `Player` and determine if the first player's guess is stronger than the second player's guess. Call this method `strongerThan` and make it return `true` or `false`. A `report` method should return the `guess` instance variable for printing purposes. Here's a possible test program

```class One {
public static void main(String[] args) {

Player bonaparte, wellington;

bonaparte = new Player();
wellington = new Player();

System.out.println("Let the game begin!");

bonaparte.makeGuess();
wellington.makeGuess();

System.out.println("The guesses have been made: ");

System.out.println("  Bonaparte has chosen .... " + bonaparte.report());
System.out.println("  Wellington has chosen ... " + wellington.report());

if (bonaparte.strongerThan(wellington))
System.out.println("Bonaparte wins!");
else if (wellington.strongerThan(bonaparte))
System.out.println("Wellington wins!");
else System.out.println("It's a draw...");
}

}```
and a possible session with this program:
```frilled.cs.indiana.edu%java One
Let the game begin!
Bonaparte has chosen .... paper
Wellington has chosen ... rock
Bonaparte wins!
frilled.cs.indiana.edu%java One
Let the game begin!
Bonaparte has chosen .... scissors
Wellington has chosen ... scissors
It's a draw...

frilled.cs.indiana.edu%java One
Let the game begin!
Bonaparte has chosen .... paper
Wellington has chosen ... paper
It's a draw...
frilled.cs.indiana.edu%java One
Let the game begin!
Bonaparte has chosen .... rock
Wellington has chosen ... paper
Wellington wins!
frilled.cs.indiana.edu%```

As a reminder

• "`rock`" is stronger than "`scissors`",
• "`scissors`" is stronger than "`paper`", and
• "`paper`" is stronger than "`rock`".

Truly, a better name for this problem would be: `Paper-Scissors-Rock` (of course).

Have fun!

6. Write a program to solve the `Elevator` problem.

Design an `Elevator` class that goes up and down in a building with 100 floors.

Here's how the `Elevator` can be tested:

```class One {
public static void main(String[] args) {
Elevator e = new Elevator(20);
e.up(26);
e.down(14);
e.up(10);
e.down(30);
e.up(e.currentFloor() + 3);
}
}```
And here's the output that this would produce:
```frilled.cs.indiana.edu%java One
Elevator going up (20 --> 26)
The elevator is now on floor 20
The elevator is now on floor 21
The elevator is now on floor 22
The elevator is now on floor 23
The elevator is now on floor 24
The elevator is now on floor 25
The elevator is now on floor 26
Elevator now on floor: 26
Elevator going down: (26 --> 14)
The elevator is now on floor 26
The elevator is now on floor 25
The elevator is now on floor 24
The elevator is now on floor 23
The elevator is now on floor 22
The elevator is now on floor 21
The elevator is now on floor 20
The elevator is now on floor 19
The elevator is now on floor 18
The elevator is now on floor 17
The elevator is now on floor 16
The elevator is now on floor 15
The elevator is now on floor 14
Elevator now on floor: 14
Sorry, from floor 14 we can't go up to floor 10
Sorry, from floor 14 we can't go down to floor 30
Elevator going up (14 --> 17)
The elevator is now on floor 14
The elevator is now on floor 15
The elevator is now on floor 16
The elevator is now on floor 17
Elevator now on floor: 17
frilled.cs.indiana.edu%```

7. A Simple Robot.

You have to define a class `Robot` that describes a robot very similar to the one you worked with in the first lab.

To clarify how easy this problem is we need to say that metaphorically your robot is:

• a `Penguin`
• a `Tigger`
• a `BankAccount`
• a `Rectangle`
• a `ConsoleReader`
• a `String`

all at the same time. The `Robot` needs to be able to:

• `turnLeft` (90 degrees)
• `moveForward` (one step)

The `Robot` also needs to remember its location.

Two methods:

• `getX()`, and
• `getY()`

can be used to report the current values of the robot's location.

The location of the robot is a pair ( x , y ) of integers, that describes the position of the robot.

When you create a `Robot` one need specify:

• the location (x, y)
• a name for the robot
• the direction the robot is facing

The robot also needs to be able to:

• report the `direction()` it's facing (N, S, W, E)
• produce a complete `report()` (x, y, name, direction)

Here's a sample program with two robots:

```class Walk {
public static void main(String[] args) {

Robot a = new Robot("Alice", 2, 3, "North");
a.report();

Robot q = new Robot("Queen", -4, -1, "West");
q.report();

a.turnLeft();
a.report();

a.moveForward();
a.report();

a.turnLeft();
a.report();

a.moveForward();
a.report();

a.moveForward();
a.report();

a.moveForward();
a.report();

q.moveForward();
q.report();

q.turnLeft();
q.report();
}
}```
This would produce the following output:
```frilled.cs.indiana.edu%java Walk
Robot Alice located at (2, 3) facing North
Robot Queen located at (-4, -1) facing West
Robot Alice now turns left.
Robot Alice located at (2, 3) facing West
Robot Alice now moves forward.
Robot Alice located at (1, 3) facing West
Robot Alice now turns left.
Robot Alice located at (1, 3) facing South
Robot Alice now moves forward.
Robot Alice located at (1, 4) facing South
Robot Alice now moves forward.
Robot Alice located at (1, 5) facing South
Robot Alice now moves forward.
Robot Alice located at (1, 6) facing South
Robot Queen now moves forward.
Robot Queen located at (-5, -1) facing West
Robot Queen now turns left.
Robot Queen located at (-5, -1) facing South
frilled.cs.indiana.edu%```
Your task is to write `Robot.java` that describes the robots.

As always, please let us know if you have any questions, of course.

8. Write a program to solve the `Tigger` Problem
Nobody bounces like Tigger!

Years of research have finally revealed the special mechanism of Tigger's bouncy step. You are to design a `Tigger` class that implements this unique movement, which I will describe below. (This problem is very similar to the `Robot` problem above and to the `Drunkard` problem of Lecture Notes Thirteen.)

A `Tigger` always starts in a random point (with coordinates `x` and `y`). When it decides to `bounce` a `Tigger` changes its `x` and `y` by the following rule(s):

• `x` becomes the sum of the squares of its digits
• `y` becomes the sum of the squares of its digits

Example:

• if `x` is `37`, then
• one `bounce` turns `x` into `32` + `72` (```= 58```).

Both `x` and `y` change by this rule.

And the `bounce` goes on (as expected).

Here's a possible test of your `Tigger` class:

```frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%cat One.java
class One {
public static void main(String[] args) {
int a = (int)(Math.random() * 1000),
b = (int)(Math.random() * 1000);
Tigger u = new Tigger(a, b);
for (int i = 0; i < 30; i++) {
u.bounce();
System.out.println(u.report());
}
}
}```
And here's the output that the previous program would produce:

```frilled.cs.indiana.edu%java One
Tigger just bounced to (162, 105)
Tigger just bounced to ( 41,  26)
Tigger just bounced to ( 17,  40)
Tigger just bounced to ( 50,  16)
Tigger just bounced to ( 25,  37)
Tigger just bounced to ( 29,  58)
Tigger just bounced to ( 85,  89)
Tigger just bounced to ( 89, 145)
Tigger just bounced to (145,  42)
Tigger just bounced to ( 42,  20)
Tigger just bounced to ( 20,   4)
Tigger just bounced to (  4,  16)
Tigger just bounced to ( 16,  37)
Tigger just bounced to ( 37,  58)
Tigger just bounced to ( 58,  89)
Tigger just bounced to ( 89, 145)
Tigger just bounced to (145,  42)
Tigger just bounced to ( 42,  20)
Tigger just bounced to ( 20,   4)
Tigger just bounced to (  4,  16)
Tigger just bounced to ( 16,  37)
Tigger just bounced to ( 37,  58)
Tigger just bounced to ( 58,  89)
Tigger just bounced to ( 89, 145)
Tigger just bounced to (145,  42)
Tigger just bounced to ( 42,  20)
Tigger just bounced to ( 20,   4)
Tigger just bounced to (  4,  16)
Tigger just bounced to ( 16,  37)
Tigger just bounced to ( 37,  58)
frilled.cs.indiana.edu%```

Isn't this assignment simply tiggerrific?

I am sure you think it is.