Introduction to Modern Programming Techniques

Lab #2

Using Classes and Objects

This class — named `Point`

has already been written, compiled, tested, and documented. The documentation was produced using
a standard Java development tool known as javadoc. `javadoc`

takes one or more Java source files that have
been marked-up with appropriately formatted comments, and produces a set of HTML pages known as an API
(Application Programmers Interface)i. (The entire Java class library that comes with the Java Development Kit is documented in the same
fashion, and is collectively known as the Java API — you can access it by Googling *Java API*. The API produced for a class includes
all public fields (remember a field is a method, variable, or constant of a class) of the class, broken up into sections for constants, variables
(rarely present — remember, variables belonging to a class are typically declared `private`

), constructors, and methods. The
whole thing is hyperlinked to allow cross-referencing.

- Here is the API for the
`Point`

class representing a 2-dimensional point. - Here is the
`.class`

file for the`Point`

class. You should download it for use while developing the code in your IDE.

Code an application class (i.e., a class containing a `main`

method), named `PointApp`

that reads
point data (i.e., x and y coordinates) from the file `points.text`

. This data is then used to create pairs of `Point`

objects
which are then used to *flex* (i.e., illustrate) the methods of the class.

The format of the `points.text`

file is:

xi.e., pairs of x/y coordinates, resulting in data for 2 Point objects per line._{point1}y_{point1}x_{point2}y_{point2}…

Sample Test Run

For example if the file `points.text`

contains:

0 0 1 1 1 1 1 -1 1 1 -1 1 1 1 -1 -1 0 0 0 0 1 1 1 1 1 1 -2 -2the program should produce the following output:

p1: (0, 0) (quadrant 0) / p2: (1, 1) (quadrant 1) p1+p2: (1, 1) (quadrant 1) The distance between (0, 0) and (1, 1) is 1.4142135623730951 p1: (1, 1) (quadrant 1) / p2: (1, -1) (quadrant 4) p1+p2: (2, 0) (quadrant 4) p1 and p2 are reflections across the x-axis p1 and p2 are equidistant from the origin The distance between (1, 1) and (1, -1) is 2.0 p1: (1, 1) (quadrant 1) / p2: (-1, 1) (quadrant 2) p1+p2: (0, 2) (quadrant 0) p1 and p2 are reflections across the y-axis p1 and p2 are equidistant from the origin The distance between (1, 1) and (-1, 1) is 2.0 p1: (1, 1) (quadrant 1) / p2: (-1, -1) (quadrant 3) p1+p2: (0, 0) (quadrant 0) p1 and p2 are reflections through the origin p1 and p2 are equidistant from the origin The distance between (1, 1) and (-1, -1) is 2.8284271247461903 p1: (0, 0) (quadrant 0) / p2: (0, 0) (quadrant 0) p1+p2: (0, 0) (quadrant 0) p1 and p2 are reflections across the x-axis p1 and p2 are reflections across the y-axis p1 and p2 are reflections through the origin p1 and p2 are equidistant from the origin The distance between (0, 0) and (0, 0) is 0.0 p1: (1, 1) (quadrant 1) / p2: (1, 1) (quadrant 1) p1+p2: (2, 2) (quadrant 1) p1 and p2 are equidistant from the origin The distance between (1, 1) and (1, 1) is 0.0 p1: (1, 1) (quadrant 1) / p2: (-2, -2) (quadrant 3) p1+p2: (-1, -1) (quadrant 3) The distance between (1, 1) and (-2, -2) is 4.242640687119285

- Remember, static (class) methods have no receiver; when calling a static method from outside the class,
the class name appears in the position the receiver usually appears, e.g. Point.read.
- This is true of static (class) variables as well: Point.ORIGIN

- When submitting to CodeLab, just include your
`PointApp`

class, I will be supplying the`Point.class`

files. **For those of you working with an IDE that automatically places your class in a package**: The`Point`

class does not being to a package (actually it is said to belong the unnamed package. You may have to remove the package statement from your class to be able to use the`Point`

. (You can't add a`package`

statement to the`Point`

class because you do not have access to the source.)

- Practice creating objects and calling methods
- Working with a previously unseen class, using its API
- Working with someone else's
`.class`

file