COSC 122 Programming II
In this lab you will complete two Java classes that represent mathematical geometric point and line objects. A provided graphical framework will use your classes to enable a user to visually manipulate geometric objects.
- Teams
You are encouraged to work with a partner for this lab. You and your partner should begin thinking about the problems and begin writing the code before lab time.
You may work by yourself if you prefer.
- What to do
Develop two Java classes, GPoint (geometric point) and GLine (geometric line). Client code should be able to create instances of these classes. The functional details of these classes are given below.
- GPoint
GPoint objects represent mathematical geometric points. These objects are very simple and may be considered primitive types for our purposes. As such, a GPoint object contains two public fields: x and y. The class provides a constructor that accepts two doubles: the x and y coordinates of its fields. The GPoint class offers a toString method that returns a human-readable text string representing the point as a mathematical ordered pair; for example, (3.4,11).
GPoint summary
Required fields (all double-precision floating-point values and public): x, y; the (x,y) coordinates of the mathematical point.
Required constructors: One; initializes the point's coordinates.
Required methods: toString, that returns a human-readable text string representing the point as a mathematical ordered pair. No other methods are necessary, as the fields are public and clients can manipulate GPoint objects as they wish.
- GLine.
A GLine object represents a mathematical geometric line. As such, a GLine object is a little more sophisticated than a GPoint object.
A line object, like a point object, contains just two fields. Every mathematical line is uniquely defined by its slope and intercept. Your GLine objects, therefore, must contain a slope field and intercept field. These fields should be private. The slope of a vertical line is undefined. A vertical line has no y intercept; instead, it has an x intercept. For vertical lines, the slope should be set to the special constant value Double.MAX_VALUE (to represent infinity), and the intercept should represent the line's x intercept.
Your GLine class must provide three overloaded constructors:
- One constructor must accept two
double-precision floating-point arguments.
The first represents the slope of the new line,
and the second represents the intercept.
This is the easiest constructor to write because
no computation is necessary to determine the
object's fields.
- One constructor must accept four double-precision
floating-point arguments,
x1, y1,
x2, and
y2, in that order.
These represent the line determined by the points
(x1,y1) and
(x2,y2); that is,
they represent two points that the line must pass through.
- The third constructor must accept two GPoint references. These represent two points that the line must pass through. This constructor may delegate its work to the second version of the constructor that accepts the four double-precision floating-point arguments.
The constructor is responsible for initializing the slope and intercept of a new GPoint object. The code in two of the constructors must compute the slope of the line and its intercept in order to initialize these fields. Since these fields are private, clients cannot change a line object's state after its creation.
The slope of the line passing through two points (x1, y1) and (x2, y2), is the difference of the y values divided by the difference of the x values: (y2 - y1) / (x2 - x1).
If the two lines have equal x coordinates, the line is vertical and its slope should be set to Double.MAX_VALUE.
If the two points are identical, an infinite number of lines may pass through that point. Here it simply is not possible to make a line object. In this case set both the slope and intercept to Double.MAX_VALUE. This represents an illegal line.
Once you have a non-vertical line's slope, you may compute the y-intercept given the coordinates of one of the points passed to the constructor. Once you calculate the slope, choose either one of the original two points, such as (x1,y1), and use the formula b = y1 – mx1, where m is the slope and b is the computed y-intercept. Finding the intercept for a vertical line is easier: simply use the x coordinate of one of the two points (both of the x coordinates should be the same).
The GLine class must provide four methods:
-
getSlope
accepts no arguments and returns the line's
slope. The return type is a
double-precision floating-point value.
This method involves no mathematics because all the
constructor(s) precompute the slope, and
clients cannot change a line object's slope after it has
been created.
-
getIntercept
accepts no arguments and returns the line's
intercept. The return type is a
double-precision floating-point value.
This method involves no mathematics because all the
constructor(s) precompute the intercept, and
clients cannot change a line object's intercept after it has
been created.
-
intersection
accepts a single GLine reference as an argument.
The method must
compute the point of intersection of the given line with
the line object reference passed in as a parameter. The method
then creates a new GPoint object with these
coordinates and returns a reference to this new point
object.
Parallel lines
have no single intersection point, so if two lines have
the same slope the intersection method should
return the special point
(Double.MAX_VALUE,Double.MAX_VALUE).
The client code then can expect this possibility
and check for a valid
intersection point.
How do you compute the point of intersection of two lines?
- If the two lines have the same slope, there is no single point of intersection. The method assigns the bogus point (Double.MAX_VALUE, Double.MAX_VALUE) in that case.
- If neither line is horizontal or vertical,
we have a pair of equations that look like
y = m1x + b1 since both righthand expressions equal y, set them equal to each other:
y = m2x + b2m1x + b1 = m2x + b2 and solve for x:m1x - m2x + b1
=
m2x - m2x + b2
m1x - m2x + b1
=
b2
m1x - m2x + b1 - b1
=
b2 - b1
m1x - m2x
=
b2 - b1
(m1 - m2)x
=
b2 - b1
x
=
(b2 - b1)/ (m1 - m2)
This final equation
x = (b2 - b1)/ (m1 - m2) serves as a formula to compute x. (Note: You do not need to implement all the above arithmetic in your Java code; you need use only the result, the single formula computing x.) Once you have x you can plug it into either of the original equations to compute y. The x and y values are then used to create the required point.
- If one of the lines is horizontal or vertical, you know immediately one of the coordinates (x for vertical lines and y for horizontal lines). Plug the known value into the equation for the other line to compute the other coordinate.
- If one of the lines is horizontal and the other is vertical, you know immediately both of the coordinates (the x coordinate comes from the vertical line and the y coordinate comes from the horizontal line).
-
toString returns a String that
represents the equation of the line in slope-intercept
form; that is,
y = mx + b.
For example, a line with a slope of 3 and a
y-intercept of 5 has the equation
y = 3x + 5. Note that
a vertical line has an equation of the form
x = b, where b is the
line's x intercept.
Calling the toString method on an illegal string results in the string ILLEGAL STRING.
GLine summary
Required fields (all double-precision floating-point values and private): slope, intercept
Required constructors: Three, clients can create GLine objects in three different ways
Required methods: getSlope, getIntercept, intersecton, and toString, all described above.
- One constructor must accept two
double-precision floating-point arguments.
The first represents the slope of the new line,
and the second represents the intercept.
This is the easiest constructor to write because
no computation is necessary to determine the
object's fields.
- GPoint
- Source Files
Download from http://computing.southern.edu/halterman/Courses/Summer2012/cosc122/Labs/GeometricObjects/ the following files and add them to a new Eclipse project:
- GPoint.java
- GLine.java
- GeometryTest.java
- GeometryWindow.java
- oopj.jar
GPoint.java and GLine.java contain skeleton code that you must augment. The remaining three files are complete and may be used as is.
GeometryTest.java is a text-based program that allows you to test out your code. A sample run of GeometryTest.java looks like:
Enter the point coordinates x1, y1, x2, y2: 2 2 5 3 The slope of the line between (2.0,2.0) and (5.0,3.0) is 0.33 (0.33) [0.33] The y-intercept of the line between (2.0,2.0) and (5.0,3.0) is 1.33 The equation of the line between (2.0,2.0) and (5.0,3.0) is y = 0.33x + 1.33 Enter the point coordinates x3, y3, x4, y4: 8 2 10 -3 The intersection of y = 0.33x + 1.33 and y = -2.50x + 22.00 is (7.3,3.8) ----------------- Enter the point coordinates x1, y1, x2, y2: 3.4 -10.3 0 1 The slope of the line between (3.4,-10.3) and (0.0,1.0) is -3.32 (-3.32) [-3.32] The y-intercept of the line between (3.4,-10.3) and (0.0,1.0) is 1.00 The equation of the line between (3.4,-10.3) and (0.0,1.0) is y = -3.32x + 1.00 Enter the point coordinates x3, y3, x4, y4: 3.4 -10.3 0 1 The intersection of y = -3.32x + 1.00 and y = -3.32x + 1.00 does not exist ----------------- Enter the point coordinates x1, y1, x2, y2: 5 2 5 5 The slope of the line between (5.0,2.0) and (5.0,5.0) is undefined (undefined) [undefined] The x-intercept of the line between (5.0,2.0) and (5.0,5.0) is 5.00 The equation of the line between (5.0,2.0) and (5.0,5.0) is x = 5.00 Enter the point coordinates x3, y3, x4, y4: 2 5 3 5 The intersection of x = 5.00 and y = 0.00x + 5.00 is (5.0,5.0) ----------------- Enter the point coordinates x1, y1, x2, y2: 2 5 3 5 The slope of the line between (2.0,5.0) and (3.0,5.0) is 0.00 (0.00) [0.00] The y-intercept of the line between (2.0,5.0) and (3.0,5.0) is 5.00 The equation of the line between (2.0,5.0) and (3.0,5.0) is y = 0.00x + 5.00 Enter the point coordinates x3, y3, x4, y4: 5 2 5 5 The intersection of y = 0.00x + 5.00 and x = 5.00 is (5.0,5.0) ----------------- Enter the point coordinates x1, y1, x2, y2:
GeometryWindow.java is a graphical program that allows users to directly manipulate the geometric objects. The following shows a run in progress:
The files GPoint.java, GLine.java, GeometryTest.java, and GeometryWindow.java are Java source files. You may add those to your Eclipse project as you would any other Java source files.
oopj.jar is a special Java archive file. It contains a number of precompiled Java source files packaged in a special way so that the Java tools can readily access its contents. The file GeometryWindow.java makes use of classes that it does not define and that are not part of the standard Java library of classes. For example, the class CartesianViewport is defined and implemented in the oopj.jar file.
After downloading the oopj.jar file you should copy it to a more permanent location because you will use it in future assignments. Even though multiple projects will reference this jar file, you need only keep one copy of it on disk.
In order to use the jar file in your project, right-click on your project within Eclipse's Package Explorer pane:
Next, select Build Path, and in the resulting popup submenu, select Add External Archives...:
A dialog box will appear that allows you to navigate to the location where you copied the oopj.jar file:
For future projects that require the functionality of the oopj.jar classes, in a similar manner you need only adjust the project's build path to refer to this same oopj.jar file.
- Check out
Your finished program will be evaluated for correctness and compliance.