|
Spring Semester 2003 |
This
is a long assignment. Start early.
In this assignment you will be practicing with loops and arrays (but not only).
Add problem below.
Report
Line
Clock
Paper
Tigger
Oracle
Elevator
a collection of 7 (seven) problems
listed after the Add problem.
Add problem).
You are to write a program (call it Add.java) that
works like this:
frilled.cs.indiana.edu%javac Add.java
frilled.cs.indiana.edu%java Add
N1: 123
N2: 123
123 +
123
----
0246
frilled.cs.indiana.edu%java Add
N1: 123
N2: 901
123 +
901
----
1024
frilled.cs.indiana.edu%java Add
N1: 99999999999999999999999999999999999999999999999999999999999999999999
N2: 1
99999999999999999999999999999999999999999999999999999999999999999999 +
1
---------------------------------------------------------------------
100000000000000000000000000000000000000000000000000000000000000000000
frilled.cs.indiana.edu%java Add
N1: 999999999999999999999999999999999999999999999999999999999999999999999999
N2: 999999999999999999999999999999999999999999999999999999999999999999999990
999999999999999999999999999999999999999999999999999999999999999999999999 +
999999999999999999999999999999999999999999999999999999999999999999999990
-------------------------------------------------------------------------
1999999999999999999999999999999999999999999999999999999999999999999999989
frilled.cs.indiana.edu%
That is,
String's.
Strings into arrays of appropriate size.
You
are not allowed to use BigIntegers! Grading scale (remember the entire problem is worth 30 points):
Strings into arrays
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 your3. TheVendorprogram 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). UseStringTokenizerto obtain the individual numbers and use theparseIntmethod from classInteger(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 Welcome, please start entering numbers. enter> 1 2 3 Thank you. Here's your report: 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 Thank you. Here's your report: 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 Thank you. Here's your report: 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 Thank you. Here's your report: 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%
Line Problem
Define a class of objects called4. TheLine. EveryLineis a pair of twoPoints. APointis a pair of two numbers.Points should be able to determine their distance to otherPoints (as described in class). 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%
Clock Problem
Define a class of objects called5. TheClock. 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"); 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%
Paper Problem
Define a class of objects called6. ThePlayerthat 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
Truly, a better name for this problem would be:
- "
rock" is stronger than "scissors",- "
scissors" is stronger than "paper", and- "
paper" is stronger than "rock".Paper-Scissors-Rock(of course).
Tigger Problem
Nobody bounces like Tigger! Years of research have finally revealed the special mechanism of Tigger's bouncy step. You are to design a7. TheTiggerclass that implements this unique movement, which I will describe below. (This problem is very similar to theRobotproblem of Homework Two 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):Example:
xbecomes the sum of the squares of its digitsybecomes the sum of the squares of its digits
- 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%
Oracle Problem
You are to simulate an8. TheOracle.Here's how the program might run:
... and here's how you might obtain that behaviour: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%Of course, thefrilled.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%Oracleis a crooked one.It goes like this:
- it takes a question, any question
- then asks for some words of advice
- then replies with some general answer
- ... and keeps the word of advice for the next question
- it is then ready to take the next call (question)
Elevator Problem
Design anElevatorclass 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:Have fun!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%