CS 101 (Spring 1999)
Lab 2: Craf-ta-Graf Implementation

Lab Assigned Design Due
(Mondays 2 PM)
Lab Due
(Fridays 2 PM)
22 Jan 25 Jan 29 Jan
By now, you should be familiar with a design for this lab. If the remarks on your graded lab design are encouraging, you should proceed by implementing that design. On the other hand, if your design appears to be flawed, or if you are unsure how to proceed, then you can take the files as they are given to you and implement them as described in this document.

A sample working version of this lab has been implemented by the instructor . You can run this to get an idea of how the project should work. If you are unsure about how something should work, please get help .

[[[ Download PC zip ]]]
This is the official launch point for your lab, but this file serves only to invoke Startup, either as an Applet or as a standalone application. It may seem mysterious, but the invocation of Startup.start() causes Startup's run method to execute. For now, trust me--- CS 102 covers threads. You should not modify this file.
void run()
This class's run method is where things really get started for this lab. The method invokes three test methods that should exercise the CS101Canvas, the MousePad, and the SegmentDisplay. The method then constructs a MousePad and SegmentDisplay for the Craf-ta-Graf.

Some code follows that "doodles" on the display. Thereafter, a loop repeatedly gets a gesture from the MousePad and passes it on to the SegmentDisplay.

You need not modify this method.

void testCanvas()
This method draws some objects and reads the mouse location in a test window. Study this method and see how it works. You should especially pay attention to the use of click() and Line, as you will find these useful for the MousePad and SegmentDisplay.
As supplied, this code does nothing. You should insert code to test your DirectionVector class and its methods. Your grade will be partly based on how thoroughly you test your work.
void testMousePad()
This method instantiates a MousePad and reads three gestures from it, reporting the results using Terminal.println(String). You need not modify this method, but it won't report anything useful until your MousePad is functional.
void testSegmentDisplay()
This method instantiates and draws on a SegmentDisplay. Nothing nice will appear until your SegmentDisplay is functional.
The version you are given contains stubs: methods that have the correct type signature, and return something useful, but do not operate fully as they should. You are to modify this file as follows and turn it in for this lab.

  1. In Symantec Cafe, open the Lab2.prj project file you downloaded.
  2. Notice that a class named DirectionVector has already been defined in the file DirectionVector.java. Notice that the class name exactly matches the file name, including capitalization. Java expects this, so when you define classes from scratch, be sure to follow this convention.
  3. Fill in your name and other information in the header comment.
  4. Complete the code for the constructor, declaring any necessary field for the class. Pay attention to their spelling and to whether they should be private or public. Names of instance variables and methods should always begin with lower case letters. (If an instance variable has multi-word name, capitalize subsequent words. For example, thisIsAVeryLongVariableName.)

    Remember: when the name of a parameter of a method matches the name of an instance variable in the same class, it "masks" the instance variable. However, you can still refer to the instance variable by preceding it with "this." For example, this.x = x will assign the value of the parameter x to the instance variable named x. Within a method, this always refers to the object on which the method has been invoked (i.e., "this" object).

  5. Fill in the code for the two accessors named getX and getY that take no parameters and return the values of the appropriate instance variables. Note: Here, the instance variables are not masked by matching parameter names (in fact, there are no parameters at all), so there is no need to use "this." You can refer to the instance variables directly by their names.
  6. Define methods that correctly implement each of the following specifications. Remember that instance variables of a class are available within its methods, so many methods need no parameters.
    1. magnitude takes no parameters and returns the length of the direction vector (i.e., the distance from (x, y) to the origin).
    2. add takes another direction vector as its parameter and returns a new DirectionVector that is the sum of this vector and the one provided as input. Recall that to add two vectors, you add their x-coordinates and their y-coordinates. For example, suppose you have a direction vector with value (3, 4) and you call the add method on it, passing in a direction vector with value (-5, 2). Then the vector returned by the method should have the value (-2, 6). (Hints: The parameter type and return type of this method are both DirectionVector. When you create the new direction vector inside your method, you'll provide the sums of the coordinates as parameter values to the constructor.) If you need help on this, don't hesitate to ask.
    3. toString takes no parameters and returns a String value that is a textual description of the contents of the vector. For example,
      (new DirectionVector(4,3)).toString()
      could have the string value "(4,3)" as its return value. As a rule, you should provide a toString method for every class you write, since Java uses that method when it concatenates the value of a variable to a string. For example, the existence of the toString method allows us to do the following:
        DirectionVector v = new DirectionVector(5,2);
        Terminal.println("Created the vector " + v + ".");
      with the resulting output:
        Created the vector (5,2).
  7. Test your methods from Startup.java as described above.
You need to implement the following methods.
DirectionVector getGesture()
This method is responsible for returning the user's mouse gesture as a DirectionVector. Process a gesture as follows:
  1. Read one mouse click from the mouse pad.
  2. Read a second mouse click from the mouse pad.
  3. Construct a DirectionVector that captures the vector from the first to the second click.
String toString()
Fill in this method to be more informative.
You must implement the following methods.
void addSeg(DirectionVector)
This method adds a segment to the display using the supplied DirectionVector. Remember that the new segment must start from the end of the previous segment. You will probably have to add some instance variables to SegmentDisplay, along with code to maintain them, so that you can "remember" where you left off.

A segment can be drawn by the appropriate call to Line from CS101Canvas.

Note that the segments you draw should never stray outside the display. Like its toy analog (Etch-a-Sketch), attempts to go outside the display are stopped at the edges.

DirectionVector minCorner() and DirectionVector maxCorner()
These methods should return a vector from (0,0) to the upper-left and lower-right corners, respectively, of the area that has been drawn so far on the display. In other words, these corners form a rectangle that completely contains the drawing so far.
This method returns the fraction of the display that has been utilized so far. Compute this as the ratio of the diagonals of the area occupied and the area of the display.

To the greatest extent possible, do not duplicate code in your class. Instead, make use of the methods you have defined.

String toString()
Oh, you know what to do here by now.

What To Turn In:

The following items should be stapled together (in order) and turned into the correct CS101 bin (by your section letter) by 2 P.M. on the due date. (Beginning with Lab 2, all work you turn in should be prepared according to the CS101 Style Guide.) Double check that you have completed all header information.

  1. The proper cover sheet printed on one page, or obtained from the top of the bins.
  2. A paper printout of the following java files
  3. A printout of your transcript file containing your test results.

Important note about testing: Think carefully about your test cases, to be sure they cover the various possible cases. Your test procedures should print out enough information for someone to understand what is being tested. For example, be sure to print the method name, parameters and return values in the test program as you call each method of your objects. Your output must be clear and well organized to receive full credit on this lab. Remember to turn in your transcript! 

Important note about demos: You will have to demonstrate your lab (i.e., let a TA execute your program) in your next lab section immediately following the due date. If you are not in your lab section to give a demonstration, we will assume that your program doesn't work.

Last modified 13:13:47 CST 22 January 1999 by Ron K. Cytron