package lrs.visitor; import lrs.*; /** * Finds, removes, and returns the minimum Integer element in a LRStruct. * NOTE: There is a bug in this code. Find and fix it. * @author D. X. Nguyen */ public class RemMinBuggy implements IAlgo { public static final RemMinBuggy Singleton = new RemMinBuggy (); private RemMinBuggy () { } public Object nullCase (LRStruct host, Object inputNotUsed) { throw new java.util.NoSuchElementException ("Empty host has no min element."); } public Object nonNullCase(LRStruct host, Object inputNotUsed) { // passes itself to the tail and asks the tail to "help" find, remove, and return the min. return host.getRest().execute ( new IAlgo () { public Object nullCase (LRStruct hostTail, Object tempMinLRS) { return ((LRStruct)tempMinLRS).removeFront(); } public Object nonNullCase (LRStruct hostTail, Object tempMinLRS) { Integer tempMin = (Integer)((LRStruct)tempMinLRS).getFirst (); Integer dat = (Integer)hostTail.getFirst (); return tempMin.intValue () <= dat.intValue ()? hostTail.getRest ().execute (this, tempMin): hostTail.getRest ().execute (this, hostTail); } } , host); } public static void main (String[] args) { LRStruct L = new LRStruct (); System.out.println ("\nL = (" + L + ')'); try { System.out.println ("RemMinBuggy L = " + L.execute (RemMinBuggy.Singleton, null)); } catch (Exception e) { System.out.println ("Get Last L: " + e); } L.insertFront (new Integer (4)); System.out.println ("\nL = ( " + L + ')'); System.out.println ("RemMinBuggy L = " + L.execute (RemMinBuggy.Singleton, null)); System.out.println ("L = ( " + L + ')'); L.insertFront (new Integer (4)); L.insertFront (new Integer (99)); System.out.println ("\nL = ( " + L + ')'); System.out.println ("RemMinBuggy L = " + L.execute (RemMinBuggy.Singleton, null)); System.out.println ("L = ( " + L + ')'); L.insertFront (new Integer (4)); L.insertFront (new Integer (-7)); L.insertFront (new Integer (99)); System.out.println ("\nL = ( " + L + ')'); System.out.println ("RemMinBuggy L = " + L.execute (RemMinBuggy.Singleton, null)); System.out.println ("L = ( " + L + ')'); } }