Assignment 03

Task

Construct and use objects of the pre-defined Point class to solve a problem.

Textbook: 2.6-2.7; 2.10
New concepts: Calling constructors and instance methods; object references.

Steps

For this program, you will use the java.awt.Point class. For more about the constructors and methods of this class, check the Java API.

To avoid typing out the long form every time you refer to Point, you can add the following line to the very top of your Java file (even before the /** javadoc */ description of your class):

  import java.awt.Point;

In your main method, create two Point objects. The first point should be at (7, 0) and the second point at (28, 28). Print out these locations. Then print the distance between the two points and the slope of the line formed by the two points. The slope and distance should be printed as doubles, with each value on its own line.

The necessary formulas:

When computing these results, use variables and method calls only. Once again, the literal values 7, 0, and 28 should occur only once in your code--when you construct your two Point objects. (Actually, the value 28 will occur twice, since it is both the x and y value for the second Point.) Don't forget about the Math methods, operator precedence, etc.

At this point, your output should look something like this:

For two points at (7.0, 0.0) and (28.0, 28.0):
Slope: 1.3333333333333333
Distance: 35.0

Now move the first point by (+3, -12). [This will place it at (10, -12). There is a handy method that lets you specify a relative adjustment to the position of an existing Point object. However, you can instead move the Point to the new location or even build another Point object.]

Repeat your output for the new location. Thus, your final output will look something like this (except that I have removed the final slope and distance results):

For two points at (7.0, 0.0) and (28.0, 28.0):
Slope: 1.3333333333333333
Distance: 35.0

After moving the first point...
For two points at (10.0, -12.0) and (28.0, 28.0):
Slope: [...]
Distance: [...]

What to Submit

Upload your UsernameA03.java file to Tamarin.

Grading [4 points]

1 - Compiles
Your program compiles successfully (no errors)
1.5 - Point use
You use the java.awt.Point class (1.0). You use literal values only when constructing or moving Point objects, but not when performing calculations or printing results (0.5).
1.5 - Results
You print the location of the points for both output sets (0.5). Your slope and distances are calculated correctly by your program using to the Point locations specified above--first for (7,0) and (28,28), and then for (10,-12) and (28,28) (1.0).

FAQs

Demo code?
Here is an example from Section 001 lab of using java.awt.Rectangles: RectangleIntersection.java.

In lab, we ran into a little trouble writing this code. Here is an explanation of what went wrong.