CSC300 / CSC402: Starter code [1/6] Previous pageContentsNext page

file:Playground.java [source]
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package algs13;
import java.text.DecimalFormat;
import stdlib.*;
public class Playground {
  private Node first;
  static class Node { 
    public double item; 
    public Node next; 
    public Node (double item, Node next) { 
      this.item = item; 
      this.next = next; 
    }
  }

  /* A silly main program to show list creation */
  public static void main (String[] args) {
    Trace.showObjectIdsRedundantly(true);
    Trace.drawStepsOfMethod ("main");
    //Trace.drawStepsOfMethod ("makeExample");
    //Trace.drawSteps();
    Trace.run ();
    Playground example1 = Playground.makeExample (1);
    Playground example2 = Playground.makeExample (5);
    Playground example3 = Playground.makeExample (7);
    Node x = example1.first;
    StdOut.println (x.item);
  }
  public static Playground makeExample(int i) {
    Node x1 = new Node (i+10, null);
    Node x2 = new Node (i+20, null);
    Node x3 = new Node (i+30, null);
    Node x4 = new Node (i+40, null);
    Playground result = new Playground ();
    result.first = x1;
    x1.next = x2;
    x2.next = x3;
    x3.next = x4;
    return result;
  }
  
  public static void main2 (String[] args) {
    //Trace.showObjectIdsRedundantly(true);
    Trace.drawStepsOfMethod ("numFives");
    Trace.drawStepsOfMethod ("numFivesH");
    Trace.run ();
    Playground list1 = Playground.from ("5 11 5 5");
    Playground list2 = Playground.from ("24 35 67");
    int result1 = list1.numFives ();
    Trace.draw();
    StdOut.println ("result: " + result1);
  }
  public int numFives () {
    return StdRandom.uniform (100); //TODO: fix this
  }
  public static void main3 (String[] args) {
    testNumFives (2, "11 5 5 21");
    testNumFives (0, "11 21 31 41");
    testNumFives (1, "11 21 5 31 41");
    testNumFives (1, "5 11 21 31 41");
    testNumFives (1, "11 21 31 41 5");
    testNumFives (5, "5 11 21 5 5 31 5 41 5");
    testNumFives (3, "5 5 5");
    testNumFives (1, "5");
    testNumFives (0, "11");
    testNumFives (0, "");
    StdOut.println ("Finished tests");
  }

  private static void testNumFives (int expected, String sList) {
    Playground list = Playground.from (sList); 
    String sStart = list.toString ();
    int actual = list.numFives ();
    if (expected != actual) {
      StdOut.format ("Failed: Expecting [%d] Actual [%d] with argument %s\n", expected, actual, list);
    }
    String sEnd = list.toString ();
    if (! sStart.equals (sEnd)) {
      StdOut.format ("Failed %s.numFives(): List changed to %s\n", sStart, sEnd);
    }
  }

  /* ToString method to print */
  public String toString () { 
    // Use DecimalFormat #.### rather than String.format 0.3f to leave off trailing zeroes
    DecimalFormat format = new DecimalFormat ("#.###");
    StringBuilder result = new StringBuilder ("[ ");
    for (Node x = first; x != null; x = x.next) {
      result.append (format.format (x.item));
      result.append (" ");
    }
    result.append ("]");
    return result.toString ();
  }

  /* Method to create lists */
  public static Playground from(String s) {
    Playground result = new Playground ();
    if ("".equals (s)) return result;

    Node first = null;
    String[] nums = s.split (" ");
    for (int i = nums.length-1; i >= 0; i--) {
      try { 
        double num = Double.parseDouble (nums[i]); 
        first = new Node (num, first);      
      } catch (NumberFormatException e) {
        throw new IllegalArgumentException (String.format ("Bad argument \"%s\": could not parse \"%s\" as a double", s, nums[i]));
      }
    }
    result.first = first;
    return result;
  }
    private static Playground of(double... items) {
        Node next = null;
        for (int i = items.length - 1; i >= 0; i--) {
            next = new Node(items[i], next);
        }
        Playground result = new Playground();
        result.first = next;
        return result;
    }
}

Previous pageContentsNext page