Language:EN
Pages: 7
Rating : ⭐⭐⭐⭐⭐
Price: $10.99
Page 1 Preview
the program should perform the operation and print

The program should perform the operation and print the resulting set

lOMoARcPSD|16107334

In mathematics, several operations are de�ned 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 di�erence 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 di�erence of A and B.

/

lOMoARcPSD|16107334

[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]

Start with an empty set.

Read the '[' that begins the set.

lOMoARcPSD|16107334

Return the set.

Read the operator.

Read the second set, B.

In the program, an error check has to be added to make sure that there is a legal operator in the correct position. I also add an error check to make sure that there is no extra data on the line.

The Solution
import java.util.TreeSet;

* The program ends when the user inputs an empty line.

*
* This program is mainly a demonstration of Sets.

while (true) {
char ch;
System.out.print("\n? ");
TextIO.skipBlanks();
if (TextIO.peek() == '\n') {
// The input line is empty.

// Exit the loop and end the program.

} // end main()

/

A = readSet(); // Read the �rst set.

TextIO.skipBlanks();
if (TextIO.peek() != '*' && TextIO.peek() != '+'
&& TextIO.peek() != '-')
throw new IllegalArgumentException(
"Expected *, +, or - after �rst set.");
op = TextIO.getAnyChar(); // Read the operator.

else if (op == '*')
A.retainAll(B); // Intersection.

else
A.removeAll(B); // Set di�erence.

lOMoARcPSD|16107334

* and stores them in a TreeSet that contains objects belonging
* to the wrapper class Integer. The set must be enclosed
* between square brackets and must contain a list of zero or
* more non-negative integers, separated by commas. Spaces
* are allowed anywhere. If the input is not of the correct
* form, an IllegalArgumentException is thrown.

TextIO.getAnyChar(); // Read the ']'.

return set;
}

else
throw new IllegalArgumentException("Expected ',' or ']'.");
}

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

/

Last modi�ed: Wednesday, 13 May 2020, 3:51 PM

Copyright © University of the People 2020. All rights reserved.

/

You are viewing 1/3rd of the document.Purchase the document to get full access instantly

Immediately available after payment
Both online and downloadable
No strings attached
How It Works
Login account
Login Your Account
Place in cart
Add to Cart
send in the money
Make payment
Document download
Download File
img

Uploaded by : Yvette Barrera

PageId: DOC91ED789