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.
- The class should have a constructor that takes two Points
as arguments and sets the internal representation of the line to pass
through these two points. (Notice that this does not stipulate
that you must use two points as the internal representation of the line.
You could still use the slope/intercept representation,
for example, and just
compute the slope and the intercept from these two given points.)
- Sometimes it will be inconvenient for the user to establish two
objects of type Point in order to call the above constructor.
Therefore, the class should have a second constructor that takes
four double arguments: The x- and y-coordinates
of one point on the line followed by the x- and y-coordinates of a second
point on the line.
- Some users really like the slope/intercept representation of lines.
Therefore, your class should have a third constructor that takes
two double arguments:
The slope of the line followed by the y-intercept.
- The class should have a method called equal that takes
a Line as an argument and determines whether the two lines represent
the same line. The function returns a boolean which is true
if the lines are the same and false otherwise. Notice that
if one line is represented internally by the points (0.0,0.0) and (1.1,1.1) and
a second line is represented internally by the points (30.1, 30.1) and
(42.0, 42.0) then these lines are in fact equal in spite of the fact
that their internal representations are different.
- The class should have a method called parallel that takes
a Line as an argument and determines whether the two lines
are parallel. The function returns a boolean which is true
if the lines are parallel and false otherwise.
- The class should have a method called on that takes
a Point as an argument and returns a boolean which
is true if the point is on the line and false otherwise.
- The class should have a method called vertical that
takes no arguments and returns a boolean which is true
if the line is vertical (infinite slope) and false otherwise.
- The class should have a method called intersection that
takes a Line as an argument and returns a Point which
is the intersection of the two lines. Ideally, the function would return
some special value if the two lines are parallel or equal, but you need
not worry about these special cases for this assignment.
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