/* File Zero.java Part of Natural Number Example H. Conrad Cunningham, Professor 1234567890123456789012345678901234567890123456789012345678901234567890 2004-01-17: Original 2016-10-26: Formatting changes, compilation check Class Zero represents the natural number value zero. It is a leaf subclass in the Composite pattern and is designed and implemented with the Singleton pattern to ensure that exactly one instance is created. */ public class Zero extends Nat { private Zero() {} // private constructor for Singleton public Nat add(Nat n) { return n; } // 0 + n = n public Nat sub(Nat n) { if (n == this) { return this; } // 0 - 0 = 0 return Err.getErr(); // undefined } public boolean isLess(Nat n) { return (n != this); } // 0 < n public boolean equals(Object n) { return (n == this); } // exactly one value of Zero public String toString() // method from class Object { return "0"; } // Set up Singleton pattern for this type public static Zero getZero() { return theZero; } private static Zero theZero = new Zero(); }