Homework #5 CMSC 131
Due March 9th, 2003 Object-Oriented Programming I
  Spring 2004

 

Etch-a-sketch

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. 

Getting the Code Distribution

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.

Your Assignment

  1. Write a controller that draws a diagonal line (see picture below). Your constructor must have the signature:
        public Diag();

  1. Write a controller that draws a zig-zag line (see picture below).  That is, it should start like Diag, but must change direction up-right, then down-right, then up-right, etc.  It must stay within the horizontal band bounded by (Y >= 100) and (Y <= 200). Your constructor must have the signature:
        public ZigZag();

  1. Write a controller that draws a Sine wave (see picture below).  It should look similar to ZigZag, but should be curved (and actually plot a sine wave).  You will want to use the Math.sin(double a) method which takes an input angle (in radians) and returns the Sine of that angle (in the range [-1, 1]).  You should scale the input and output appropriately so it draws a clearly visible Sine wave. But the precise frequency and amplitude that you draw is not important - so your picture does not have to look exactly like the one pictured below. Your constructor must have the signature:
        public Sine();


    Don't forget that when doing computations, if all of the elements of an expression are integers, then the result will be an integer, and it will be truncated.  To force an expression to be computed using floating point arithmetic, make sure that the first element is a floating point number (i.e., "100.0" instead of "100"), or cast the first element of the computation to a floating point number (i.e., "(double)i / j").

  1. Reimplement the diag line as above, but this time, when the line reaches any edge of the etch-a-sketch, it must wrap to the opposite side.  For example, if the line goes off the bottom edge of the screen, it should reappear at the top (meaning the Y value gets set to 0, and the X value doesn't change). As you can see in the figure below, this will result in a line being drawn from one side of the screen to the other when it wraps - this is OK. To implement this, you will need to know the bounds of the etch-a-sketch, so the constructor takes the dimensions of the etch-a-sketch as parameters. Your constructor must have the signature:
        public DiagWrap(int theWidth, int theHeight);


 

  1. Reimplement the diag line one last time, but this time, when the line reaches any edge of the etch-a-sketch, it must bounce off the edge and keep going.  For example, if the line starts to go off the bottom edge, it must switch direction and start going up (see picture below). Your constructor must have the signature (where the parameters hold the dimensions of the etch-a-sketch object):
        public DiagBounce(int theWidth, int theHeight);

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>

Open Assignment

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:

  1. 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.

  2. 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.

Challenge Problem

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.