| 
001002
 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
 
 | package ds1.student.linked;
import stdlib.StdOut;
import stdlib.StdRandom;
import java.text.DecimalFormat;
public class LinkedNumFives {
    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) {
        LinkedNumFives example1 = LinkedNumFives.makeExample (1);
        LinkedNumFives example2 = LinkedNumFives.makeExample (5);
        LinkedNumFives example3 = LinkedNumFives.makeExample (7);
        Node x = example1.first;
        StdOut.println (x.item);
    }
    public static LinkedNumFives 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);
        LinkedNumFives result = new LinkedNumFives();
        result.first = x1;
        x1.next = x2;
        x2.next = x3;
        x3.next = x4;
        return result;
    }
    
    public static void main2 (String[] args) {
        LinkedNumFives list1 = LinkedNumFives.from ("5 11 5 5");
        LinkedNumFives list2 = LinkedNumFives.from ("24 35 67");
        int result1 = list1.numFives ();
        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) {
        LinkedNumFives list = LinkedNumFives.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 LinkedNumFives from(String s) {
        LinkedNumFives result = new LinkedNumFives();
        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 LinkedNumFives of(double... items) {
        Node next = null;
        for (int i = items.length - 1; i >= 0; i--) {
            next = new Node(items[i], next);
        }
        LinkedNumFives result = new LinkedNumFives();
        result.first = next;
        return result;
    }
}
 |