CSC 171
Homework 4
Posns, Colors, and User-defined Classes

Assigned Mar 6, due Mar 22 25

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. Modify the Pacman animation from class to wrap around at the edges of the window: when it moves off the right-hand edge, it reappears at the left-hand edge, and vice versa, and when it moves off the top edge, it reappears at the bottom, and vice versa.

  2. Write an animation in which a colored dot jumps around the screen randomly, every half second, not leaving a trace of its old locations. If the user clicks the mouse inside the dot, the animation ends. If the user clicks the mouse anywhere else, it has no effect.

  3. Define a data class Candidate to represent a candidate in an election, with two fields: the candidate's name and the number of votes.
    Provide a constructor that takes in values for the two fields, a toString method, a same method, and a getter for each field. Write a test class that creates several instances of the class and calls the various methods on them.

    Define a static method whoWon in this class that takes in three Candidate parameters and returns the name of the one with the most votes, or "tie" if there was a tie for first place.
    Note: this resembles a problem from homework 3, but rather than assuming the first candidate is named "Anne", the second "Bob", and the third "Charlie", it works on Candidates with any names.

    Add a method named addAVote to the Candidate class: it takes no parameters, and returns a new Candidate with the same name and one more vote than before.

  4. Write an animation like the one in problem 2, but keeping track of the elapsed time (in the top left corner of the screen) and the number of clicks (in the top right corner). You'll need to write a new data class for the model, and a new animation class. When the user clicks inside a dot, the final screen should show (appropriately labeled) how many seconds and how many clicks it took.

  5. Write an animation in which a small picture moves with the mouse over a large fixed background picture, and every time the user clicks the mouse, a copy of the picture is left behind at the mouse location. Something like this (which was actually written in a different language, but it's the same exercise).

  6. Define a data class named PlacedCircle which represents a mathematical circle, with a radius and the location of the center. As before, you should write a constructor, a toString method, a same method, getters, and a test class.
    Note: this class has nothing to do with WorldImages; it's just a mathematical construct.

    Add a method area: it should take no parameters, and return the area of the circle.

    Add a method contains: it should take a Posn parameter, and return a boolean indicating whether the Posn is inside the circle.

    Add a method overlaps to the PlacedCircle class: it should take another PlacedCircle as a parameter, and return a boolean indicating whether the two circles overlap.

  7. Write an animation of a dot that moves around the screen at a constant velocity until it hits the top, bottom, left, or right edge, at which point it "bounces off".
    Hint: When you hit, say, the right-hand wall, the x-coordinate of velocity should change, while the y-coordinate of velocity shouldn't.
    Hint: You may find it easier to break your tick handler into three methods: one to move the dot, one to decide whether it should bounce in the x dimension, and one to decide whether it should bounce in the y dimension.

    Add a "rocket" feature: if you press any of the arrow keys, the dot accelerates (changes velocity) in the specified direction.

    Change the picture so it isn't just a dot, but rather a rocket-ship pointing in the direction that it's currently moving. (What should you do if it's not moving?)

  8. Write an animation similar to the "typing" problem from homework 3, except that there's a vertical-bar cursor showing where you're currently typing. The left and right arrow keys move the cursor left and right (unless it's already at the beginning or end, respectively, of the string). Typing a letter key should insert it at the cursor location, and move the cursor to be after the new letter.

  9. Write a painting program with a "palette" containing four colored panels down the left-hand side, and a "picture region", initially all white, filling the rest of the window. When you click on any of the colored panels, a dot of that color starts moving with the mouse, and when you click the mouse anywhere in the picture region, the dot is left there; then you can go on and add more dots of that color, or pick up a different color and add some different-colored dots.


  10. Write an interesting animation whose model is a user-defined type. Go wild.

Grading standards

Error log:       /50
(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 animation requires writing a class with two or more methods; in some cases you'll also need to write a new class to be the model.

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