|
First Summer 2006 |
Also, please make sure you
with the Computer Science Department's Statement on Academic Integrity before turning in your assignment.
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.
Write a program to solve thePointproblem.Define a class of objects called
Point(in the two-dimensional plane).A
Pointis thus a pair of two coordinates (xandy).Every
Pointshould be able to calculate its distance to any otherPointonce the second point is specified.Here's an example of using the class:
should produce: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())); }5.0 1.4142135623730951
Write a program to solve theLineproblem.Define a class of objects called
Line. EveryLineis a pair of twoPoints. APointis a pair of two numbers (theLines are also in the plane in these exercises).Points should be able to determine their distance to otherPoints (see above). Lines are created by passing twoPoints to theLineconstructor. ALineobject must be able to report itslength, which is the distance between its two end points. Makelengtha method and write a test program in which you create a fewLines 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%
Write a program to solve theTriangleproblem.Define a class of objects called
Triangle. ATriangleshould be a set of threeLines (which for the purpose of this problem should be a very adequate representation). However, aTriangleis created by specifying threePoints(which are located in the plane as discussed above). Using the formula in the book everyTriangleshould be able to calculate and report its area. (If the threePoints are collinear theTriangleis extremely flat, its area is 0 (zero), and that should be acceptable.)Here's an example:
should produce: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)) } }6.0
Write a program to solve theClockproblem.Define a class of objects called
Clock. An object of typeClockstores time (hours and minutes) in military time format, in two instance variables of typeint. Objects of typeClockhave two instance methods:reportwhich is simply returning the time, andtickwhich advances the clock by one minute. The constructor for classClocktakes one argument, aStringthat 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%
Write a program to solve thePaper(and also Rock/Scissors) problem.Define a class of objects called
Playerthat could be used in a Paper Scissors Rock game. Such aPlayerobject should have a method, calledmakeGuessthat 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 (aString, calledguess). Another method of classPlayershould be able to compare the choice of the player it belongs to with the choice of any otherPlayerand determine if the first player's guess is stronger than the second player's guess. Call this methodstrongerThanand make it returntrueorfalse. Areportmethod should return theguessinstance variable for printing purposes. Here's a possible test programclass 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!
Write a program to solve theElevatorproblem.Design an
Elevatorclass that goes up and down in a building with 100 floors.Here's how the
Elevatorcan 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%
A Simple Robot.You have to define a class
Robotthat 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
Stringall at the same time. The
Robotneeds to be able to:
turnLeft(90 degrees)moveForward(one step)The
Robotalso needs to remember its location.Two methods:
getX(), andgetY()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
Robotone 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:
This would produce the following output: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(); } }Your task is to writefrilled.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%Robot.javathat describes the robots.
As always, please let us know if you have any questions, of course.
Write a program to solve theTiggerProblemNobody bounces like Tigger!Years of research have finally revealed the special mechanism of Tigger's bouncy step. You are to design a
Tiggerclass that implements this unique movement, which I will describe below. (This problem is very similar to theRobotproblem above and to theDrunkardproblem of Lecture Notes Thirteen.)A
Tiggeralways starts in a random point (with coordinatesxandy). When it decides tobounceaTiggerchanges itsxandyby the following rule(s):
xbecomes the sum of the squares of its digitsybecomes the sum of the squares of its digitsExample:
- if
xis37, then- one
bounceturnsxinto32+72(= 58).Both
xandychange by this rule.And the
bouncegoes on (as expected).Here's a possible test of your
Tiggerclass: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.