Spring Semester 2003

Homework Assignment Four: Adding Up Integers

This is a long assignment. Start early.

In this assignment you will be practicing with loops and arrays (but not only).

1. For 30 points you are to solve the `Add` problem below.

2. For the rest 70 points you are supposed to solve the following,

1. `Report`
2. `Line`
3. `Clock`
4. `Paper`
5. `Tigger`
6. `Oracle`
7. `Elevator`

a collection of 7 (seven) problems listed after the `Add` problem.

Problem 1 (The `Add` problem).

You are to write a program (call it `Add.java`) that works like this:

```frilled.cs.indiana.edu%javac Add.java
N1: 123
N2: 123

123 +
123
----
0246

N1: 123
N2: 901

123 +
901
----
1024

N1: 99999999999999999999999999999999999999999999999999999999999999999999
N2: 1

99999999999999999999999999999999999999999999999999999999999999999999 +
1
---------------------------------------------------------------------
100000000000000000000000000000000000000000000000000000000000000000000

N1: 999999999999999999999999999999999999999999999999999999999999999999999999
N2: 999999999999999999999999999999999999999999999999999999999999999999999990

999999999999999999999999999999999999999999999999999999999999999999999999 +
999999999999999999999999999999999999999999999999999999999999999999999990
-------------------------------------------------------------------------
1999999999999999999999999999999999999999999999999999999999999999999999989

frilled.cs.indiana.edu%```
That is,
1. the program prompts the user for two numbers of any length.

2. Reads those numbers one by one, as `String`'s.

3. Turns the `String`s into arrays of appropriate size.

4. Allocates space for an array that will hold the result (thier sum).

5. Then calculates the result and prints a report.

You are not allowed to use `BigInteger`s!

Grading scale (remember the entire problem is worth 30 points):

• 7.5 points for properly converting the `String`s into arrays
• 3.0 points for allocating enough space for the result
• 4.5 points for doing the basic addition properly
• 4.5 points for also taking care of the carry-overs
• 7.5 points for properly writing up the report (numbers aligned right)
• 3.0 points for getting rid of the occasional leading 0 (zero) in the result
Notice that the program I posted would only get a 27/30 (or 9 out of 10, which is the lowest `A`).

Problems 2-8 (10 points each) are described below:

2. The `Report` Problem

Write a program that reads lines of text from the user very much like your `Vendor` program for Lab Seven. On each line the user is supposed to enter a series of (positive or negative) integer values. Your program should read all the values on the line and immediately write a report. The report should state the largest and the smallest of the values on that line, the sum of all values, and their average, also the number of values strictly greater than 0 (zero), equal to 0 (zero), and strictly lower than 0 (zero). Use `StringTokenizer` to obtain the individual numbers and use the `parseInt` method from class `Integer` (page 108 in the text, or web lecture notes 4) to turn them into numbers that you can work with. Here's how a session with your program might look (like):
```frilled.cs.indiana.edu%java Report
enter> 1 2 3
You have entered 3 numbers.
Their sum is: 6
Their average is: 2.0
There are: 1 even number(s), and 2 odd number(s).
3 number(s) are > 0, and 0 number(s) are < 0.
You have also entered 0 zero((e)s).
Max: 3, min: 1
enter> -3 0 4 0 2 -6
You have entered 6 numbers.
Their sum is: -3
Their average is: -0.5
There are: 5 even number(s), and 1 odd number(s).
2 number(s) are > 0, and 2 number(s) are < 0.
You have also entered 2 zero((e)s).
Max: 4, min: -6
enter>
Empty line? What's that supposed to mean (or, average)!?
enter> 1 2 3 4 0 0 0
You have entered 7 numbers.
Their sum is: 10
Their average is: 1.4285714285714286
There are: 5 even number(s), and 2 odd number(s).
4 number(s) are > 0, and 0 number(s) are < 0.
You have also entered 3 zero((e)s).
Max: 4, min: 0
enter> -4 -3 -2 -1 0 1 2 3 4
You have entered 9 numbers.
Their sum is: 0
Their average is: 0.0
There are: 5 even number(s), and 4 odd number(s).
4 number(s) are > 0, and 4 number(s) are < 0.
You have also entered 1 zero((e)s).
Max: 4, min: -4
enter> done
Thanks for using this program!
frilled.cs.indiana.edu%```
3. 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. `Point`s should be able to determine their distance to other `Point`s (as described in class). 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%```
4. 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");
for (int i = 0; i < 20; i++) {
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. The `Paper` 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).

6. 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 of Homework Two 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%```
7. The `Oracle` Problem
You are to simulate an `Oracle`.

Here's how the program might run:

```frilled.cs.indiana.edu%java One
Oracle> Hi, ask me any question...
Will Jordan make the playoffs?
Oracle> That's a tough one... any words of wisdom that would apply to this?
Life is short. Play hard.
Oracle> Nice. Back to your question I'd say:
***(The answer, my friend, is in the blowing of the wind.)***
Oracle> Hi, ask me any question...
Why did Bob Dylan and Jimmy Hendrix have such different careers?
Oracle> That's a tough one... any words of wisdom that would apply to this?
Once said, never taken back.
Oracle> Nice. Back to your question I'd say:
***(Life is short. Play hard. )***
Oracle> Hi, ask me any question...
Are you crazy?
Oracle> That's a tough one... any words of wisdom that would apply to this?
You seem to make fun of me.
Oracle> Nice. Back to your question I'd say:
***(Once said, never taken back. )***
frilled.cs.indiana.edu%```
... and here's how you might obtain that behaviour:
```frilled.cs.indiana.edu%cat One.java
class One {
public static void main(String[] args) {
Oracle a = new Oracle();
for (int i = 0; i < 3; i++) {
a.takeCall();
}
}
}
frilled.cs.indiana.edu%```
Of course, the `Oracle` is a crooked one.

It goes like this:

1. it takes a question, any question
3. then replies with some general answer
4. ... and keeps the word of advice for the next question
5. it is then ready to take the next call (question)
8. 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%```
Have fun!

Last updated: Mar 9, 2003 by Adrian German for A201