-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntegerSet.java
More file actions
135 lines (98 loc) · 3.01 KB
/
IntegerSet.java
File metadata and controls
135 lines (98 loc) · 3.01 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import java.util.*;
/**
TODO Write a one-sentence summary of your class here.
TODO Follow it with additional details about its purpose, what abstraction
it represents, and how to use it.
@author TODO Your Name
@version TODO date
@author Period - TODO Your Period
@author Assignment - Ch54 IntegerSet
@author Sources - TODO list collaborators
*/
public class IntegerSet {
private ArrayList<Integer> set;
/* construct a new empty IntegerSet object */
public IntegerSet() {
set = new ArrayList<Integer>();
}
/* utility method: return size of set */
public int size() {
return set.size();
}
/*
* @param True if k is in the set
*/
public boolean contains(int k) {
int count = 0;
for(int i=0; i<set.size();i++)
{
if (set.get(i) == k)
{
count++;
}
}
if (count == 0)
{
return false;
}
return true;
}
/**
* add the num into the set but only if it is not already in the set
* @return True if number was added to the set; False if it did not need to be added
*/
public boolean add(int num) {
int count = 0;
for(int i=0; i<set.size();i++)
{
if (set.get(i) == num
{
count++;
}
}
if(count == 0)
{
return count;
}
}
/* add elements from Array nums to set
* @param nums array of integers to add to the list
* @return count of elements successfully added
* postcondition: set has no duplicates (do not add duplicates)
*/
public int addFromArray(int [] nums) {
// TODO
}
/**
* @param return a string that looks like {1 2 3}
* don't worry about the spaces as long as there is a space between the numbers
* there can be extra spaces before or after the { }
*/
public String toString() {
String s = "{";
// TODO - concatenate all the number of the set into the string s
s = s + "}";
return s;
}
public static void main (String[] args) {
IntegerSet s = new IntegerSet();
System.out.println("Initial size is " + s.size());
System.out.println("Found a 6 in the list: " + s.contains(6));
s.add(2);
s.add(6);
s.add(8);
s.add(2);
s.add(6);
s.add(0);
System.out.println(s);
System.out.println("Size is now: " + s.size()); // should be 4
System.out.println("Adding another 0: " + s.add(0));
System.out.println("Adding a 10: " + s.add(10));
System.out.println("Size is now: " + s.size()); // should be 5
s = new IntegerSet();
int[] n = {2, 6, 8, 2, 6, 0};
int numAdded = s.addFromArray(n);
System.out.println("Size of new set from addFromArray: " + numAdded); // should be 4
System.out.println(s);
}
}