Homework #5 | CMSC 131 |
Due March 9th, 2003 | Object-Oriented Programming I |
Spring 2004 |
This homework will give you further practice writing simple expressions and statements in Java. By now you should be getting used to keeping a log of how you spend your time. You will be graded as follows:
The application that you'll be playing with this week is a simple drawing application that resembles a child's toy called an Etch-A-Sketch. In case you are not familiar with an Etch-A-Sketch, here is a brief description:
An Etch-A-Sketch is a rectangular frame with a silver screen in the center and two white knobs, one in each of the lower corners. Inside the silver screen is a point of darker gray. As you turn the knobs, the darker gray point moves around the screen, leaving a darker gray trail behind it. By controlling the knobs carefully, you can draw pictures.
Each knob controls one direction of motion of the darker grey dot. Rotating the left knob moves the dot from side to side. Rotating the right knob moves the dot up and down. By rotating just one knob -- by leaving the position of the other knob fixed, or constant -- you can draw a straight (horizontal or vertical) line. By rotating both knobs at appropriately coupled velocities, you can draw diagonal lines of varying slope.
In this exercise, we will perform similar operations on a similar display. Behind the scenes, another bit of code (similar in spirit to the Timer we made in class) will check the position of each knob and update the position of the dot ten times per second.
Your job will be to write the instructions for the dot position. The interesting thing is that whatever instruction you write will be executed repeatedly. If your instruction always gives the same value, it will be as though that knob is stuck in one position. If your instruction changes the value, the knob's position will get that new value, and thus the dot will move. To keep the knob moving, you'll have to keep generating different values.
In your application, as in the Etch-a-Sketch, there is a blank screen with a dot on it. When the dot moves, it leaves behind a trail. The motion of the dot is controlled by a controller that you will write. The controller consists of a rule that gets called over and over again. Each time it gets called, it must specify the new position of the dot. In addition, the etch-a-sketch might change the position of the dot independently of your rule (if a user clicks on the Etch-a-sketch with the mouse or presses the arrow keys.) The control rule is automatically invoked by the application; your job is simply to write down appropriate control rules. A control rule is specified by implementing the EtchASketchAnimator interface, and by telling the EtchASketch application about your animator callback method. The callback contains one method:
public Point getPoint(Point currentPos);
getPoint() must return the new position of the dot given the time step of the animation and the current position of the dot (which is always the value that was returned on the previous animation step). EtchASketch uses the same coordinate system as images. The origin is in the top-left corner, with X increasing to the right, and Y increasing to the bottom. The first time getPoint is called, currentPos will hold the point (100, 100). The dimensions of the drawing area are available through the etchASketch.getDrawableWidth() and etchASketch.getDrawableHeight() methods. You can see a couple of simple controllers (Dot and Hor) that are distributed with the EtchASketch program, and can be run with the EtchASketchDemo class.
We will be using CVS with AutoCVS as we have for previous assignments. There is an updated version of AutoCVS that fixes a few bugs that people experienced previously with it. The new version (0.0.5) has been installed on WAM. If you are using a home computer, please download autocvs-0.0.5.zip and install it as follows. (autocvs-0.0.5 is quite small and shouldn't be a problem to download, even over dialup phone lines).
You can access the assignment code by checking it out from CVS with Eclipse (it is called 'hw5'). You can read the API documentation for more info.
You can test your classes with the supplied EtchASketchSolution class.
Submit your program by right-clicking on your project and select "Submit Project". Note that this option will only appear if you have successfully installed AutoCVS as described previously. If you decide that you would like to make a change after you have submitted your project, you can resubmit as many times as you like before the extended deadline. We will grade the last project submitted.
/* Name * Student ID * Homework #5 */ <your code goes here> |
This is an "open" assignment. You are free to get whatever help you want to get this assignment done. You may look on the web, and talk to friends. You can even look at other student's solutions or have other students help you debug your code. But there are two conditions:
You must document all outside help you get in your timelog. If you have any non-trivial interaction with anyone about this homework, you must describe it (i.e., "<student xxx> helped me debug my program for 2 hours"). You will not be penalized for this help, but we want to know how much help people are getting to do this work - and we want you to be explicitly aware of how much help you are getting.
You are responsible for understanding the code you turn in. We expect you to know what you did as if you did it entirely privately. You need to be able to do this level of work at this point in the semester if you expect to succeed in following projects and in exams.
For an extra challenge, implement a bouncing ball. This
problem will not be graded. The ball should move continuously
to the right (as with Hor) but fall to the
bottom with increasing speed (i.e., with acceleration). When it hits
the bottom of the screen, it's speed should remain constant, but it should
bounce up (similar to DiagBounce). In
order to have the ball bounce with decreasing height, there must be friction
which should slow the velocity by a fixed percentage. See example
screenshot below. Your constructor should have the signature:
public Ball(int theWidth, int
theHeight);
This project is derived from Lynn Stein's www.cs101.org Project 1.