Computer Science 60
Principles of Computer Science
Spring 2005


Assignment 1: Fun with Points and Lines! [60 points]
Due Monday, January 24 by 11:59 PM
Recommended Reading: Keller, Chapter 7, pages 227-238.

Now that you have completed Assignment 0, this short assignment will further build up your "object-oriented" muscles. Please see the tutoring schedule for the schedule of tutoring hours. You are also encouraged to come and talk to Zach or Ran with any questions. Also, you may send e-mail to cs60help@cs.hmc.edu for help with short questions.

In lecture we saw a Point class like the one below which defines a point in 2-dimensional space.

    // Point class
    // Written by Ran Libeskind-Hadas 
    // Modified without permission by Z Dodds
    // Date:  January 5, 2005  

    // This class represents a point in two dimensions.  The coordinates
    // are represented as double precision numbers.

    class Point
    {
      public double x;     // public allows for simple manipulation
      public double y;     // the internal representation of the Line
                           // class should be private

      // Constructor takes x- and y-coordinates
      Point (double x, double y)  
      {
        this.x = x;
        this.y = y;
      }

      // Constructor takes no arguments and sets x- and y-coordinates to 0.0.
      Point ()  
      {
        this(0.0, 0.0);
      }

      // equals method returns true if point p represents the same
      // point as this.  
      public boolean equals (Point p)
      {
        return ( (x == p.x) && (y == p.y) );
      }

      // toString returns the String representation of a point.  
      public String toString()
      {
        String s = new String("(" + x + ", " + y + ")" );
        return s;
      }
    }
  

You will find this class in the file Point.java in the directory /cs/cs60/assignments/assignment1 and you should copy it into your own directory and use it. Please use this Point class without modification.

In this assignment you will be implementing a Line class. You'll be able to then create new objects of type Line and do neat things like test if two lines are parallel, find the intersection of two lines (assuming they aren't parallel), and other things that one might wish to do with lines.

Notice that there are many ways to represent and implement a line. For example, a line might be represented as two points, or by its slope-intercept representation, or by a point and an angle, or even in some other way. The choice of implementation is a data structure "detail". From the user's perspective, the important thing is that a line is an Abstract Data Type (ADT) with certain methods that are guaranteed to be supported. The abstract data type can be expressed as a Java interface which we will call LineADT.java. You can find this file in the directory /cs/cs60/assignments/assignment1. Here is what it looks like:

interface LineADT
{
    public boolean on (Point p);
    public boolean equals (Line l);
    public boolean parallel (Line l);
    public boolean vertical ();
    public Point intersection (Line l);
}

Your class should be called Line and should therefore be in a file called Line.java. The first line of code (after the initial comments that every file should contain) in your implementation should then be:

class Line implements LineADT
This says that class Line promises to implement all of the public methods declared in LineADT. In addition though, your Line class will have some constructors (more on this below) and might have any number of private methods which are used within the Line class to help out the public methods. These are up to you.

You may use any internal representation that you wish for representing a line. One nice and simple solution is to represent a line by two points (using the Point class! Other possible internal representations are using the slope and y-intercept, one point and an angle, etc.). In any case, your Line class should have the public methods listed in the LineADT interface and explained in detail below. Be sure to use exactly these names and the specified kinds of arguments and return values. If implemented carefully, none of these methods requires more than 6 or 7 lines of code (and some even less). As indicated above you may use any additional private methods that you like in order to build your public methods (this is not required, but it is permitted). Any variables that are part of the internal representation of the Line should not be accessible to the outside world. For example, it is not the user's "business" whether you represented the line internally using two Points or using some other representation. Therefore, these representational variables should be private as well.


You'll find a test program called LineTester.java in the directory /cs/cs60/assignments/assignment1. You can copy this into your own directory and run it to test the methods in your Line class. If your program is correct, the LineTester program will produce the output shown in the file TestOutput which is also in the /cs/cs60/assignments/assignment1 directory.

We will often provide you with some simple testing programs like LineTester.java. However, you should always write your own test programs to test every method that you have written. Learning to test your own code is an important part of learning to program, In general, the graders will use more test cases than what we provide for you.

Note on precision     In general, you should be wary of using == to compare two values of type double, because round-off error within computations and the computer's internal representation of values can cause mathematically valid equalities to become unequal. However, since the purpose of this assignment is to provide practice with java, and not write a computational geometry engine, you should feel free to compare double values for equality with ==.

Submitting

Use cs60submit to submit your file Line.java file. You do not need to submit the Point.java nor LineTester.java files since we have these.

Entirely optional extra credit bonus problem     [worth up to 5 points]

It turns out that implementing a LineSegment class makes the intersection method a bit trickier, because your code needs to determine whether or not a point of intersection actually falls on both of the LineSegments.

For extra credit, implement a LineSegment class in the file LineSegment.java, with all of the above methods except the slope/intercept constructor. In addition, you should change your on and intersection methods: on should only return true when the input point is within the endpoints of the LineSegment (including either endpoint). intersection should return null in the case that the segments do not intersect for any reason. If the segments overlap in more than one point, any point on that overlap may be returned. The equals method should return true if and only if this and the input are identical line segments.

An additional note: In order that LineSegments and their methods interact naturally, your LineSegment class should not implement LineADT. Instead, the methods should use LineSegments wherever the Line class used Lines.

Last modified January 2005 by Zach Dodds or Ran Libeskind-Hadas