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