forked from erasmuss22/Music-Player
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestLinkedSet.java
More file actions
173 lines (155 loc) · 4.86 KB
/
TestLinkedSet.java
File metadata and controls
173 lines (155 loc) · 4.86 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
///////////////////////////////////////////////////////////////////////////////
// ALL STUDENTS COMPLETE THESE SECTIONS
// Title: TestLinkedSet.java
// Files: LinkedSet.java, SimpleSet.java, LinkedSetIterator.java
// Semester: Spring 2011
//
// Author: Erin Rasmussen ejrasmussen2@wisc.edu
// CS Login: rasmusse
// Lecturer's Name: Beck Hasti
// Lab Section: Lecture 2
//
//
//////////////////////////// 80 columns wide //////////////////////////////////
import java.util.*;
/**
* This class demonstrates the capability of the LinkedSet class. The user
* enters numbers to make a list and different commands to show what the
* class does. It is not robust against improper input from the user because
* it is a test program and proper input was assumed.
*
* <p>Bugs: not robust for input
*
* @author Erin Rasmussen
*/
public class TestLinkedSet {
public static void main(final String[] args) {
LinkedSet<Integer> ls = new LinkedSet<Integer>();
Iterator<Integer> iter;
Scanner stdin = new Scanner(System.in);
String input = "1";
boolean done = false;
printOptions();
while (!done) {
System.out.print("Enter option ( acheiqlrus ): ");
input = stdin.nextLine();
input = input.toLowerCase(); // convert input to lower case
int position = 0;
LinkedSet<Integer> ls2 = new LinkedSet<Integer>();
LinkedSet<Integer> ls3 = new LinkedSet<Integer>();
// only do something if the user enters at least one character
if (input.length() > 0) {
char choice = input.charAt(0); // strip off option character
String remainder = ""; // used to hold the remainder of input
if (input.length() > 1) {
// trim off any leading or trailing spaces
remainder = input.substring(1).trim();
}
switch (choice) {
case 'a':
if (ls.contains(Integer.parseInt(remainder))){
System.out.println("You already added that!!");
}
ls.add(Integer.parseInt(remainder));
break;
case 'c':
if (ls.contains(Integer.parseInt(remainder))){
System.out.println(Integer.parseInt(remainder)
+ " is in your LinkedSet");
}
else {
System.out.println(Integer.parseInt(remainder)
+ " is not in your LinkedSet");
}
break;
case 'h':
printOptions();
break;
case 'e':
if (ls.isEmpty()){
System.out.println("Your LinkedSet is empty");
}
else {
System.out.println("Your LinkedSet is not" +
" empty and has " + ls.size() + " integers");
}
break;
case 'i':
position = 0;
while (!input.equals("n")){
System.out.println("Please enter an integer for your " +
"LinkedSet" + " to show the intersection");
input = stdin.next();
if (!input.equals("n")){
position = Integer.parseInt(input);
ls2.add(position);
}
}
ls3 = (LinkedSet<Integer>) ls.intersection(ls2);
iter = ls3.iterator();
while (iter.hasNext()){
System.out.print(iter.next() + ", ");
}
System.out.println();
break;
case 'q':
done = true;
System.out.println("quit");
break;
case 'l':
ls.clear();
System.out.println("The size of your LinkedSet is now "
+ ls.size());
break;
case 'r':
ls.remove(Integer.parseInt(remainder));
break;
case 'u':
position = 0;
while (!input.equals("n")){
System.out.println("Please enter an integer for " +
"your LinkedSet" + " to show the union");
input = stdin.next();
if (!input.equals("n")){
position = Integer.parseInt(input);
ls2.add(position);
}
}
ls3 = (LinkedSet<Integer>) ls.union(ls2);
iter = ls3.iterator();
while (iter.hasNext()){
System.out.print(iter.next() + ", ");
}
System.out.println();
break;
case 's':
System.out.println(ls.size());
break;
default: // ignore any unknown commands
break;
}
}
}
}
private static void printOptions() {
System.out.println("a <integer> add an integer to your list");
System.out.println("c <integer> see if the linked set contains your " +
"integer");
System.out.println("e - see if the linked list is empty");
System.out.print("i <LinkedSet> returns the intersections of the " +
"two sets");
System.out.print(" list integers separated by commas 1,2,3 for the " +
"other set");
System.out.println();
System.out.println("q - quit");
System.out.println("h - print options");
System.out.println("u <LinkedSet> - returns the union of the two " +
"sets");
System.out.println("l - clears the set");
System.out.print("r <integer> - remove the integer from the linked " +
"list");
System.out.println();
System.out.print("s - returns the amount of integers in the list");
System.out.println();
}
}