Comparing traditional recursive programming with structure-shy programming code in: /home/lieber/.www/courses/csg110/sp08/exams/midterm/DemeterJBST Traditional Object-Oriented Programming (OO) BSTInt { {{ abstract int diameter2(); abstract int height2(); }} } NodeInt { {{ int height2() { return 1 + Math.max(left.height2(), right.height2()); } int diameter2() { int lheight = left.height2(); int rheight = right.height2(); int ldiameter = left.diameter2(); int rdiameter = right.diameter2(); return Math.max(lheight + rheight + 1, Math.max(ldiameter, rdiameter)); } }} } EmptyInt { {{ int diameter2() {return 0; } int height2() {return 0; } }} } Structure-Shy Object-Oriented Programming (AP-F) // DiameterPair = "height" int "diameter" int. class Diameter extends IDba{ // type unifying DiameterPair combine(BSTInt l) {return new DiameterPair(0,0);} DiameterPair combine(Object l, DiameterPair i)) {return i;} DiameterPair combine(NodeInt t, Integer d, DiameterPair l, DiameterPair r){ return new DiameterPair(Math.max(l.get_height(), r.get_height())+1, Math.max(l.get_height() + r.get_height() +1, Math.max(l.get_diameter(), r.get_diameter()))); } } Common: ==================================== OO / AP-F lheight + rheight + 1 versus (l.get_height() + r.get_height() +1 Math.max(ldiameter, rdiameter) versus Math.max(l.get_diameter(), r.get_diameter()) Differences: ==================================== OO is more modular, but AP-F solution can also be made modular. AP-F does not hard-wire fieldnames; left and right are not used in class Diameter. OO assumes a class graph of the form: BSTInt is abstract and has NodeInt and EmptyInt as subclass. NodeInt must have a left and a right field of type BSTInt. Example: BSTInt : X | Y. X : X1. X1 : NodeInt. Y : EmptyInt. NodeInt = .... BSTInt ... BSTInt ... . AP-F solves a more general problem AP-F assumes a class graph of the form: Traversal from BSTInt must reach entire tree. The leaf classes without any parts must inherit from BSTInt. NodeInt is of the form: NodeInt = Integer SubTree SubTree ... . There may be several classes with one SubTree. Example: BSTInt : EmptyInt | NodeInt. NodeInt = "(" int N1 N2 ")". N1 = BSTInt . N2 = BSTInt . EmptyInt = "empty". Problem solving by generalization and decomposition by AP-F Problem solving by decomposition in plain OO