Computer Science 60
Principles of Computer Science
Fall 2003


Assignment 1
Due Monday, September 8 by 11:59 PM

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 may also send e-mail to cs60help@cs.hmc.edu for help.

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:  September 3, 2003

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

      // equal method returns true if point p represents the same
      point as this.  Note that this was called 'equals' in class.  My bad.
     
      boolean equal (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 use this Point class to implement a Line class. Your class should be stored in a file called Line.java. You may use any internal representation that you wish for representing a line, but 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 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). 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 might wish to write some of your own test programs as well. This is a good thing to do to give you confidence that you have tested all of the cases that might arise in practice. 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 September 2003 by Ran Libeskind-Hadas