Checking Class Invariants

You should always try and answer the question "can I feed this method something that will cause the class invariants to be violated?" If I pass in a negative number of sides for shape, will it accept it? If I give my cash register a negative amount of money, what will happen?


public static void main(String[] args) {
  //Test shape
  Shape s = new Shape(-1);
}

public Shape(int numSides) {
   if(numSides < 1) {
      numSides = 1;
   }
...
}

Think about your Fraction, Point and Rectangle classes. What are the invariants for each class? Can a Fraction have a denominator with a value of 0? What are the requirements for the Points in the Rectangle class?
  1. Write a description about the class and its invariants in the javadoc before the class.
  2. Modify the code to enforce the invariants.
Below is code for a helper method for the Rectangle class to determine a whether a right angle exists between three points. There are many ways to determine if a right angle exists in your Rectangle. You can use two vectors as we sugested in Lab 4 or you can use a three points as seen below. The code below uses the Pythagorean theorem.

private static boolean isRightAngle(Point one, Point corner, Point two)
{
	int sideOne = Point.distance(one, corner);
	int sideTwo = Point.distance(two, corner);
	int hypotenuse = Point.distance(one, two);
	return Math.pow(sideOne, 2) + Math.pow(sideTwo, 2) == Math.pow(hypotenuse, 2);
}