|
Spring Semester 2004 |
But the drop box will stay open until Wed the end of the day.
So you have one more day to work on it or finish it up.
Also, please make sure you
the Computer Science Department's Statement on Academic Integrity before turning in your assignment.
1. 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. The Line problem.
Define a class of objects called
Line. Every Line is a pair of
two Points. A Point is a pair of
two numbers (the Lines are also in the plane in
these exercises).
Points should be able to determine
their distance to other Points (see above).
Lines are created by passing two Points
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 Lines 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. The Triangle problem.
Define a class of objects called Triangle. A Triangle should
be a set of three Lines (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 Points 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. 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. 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%
6. 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! The guesses have been made: Bonaparte has chosen .... paper Wellington has chosen ... rock Bonaparte wins! frilled.cs.indiana.edu%java One Let the game begin! The guesses have been made: Bonaparte has chosen .... scissors Wellington has chosen ... scissors It's a draw... frilled.cs.indiana.edu%java One Let the game begin! The guesses have been made: Bonaparte has chosen .... paper Wellington has chosen ... paper It's a draw... frilled.cs.indiana.edu%java One Let the game begin! The guesses have been made: 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!