CS 630 - Computing Fundamentals I - Summer 2004
Problem 1 - Writing classes


Loyola College > Department of Computer Science > CS 630 > Homework > Homework 5 > Problem 1

Objectives

Introduction

A C++ class definition contains the declarations of the data fields, which record the essential information about an object, the definitions of the constructors, which initialize the data fields, and the defintions of methods, which manipulate the data fields.

For a line class, the fields must record enough information to uniquely identify a line. There are different ways to do this: two points on a line are enough to distinguish it from other lines; a point and the slope will distinguish a line; and the slope and y-intercept will distinguish a line.

The class definition allows us to hide our choice of data fields from the users of our class. However, through the use of different versions of the constructor, users may be able to specify a line either by giving a slope and intercept or by giving two points on the line. The constructors would convert the information given as constructor arguments to the information stored in the data fields. For example, if we choose to implement our line class with slope and intercept fields, we can still allow users to specify lines by giving two points through calculations.

The methods must be written to reflect our choice of data fields. For example, writing a method that returns the slope of a line is as simple as returning the value in the appropriate data field if we choose slope and intercept as our fields. On the other hand, if we choose the coordinates of two points on the line as our data fields, then we would have to perform the "rise over run" calculation in order to compute the slope. Either way, though, the behavior is the same from the users' point of view. No matter how we implement our line class, the code

Line l = new Line(0, 0, 6, 3); // make a line through (0, 0) and (6, 3)
System.out.println(l.getSlope());
should produce the same results.

Assignment

Write a class called Line2D that can be used to represent non-vertical lines in the plane. Your class should contain

Your completed class should work with the following test driver. Download the driver and save it in the same directory as your Line2D class.

Submissions

Submit the source code for the Line2D class with your additions. You need not submit the lineTest.cpp.