CSE 142 Summer 2001

Homework #4

Due: Electronic turnin by 11:00 am, Monday, July 16, 2001

 

Purpose

In this assignment, you'll create a small class that contains instance variables, constructors, and methods, including some value-returning (non-void) methods.

Problem Statement

Create a class Puck that implements a simple pen-like graphical object.  A Puck is similar to the Pen class we encountered in homework 1.  The Puck has a current position and color, and its pen can be either up or down.  Class Puck includes methods to move to a new position, either using absolute coordinates, or relative to its current location.  Unlike the Pen class, a Puck does not have a current angle - movement is given by x and y coordinates, not a direction and a distance.  Also, unlike class Pen, a Puck has an associated GWindow that it uses for drawing; it is not added to a separate GWindow.  Although a Puck is similar to a Pen, do not use class Pen in your implementation of Puck.  Use instances of class Line to draw line segments when needed.

The summary part from a JavaDoc formatted specification for class Puck is available here (attached to the end of this writeup in the printed version).  Your job is to implement class Puck and its methods as specified.  Use the class and method names as given; don't change them.

Class Puck should have two constructors.  Both constructors should create a new GWindow for the Puck to draw in and initialize the Puck with the pen down and current color set to black (Color.black).  One constructor has no parameters, and should initialize the Puck location to the center of the GWindow (200,200).  The other constructor should have three parameters specifying the initial x and y coordinates of the Puck and whether the pen is initially up or down.

Two methods should change the position of the puck.  moveTo(x,y) should move the puck to the given coordinates.  move(deltax,deltay) should move the puck the given distance in the x and y directions from its current location.  In either case, if the pen is currently down, a line should be drawn from the Puck's previous location to its new location using the current color.  If the pen is up, the Puck should move without drawing anything.

Methods penUp and PenDown raise and lower the pen, respectively.  Method penIsDown should return true if the pen is down and false if it is up.  Method getColor returns the current color of the pen, while setColor should change the color.

Finally, class Puck should include a method called test.  When this method is called, it should perform a test of the other methods by calling them to move, change the pen color and up/down status, and so forth, creating some sort of drawing using the Puck.  Part of your job in this assignment is to design this method so it performs a thorough test of the other methods.

The class will need to include appropriate instance variables to remember things like the current Puck location, pen color, and pen status (up or down).  Be sure to make methods and instance variables public or private as appropriate so that clients who use the Puck class only have access to items that should be in the public interface.

Implementation suggestions

A mostly empty Puck.java file is available to start the assignment.  Download this file and use it to create a new project in BlueJ.  Add the instance variables, constructors, and methods that you need to implement Puck as specified.  None of the code in this assignment is particularly complicated; the main issues are how to organize the class and the members in it.

Here are a few specific suggestions:

  1. When a Puck is created (constructed), it should create a new GWindow to be used for drawing.  As the Puck moves, you can draw lines by adding new Line objects to that GWindow.
  2. Your job will be easier if you implement a few things at a time and check that they work properly, then add to class Puck until it is complete.  For instance, you might start with a single no-argument constructor that creates a black pen in the center of the window, then implement moveTo next.  That's enough to see if you're able to draw lines and update the Puck's location.  Then you could add things like pen up/pen down, color changes, and moves relative to the current Puck location.
  3. There is a slight technicality if you try to test setColor using BlueJ's popup method calls (right clicking on an object).  The issue is that if you put something like Color.blue in the popup window, it won't be recognized because it's not inside a Java file that contains "import java.awt.*;" at the beginning.  You can test setColor using the popup method call window if you specify the color as follows: Instead of Color.blue, use java.awt.Color.blue in the popup window, and similarly for the other colors.

What to Turn In

Hand in your work by filling out this turnin form and selecting the file Puck.java containing your Puck class.  When you submit your file, it will be compiled on the turnin server and you will see a receipt indicating whether the program compiled successfully.  If any errors were detected, please fix them and turn your program in again.  If no errors were found, you are done, however, we suggest that you print or save a copy of the electronic receipt for future reference.