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?
- Write a description about the class and its invariants in the javadoc before the class.
- Modify the code to enforce the invariants.
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); }