A9 ‣ Iterable and Comparable

Create a directory in your repository named a9 and include all of the files for this assignment in the a9 directory.

  1. Write an class named Point.
    1. The class should have two private fields that hold integers.
    2. The class should have a constructor that sets both fields with the values passed into it.
    3. The class should override toString so that it returns a String with the value “(x,y)” where x and y are the values of the fields.
    4. The class should override equals and hashCode using both fields as the significant fields.
      1. Note that order matters: (0,1) is not the same as (1,0).
  2. Write a class named IllFormedPolygonException.
  3. Write a class named Polygon.
    1. The class should contain a field that contains an array of Point objects.  To mentally view a Polygon object as a 2D shape we can imagine there are line segments drawn between adjacent Points in the array and a line segment drawn between the first and last Point in the array.
    2. The class should have a constructor that requires at least three Point objects.
      1. The class should add to the array the points that are passed into the constructor in the order in which they are passed to the constructor.
    3. The class should override toString so that it prints each of the points in the array in the order in which they were added.
    4. The class should override equals and hashCode and consider two Polygons equal if when viewed as 2D shapes (see 3.1) they contain the same line segments.
    5.  The class should implement the Iterable interface and provide an Iterator that iterates over the array of Points.
    6. The class should implement the Comparable interface.  We’ll define comparably larger and comparably equivalent as follows: for any two Polygon objects A and B, A is comparably larger than B if A has more line segments than B.  They are comparably equivalent if they contain the same number of line segments.
  4. Write a driver that thoroughly tests your classes.

For this assignment, I will test your code using a driver that contains 20 test cases.  Each test case will be worth 5 points.  My test cases will rigorously test your code, so be sure to start early, work diligently, and test your code rigorously.

© 2017 – 2019, Eric. All rights reserved.