In mathematics, several operations are defined on sets. The union of two sets A
ID: 3914561 • Letter: I
Question
In mathematics, several operations are defined on sets. The union of two sets A and B is a set that contains all the elements that are in A together with all the elements that are in B. The intersection of A and B is the set that contains elements that are in both A and B. The difference of A and B is the set that contains all the elements of A except for those elements that are also in B.
Suppose that A and B are variables of type set in Java. The mathematical operations on A and B can be computed using methods from the Set interface. In particular: A.addAll(B) computes the union of A and B; A.retainAll(B) computes the intersection of A and B; and A.removeAll(B) computes the difference of A and B. (These operations change the contents of the set A, while the mathematical operations create a new set without changing A, but that difference is not relevant to this exercise.)
For this exercise, you should write a program that can be used as a "set calculator" for simple operations on sets of non-negative integers. (Negative integers are not allowed.) A set of such integers will be represented as a list of integers, separated by commas and, optionally, spaces and enclosed in square brackets. For example: [1,2,3] or [17, 42, 9, 53,108]. The characters +, *, and - will be used for the union, intersection, and difference operations. The user of the program will type in lines of input containing two sets, separated by an operator. The program should perform the operation and print the resulting set. Here are some examples:
Input Output
------------------------- -------------------
[1, 2, 3] + [3, 5, 7] [1, 2, 3, 5, 7]
[10,9,8,7] * [2,4,6,8] [8]
[ 5, 10, 15, 20 ] - [ 0, 10, 20 ] [5, 15]
To represent sets of non-negative integers, use sets of type TreeSet<Integer>. Read the user's input, create two TreeSets, and use the appropriate TreeSet method to perform the requested operation on the two sets. Your program should be able to read and process any number of lines of input. If a line contains a syntax error, your program should not crash. It should report the error and move on to the next line of input. (Note: To print out a Set, A, of Integers, you can just say System.out.println(A). Here, the syntax for sets to be the same as that used by the system for outputting a set.)
Explanation / Answer
/**
*
*/
package com.learningbydoing.setoperations;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
/**
* @author Maddipatla Chandra Babu
* @date 25-Feb-2018
*/
public class SetOperations {
static Set<Integer> setOne = new LinkedHashSet<>();
static Set<Integer> setTwo = new LinkedHashSet<>();
static Set<Integer> output = new LinkedHashSet<>();
static List<String> operators = new ArrayList<String>() {
{
add("*");
add("+");
add("-");
}
};
static String operator;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String character;
do {
System.out.println(" Set Operations ");
System.out.println("1. Union");
System.out.println("2. Intersection");
System.out.println("3. Difference");
int choice = Integer.parseInt(scan.nextLine());
switch (choice) {
case 1:
if (processInput(scan)) {
union();
print();
clear();
}
break;
case 2:
if (processInput(scan)) {
intersection();
print();
clear();
}
break;
case 3:
if (processInput(scan)) {
difference();
print();
clear();
}
break;
default:
break;
}
System.out.println(" Do you want to continue (Type y or n) ");
character = scan.nextLine();
} while (character.equalsIgnoreCase("Y"));
scan.close();
}
public static boolean processInput(Scanner scan) {
System.out.println("Enter two Sets separated by operator: Example [1, 2, 3] */+/- [5, 2, 3]");
String input = scan.nextLine();
if (input.contains("+")) {
operator = "+";
return processInput(input, operator);
} else if (input.contains("-")) {
operator = "-";
return processInput(input, operator);
} else if (input.contains("*")) {
operator = "*";
return processInput(input, operator);
} else {
System.out.println("Invalid data");
return false;
}
}
public static boolean processInput(String input, String operator) {
String data = input.substring(1, input.indexOf(operator) - 1).trim();
data = data.trim().substring(0, data.length() - 1).trim();
String[] strings = data.split(",");
String dataTwo = input.substring(input.indexOf(operator) + 1, input.length() - 1).trim();
dataTwo = dataTwo.trim().substring(1).trim();
String[] stringsTwo = dataTwo.split(",");
for (String string : strings) {
setOne.add(Integer.parseInt(string.trim()));
}
for (String string : stringsTwo) {
setTwo.add(Integer.parseInt(string.trim()));
}
return true;
}
public static void union() {
output.addAll(setOne);
output.addAll(setTwo);
}
public static void intersection() {
for (Integer element : setOne) {
if (setTwo.contains(element))
output.add(element);
}
}
public static void difference() {
for (Integer element : setOne) {
if (!setTwo.contains(element))
output.add(element);
}
}
public static void print() {
System.out.println(setOne + " " + operator + " " + setTwo + " : " + output);
}
public static void clear() {
setOne.clear();
setTwo.clear();
output.clear();
}
}
$javac com/learningbydoing/setoperations/SetOperations.java
$java -Xmx128M -Xms16M com/learningbydoing/setoperations/SetOperations
Set Operations
1. Union
2. Intersection
3. Difference
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at com.learningbydoing.setoperations.SetOperations.main(SetOperations.java:67)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.