CS1103 Exercise Unit 4 - Perform the union, intersection and difference operations on sets based on the TreeSet<> class

Questions

Perform the union, intersection and difference operations on sets based on the TreeSet<> class.

1. Codes

SetOperation.java

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
package learningjournal;

import java.util.TreeSet;

/**
* The program performs the union, intersection and difference operations on sets,
* and print the resulting set.
*/

public class SetOpration {
public static void main(String[] args) {
while (true) {
System.out.print("The expression: ");

char character;
TextIO.skipBlanks();

// input enter to exit the program
if (TextIO.peek() == '\n') {
break;
}

// Perform the operation and catch the exception
try {
expressionValue();
} catch (IllegalArgumentException e) {
System.out.println("Error message: " + e.getMessage());
}

// Continue to calculate until pressing the key enter
do {
character = TextIO.getAnyChar();
System.out.print(character);
} while (character != '\n');
}
}


/**
* Perform the operation and print the resulting set
*/
private static void expressionValue() {
char operator;
TreeSet<Integer> set1;
TreeSet<Integer> set2;

//Read set1
set1 = readSet();

//Read the operator
TextIO.skipBlanks();
if (TextIO.peek() != '*' && TextIO.peek() != '+' && TextIO.peek() != '-') {
throw new IllegalArgumentException("The oprator should be *, +, or -");
}
operator = TextIO.getAnyChar();

//Read set2
set2 = readSet();

// Read the key enter
TextIO.skipBlanks();
if (TextIO.peek() != '\n') {
throw new IllegalArgumentException("Unexpected input");
}

// Apply the operator
switch (operator) {
case '+':
set1.addAll(set2); // Union.
break;
case '*':
set1.retainAll(set2); // Intersection.
break;
case '-':
set1.removeAll(set2); // Set difference.
break;
}

System.out.print("The value:" + set1);
System.out.println();
}

/**
* Read a set and return it
*/
private static TreeSet<Integer> readSet() {

TreeSet<Integer> set = new TreeSet<Integer>();

// Read the '[' that begins the set
TextIO.skipBlanks();
if (TextIO.peek() != '[') {
throw new IllegalArgumentException("Expected '[' at start of set.");
}
TextIO.getAnyChar();

// Read an empty set and return
TextIO.skipBlanks();
if (TextIO.peek() == ']') {
TextIO.getAnyChar();
return set;
}

// Read the integers
while (true) {
// Read the next integer and add it to the set.
TextIO.skipBlanks();
if (!Character.isDigit(TextIO.peek())) {
throw new IllegalArgumentException("Expected an integer");
}
Integer n = TextIO.getInt();
set.add(n);

// Read the next character, including ',' or ']'
TextIO.skipBlanks();
if (TextIO.peek() == ']')
break; // ']' marks the end of the set.
else if (TextIO.peek() == ',')
TextIO.getAnyChar(); // Read a comma and continue.
else
throw new IllegalArgumentException("Expected ',' or ']'.");
}

// Read the ']' that ends the set
TextIO.getAnyChar();

return set;
}
}

TexoIO.java
Please click here to download.

2. Outputs

1
2
3
4
5
6
7
8
9
10
11
12
The expression: [1,2,3]+[4,5,6]
The value:[1, 2, 3, 4, 5, 6]

The expression: [1,2,3]-[2,3]
The value:[1]

The expression: [1,2,3]*[2,3,4]
The value:[2, 3]

The expression:

Process finished with exit code 0

​


This is the ending, thanks for reading.