Second Summer 2003

Homework Assignment Three: More Help
1. Practice Four Tigger

Here's a solution:

```class Tigger {
String x, y;
Tigger(int x, int y) {
this.x = x + "";
this.y = y + "";
}
void bounce() {
int a = calculate(x),
b = calculate(y);
this.x = a + "";
this.y = b + "";

}
int calculate(String a) {
int sum = 0;
for (int i = 0; i < a.length(); i++) {
sum += (a.charAt(i) - '0') * (a.charAt(i) - '0');
}
return sum;
}
String report() {
String
x = "   " + this.x,
y = "   " + this.y;
return
"Tigger just bounced to (" +
x.substring(x.length() - 3) + ", "
+ y.substring(y.length() - 3) + ")";
}
} ```
2. Practice Four Paper-Scissors-Rock
```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...");     }
}

class Player {
String guess;
String makeGuess() {
int value = (int) (Math.random() * 3);
if (value == 0) this.guess = "paper";
if (value == 1) this.guess = "rock";
if (value == 2) this.guess = "scissors";
return this.guess;
}
boolean strongerThan(Player other) {
if (this.guess.equals("paper")    && other.guess.equals("rock"    ) ||
this.guess.equals("rock" )    && other.guess.equals("scissors") ||
this.guess.equals("scissors") && other.guess.equals("paper"))
return true;
else return false;
}
String report() {
return guess;
}
}```
3. Practice Four Line
```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());
}
}

class Line {
Point a, b;
Line(Point a, Point b) {
this.a = a;
this.b = b;
}
double length() {
return (this.a).distanceTo(this.b);
}
}

class Point {
double x, y;
Point(double x, double y) {
this.x = x; this.y = y;
}
double distanceTo(Point other) {
double
dX = this.x - other.x,
dY = this.y - other.y;
return Math.sqrt(dX * dX + dY * dY);
}
}```
4. Practice Four Oracle
```class Oracle {
void takeCall() {
System.out.println("Oracle> Hi, ask me any question...");
System.out.println("Oracle> That's a tough one... any words of wisdom that would apply to this?");
System.out.println("Oracle> Nice. Back to your question I'd say: ");
}
String answer = "The answer, my friend, is in the blowing of the wind.";
String advice = " Man gave names to all the animals, in the beginning... ";
}
```
5. Practice Four Elevator
```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);
}
}

class Elevator {
int floor;
Elevator(int floor) {
this.floor = floor;
}
void up(int to) {
if (this.floor >= to) {
System.out.println("Sorry, from floor " + this.floor +
" we can't go up to floor " + to);
} else {
System.out.println("Elevator going up (" +
this.floor +
" --> " + to + ")");
for (int i = this.floor; i <= to; i++) {
this.floor = i;
this.report();
}
System.out.println("Elevator now on floor: " + this.floor);
}
}
void down(int to) {
if (this.floor <= to) {
System.out.println("Sorry, from floor " + this.floor +
" we can't go down to floor " + to);
} else {
System.out.println("Elevator going down: (" +
this.floor +
" --> " + to + ")");
for (int i = this.floor; i >= to; i--) {
this.floor = i;
this.report();
}
System.out.println("Elevator now on floor: " + this.floor);
}
}
void report() {
System.out.println("   The elevator is now on floor " + this.floor);
}
int currentFloor() { return this.floor; }
}```
6. Practice Four `Report` (named here `Historian`) Take a look:
```import java.util.*;

class Historian {
int max = 0;
int min = 0;
int sum = 0;
int num = 0;

void record(int a) {
this.sum += a;
if (this.num == 0) {
this.min = a;
this.max = a;
}
if (this.min > a) this.min = a; // a bit redun-
if (this.max < a) this.max = a; // dant sometimes
this.num += 1; // order of updates is important!
}

void report() {
if (this.num == 0) {
System.out.println("Nothing to report on...");
} else {
System.out.println
(
"  I've looked at " + this.num + " number(s) thus far.\n" +
"  The largest I've seen was " + this.max +
"\n  The smallest was " + this.min +
"\n  The average of the numbers is " +
this.sum / (double) this.num
);
}

}

// testing...

public static void main(String[] args) {
String line;
System.out.println("Welcome to the Historian!");
System.out.print("enter> ");
while (! line.equals("done")) {
StringTokenizer stapler = new StringTokenizer(line);
Historian herodotus = new Historian();
while (stapler.hasMoreTokens()) {
herodotus.record(
Integer.parseInt(
stapler.nextToken()));
}
herodotus.report();
System.out.print("enter> ");
}
}

}```
Note we're getting closer to arrays, but we're not there yet.

Notice also that every `Historian` works with a `stapler`.

Here's a session with this program:

```frilled.cs.indiana.edu%java Historian
Welcome to the Historian!
enter> 1 2 3
I've looked at 3 number(s) thus far.
The largest I've seen was 3
The smallest was 1
The average of the numbers is 2.0
enter> -1 -3 4 2 5 0 -1
I've looked at 7 number(s) thus far.
The largest I've seen was 5
The smallest was -3
The average of the numbers is 0.8571428571428571
enter> Nothing to report on...
enter> 0
I've looked at 1 number(s) thus far.
The largest I've seen was 0
The smallest was 0
The average of the numbers is 0.0
enter> done
frilled.cs.indiana.edu%```
The empty line problem is solved with a count of numbers seen thus far.

Last updated: Jul 15, 2003 by Adrian German for A201