001package algs13; 002import java.text.DecimalFormat; 003import stdlib.*; 004public class Playground { 005 private Node first; 006 static class Node { 007 public double item; 008 public Node next; 009 public Node (double item, Node next) { 010 this.item = item; 011 this.next = next; 012 } 013 } 014 015 public int numFives () { 016 return StdRandom.uniform (100); //TODO: fix this 017 } 018 019 public static void main1 (String[] args) { 020 Trace.showBuiltInObjects(true); 021 Trace.drawStepsOfMethod ("numFives"); 022 Trace.drawStepsOfMethod ("numFivesH"); 023 Trace.run (); 024 Playground list1 = Playground.of ("5 11 5 5"); 025 Playground list2 = Playground.of ("24 35 67"); 026 int result1 = list1.numFives (); 027 Trace.draw(); 028 StdOut.println ("result: " + result1); 029 } 030 031 public static void main2 (String[] args) { 032 Trace.showBuiltInObjects(true); 033 Trace.drawStepsOfMethod ("main"); 034 Trace.drawStepsOfMethod ("example"); 035 //Trace.drawSteps(); 036 Trace.run (); 037 Playground example1 = Playground.example (1); 038 Playground example2 = Playground.example (5); 039 Playground example3 = Playground.example (7); 040 Node x = example1.first; 041 StdOut.println (x.item); 042 } 043 044 /* A silly method to show list creation */ 045 public static Playground example(int i) { 046 Node x1 = new Node (i+10, null); 047 Node x2 = new Node (i+20, null); 048 Node x3 = new Node (i+30, null); 049 Node x4 = new Node (i+40, null); 050 Playground result = new Playground (); 051 result.first = x1; 052 x1.next = x2; 053 x2.next = x3; 054 x3.next = x4; 055 return result; 056 } 057 058 059 public static void main (String[] args) { 060 testNumFives (2, "11 5 5 21"); 061 testNumFives (0, "11 21 31 41"); 062 testNumFives (1, "11 21 5 31 41"); 063 testNumFives (1, "5 11 21 31 41"); 064 testNumFives (1, "11 21 31 41 5"); 065 testNumFives (5, "5 11 21 5 5 31 5 41 5"); 066 testNumFives (3, "5 5 5"); 067 testNumFives (1, "5"); 068 testNumFives (0, "11"); 069 testNumFives (0, ""); 070 StdOut.println ("Finished tests"); 071 } 072 073 private static void testNumFives (int expected, String sList) { 074 Playground list = Playground.of (sList); 075 String sStart = list.toString (); 076 int actual = list.numFives (); 077 if (expected != actual) { 078 StdOut.format ("Failed: Expecting [%d] Actual [%d] with argument %s\n", expected, actual, list); 079 } 080 String sEnd = list.toString (); 081 if (! sStart.equals (sEnd)) { 082 StdOut.format ("Failed %s.numFives(): List changed to %s\n", sStart, sEnd); 083 } 084 } 085 086 /* ToString method to print */ 087 public String toString () { 088 // Use DecimalFormat #.### rather than String.format 0.3f to leave off trailing zeroes 089 DecimalFormat format = new DecimalFormat ("#.###"); 090 StringBuilder result = new StringBuilder ("[ "); 091 for (Node x = first; x != null; x = x.next) { 092 result.append (format.format (x.item)); 093 result.append (" "); 094 } 095 result.append ("]"); 096 return result.toString (); 097 } 098 099 /* Method to create lists */ 100 public static Playground of(String s) { 101 Playground result = new Playground (); 102 if ("".equals (s)) return result; 103 104 Node first = null; 105 String[] nums = s.split (" "); 106 for (int i = nums.length-1; i >= 0; i--) { 107 try { 108 double num = Double.parseDouble (nums[i]); 109 first = new Node (num, first); 110 } catch (NumberFormatException e) { 111 throw new IllegalArgumentException (String.format ("Bad argument \"%s\": could not parse \"%s\" as a double", s, nums[i])); 112 } 113 } 114 result.first = first; 115 return result; 116 } 117 118}