/* File Nat.java Part of Natural Number Example H. Conrad Cunningham, Professor 1234567890123456789012345678901234567890123456789012345678901234567890 2004-01-17: Original 2016-10-26: Formatting changes, compilation check The cluster of classes consisting of base class Nat and its subclasses Zero, Err, and Succ defines a representation for the "natural numbers" with a hierarchical class structure inspired by Peano's Postulates. It does not use any builtin integer type. We consider 0 as being a natural number as is customary for many computer scientists. Mathematically, we can define the set Nat (of natural numbers) as follows: x in Nat if and only if (1) x = 0 -- zero or (2) (Exists y in Nat :: x = Succ(y)) -- successor function This design uses the Composite pattern to represent natural numbers. The abstract base class for the set is Nat. Subclass Zero is the leaf case of the Composite pattern; it represents 0. Subclass Succ is the composite case of the Composite pattern; it represents a successor of the single Nat it contains. Subclass Err is another leaf case of the Composit pattern; it represents errors. The design and implementations of Zero and Err also reflect the Singleton design pattern, which provides for exactly one instance to be created. Err also reflects the Null Object design pattern, in that its instances are null or inert objects that can be safely returned by operations in error situations. This implementation essentially gives a a unary representation for the natural numbers -- one Succ object in a linear structure for each natural number value greater than 0. */ /* Class Nat is the base class of our Natural numbers. It specifies the operations that Nats must support. Additional operations should be included. */ abstract public class Nat { abstract public Nat add(Nat n); abstract public Nat sub(Nat n); abstract public boolean isLess(Nat n); }