CSC 171
Homework 3
Booleans and Conditionals

Assigned Feb 22, due Mar 6 Mar 8

For each method, be sure to follow the design recipe for methods.

For each animation, be sure to follow the design recipe for animations.

Be sure to choose meaningful names for methods and parameters, and watch for opportunities to re-use methods you, I, or Dr. Stemkoski have already written.

Also turn in a log of how many errors of different kinds you encountered in the assignment, with brief comments describing each one ("mismatched parentheses" is self-explanatory, but more complex errors might need more description). You may do this using the PSP forms, or simply by keeping track in a text file or on paper and turning it in.

How to turn this in

You are encouraged to do this in teams of two students; turn in one copy of the project with both names on it (in the README file or in comments in the Java source files). Even if you do the assignment without a partner, make sure your name is on it.

  1. Develop a method named withinDistance that takes in two Posns and a double, and tells whether the Posns are at most the given distance apart. For example, the point (3,4) is at a distance of 5 from the origin, so withinDistance(new Posn(0,0), new Posn(3,4), 5.0) would be true, while withinDistance(new Posn(0,0), new Posn(3,4), 4.9) would be false.

  2. Develop a method named anyTwoSame that takes in three ints, and tells whether any two (or more) of them are equal.

  3. Develop a method named between that takes in three doubles, and tells whether the first one is strictly between the second and third. (For a first attempt, you may assume that the second is less than the third. Once you've got that working, try to drop that assumption. How many test cases do you need?)

  4. Develop a method named smallestOf3 that takes in three numbers and returns the smallest of the three. Try to do this without using the built-in Math.min method, using conditionals instead. Then try it using Math.min (look it up if necessary).

  5. Develop a method countVotes4 that takes in five Strings. The first is the name of a candidate; the other four are votes. The method should tell how many of the four votes are for the specified candidate. For example, countVotes4("Ann", "Bob", "Ann", "Charlie", "Bob") should return 1 because one of the four votes was for Ann.

    Hint: If you try to do this in one method, it'll be horribly long and complicated. Try writing a helper method that deals with just one vote at a time, then combine the results.

  6. Develop a method digitalThermometer that takes in a temperature (in degrees Fahrenheit) and produces a digital read-out of the temperature, colored either green (below 99 degrees), yellow (at least 99 but less than 101), or red (at least 101 degrees).

  7. Develop an animation of a rocketship that starts at the top of the screen, descends gradually, and stops moving when its bottom edge reaches the bottom of the screen.

  8. Develop an animation of a traffic light: it displays a solid circle of a color, which cycles every second from red to green to yellow to red to ...

    Note: You have several reasonable choices for "what type is the model?" In the past, some students have used a String ("red", "green", "yellow"), others have used a Color (Color.red, Color.green, Color.yellow), while still others have used an int (1, 2, 3). If you use a String or a Color, remember to compare using .equals() rather than ==.

    (A more elaborate, extra-credit version: it displays three disks arranged vertically. At all times, two of the three are black, and the other is a color: red appears at the top, yellow in the middle, and green at the bottom.)

  9. Develop an ImageMap named SimpleColors that "simplifies" the colors in the given image:

  10. Three candidates (Anne, Bob, and Charlie) are running for mayor of Javaville. Develop a method whoWon that takes in three numbers (the number of votes for Anne, the number for Bob, and the number for Charlie) and returns the name of the winner, either "Anne", "Bob", or "Charlie". If two or more candidates tie for first place, return the word "tie".

    Choose your test cases carefully!

  11. Develop a method fourVotesToWinner that takes in four strings representing votes, and returns the name of the winner (or "tie" if there's a tie for first). Assume that the only candidates in the race are Anne, Bob, and Charlie.

    Hint: A good programmer is lazy! Re-use previously-defined methods.

  12. Develop an animation that places a small dot at the mouse location every time the mouse is clicked. The dot should be green if the mouse location is within 50 pixels of the center of the window, and red otherwise.

  13. Develop an animation that moves an image of your choice left and right (staying at a fixed y coordinate):

  14. Develop an animation that allows the user to "type" into the animation window: every time the user types an ordinary character, that character is added to the right-hand end of the text in the animation window. The animation should ignore arrow keys. Note that it'll all have to fit on a single line; handling multiple lines is more complicated. Also note: all the "ordinary" keys (letters, digits, punctuation marks, etc.) are given to gotKeyEvent as length-1 strings, while the arrow keys show up as longer strings ("left", "up", etc.)

  15. Develop an interesting animation of your own, using conditionals to make it more interesting. Go wild.

Grading standards

Error log:       /10
(I'm not grading on how many or how few errors you encountered, only on whether you recorded them adequately.)

Programming: To save time and get you feedback more quickly, we won't grade all of the problems, but a selection of them. Each ImageMap requires writing a class with one or two methods, and each animation requires writing a class with two or more methods.

General skills:

Following directions /10
Writing contracts from word problems /10
Choosing test cases /10
Choosing names; readability /10
Coding /10
Code re-use and function composition /10

Last modified:
Stephen Bloch / sbloch@adelphi.edu