Computer Science 60
Principles of Computer Science
Spring 2004


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

This is a short assignment intended to 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 Ran or Zach with any questions. Finally, 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
    // Date:  January 15, 2004

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

    class Point
    {
      public double x;
      public double y;

      // 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) );
      }

      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.

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, although it is not necessary, you may use any additional private methods that you like in order to build your public methods. 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 to test the methods in your Line class. You should always write your own test programs as well 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.

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.

Last modified January 2004 by Ran Libeskind-Hadas