1
Handout D
for
e01
CS56 W18

LotteryPick.java with blanks

NOTE: ANSWER ON THE EXAM PAPER, NOT THIS SHEET!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.util.ArrayList;

public class LotteryPick extends ArrayList<Integer> {

    public LotteryPick(LotteryPick other) {
	// use superclass constructor
	// in form ArrayList(Collection<? extends E> c) 
	_____________________________
    }

    public LotteryPick(int [] nums) {
        ____________________________
	for (int i: nums) {
	    __________________
	}
	_______________________
    }

    public void sortNumbers() {
	// do it with one line of code,
	// taking advantage of the fact that
	// java.lang.Integer implements Comparable<Integer>

        ___________________________________
    }

    public boolean unique() {
	this.sortNumbers();
	
	for (__________; ___________; ________) {
	    if (this.get(i) == this.get(i-1))
	       __________________
	}
	
	_______________
    }

    public int numMatched(LotteryPick other) {
	if (!this.unique()) {
	    throw new IllegalArgumentException("this contains duplicates");
	}
	if (!other.unique()) {
	    throw new IllegalArgumentException("other contains duplicates");
	}

	// both now sorted; merge the two together, sort and count duplicates
	LotteryPick merged = new LotteryPick(this);
	merged.addAll(other);
	merged.sortNumbers();
	
        _______________
	for (int i=1; i<merged.size(); i++) {
	    if (merged.get(i)==merged.get(i-1))
		dups ++;
	}

        _______________
    }
}

LottoPick.java with blanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.util.ArrayList;
public class LottoPick {

    private ArrayList<Integer> picks;
    
    public LottoPick(LottoPick other) {
	// make sure to do a deep copy, not a shallow one
	// use one line of code
        _____________________________________________
    }

    public LottoPick(int [] nums) {
        ______________________________
	for (int i: nums) {	
	    ____________________
	}
        ________________________
    }

    public ArrayList<Integer> getPicks() {
        return new ArrayList<Integer>(this.picks);
    }
    
    public void sortNumbers() {
	// do it with one line of code,
	// taking advantage of the fact that
	// java.lang.Integer implements Comparable<Integer>
        ___________________________________________
    }

    public boolean unique() {
	_______.sortNumbers();
	for (int ______; _________; ________) {
	    if (picks.get(i) == picks.get(i-1))
		_________________
	}
	_______________
    }

    public static int numMatched(
    	   LottoPick first,
    	   LottoPick second) {
	if (!first.unique()) {
	    throw new IllegalArgumentException
	    	  ("this contains duplicates");
	}
	if (!second.unique()) {
	    throw new IllegalArgumentException
	    	  ("other contains duplicates");
	}

	// both now sorted; merge the two together, sort and count duplicates
	// HINT: LOOK AT THE IMPLEMENTATION in LotteryPick
	
	LottoPick merged = ______________
        _________________________________;

	merged.sortNumbers();
        __________________
	for (int i=1; i<merged.picks.size(); i++) {
	    if (merged.picks.get(i)==merged.picks.get(i-1))
		dups ++;
	}
        ____________________
    }

    @Override
    public boolean equals(Object o) {
	if (this == o) return true;
	if (o == null) return false;
	if (getClass() != o.getClass()) return false;
	LottoPick other = (LottoPick) o;
	return picks.equals(other.picks);	
    }
    
    public int hashCode() {
	return picks.hashCode();
    }
}
End of Handout