Using Java programming language, build a class called IntegerSet.
Instructions
- Create class IntegerSet
IntegerSet() public IntegerSet union(IntegerSet iSet) public IntegerSet intersection(IntegerSet iSet) public IntegerSet insertElement(int data) public IntegerSet deleteElement(int data) public boolean isEqualTo(IntegerSet iSet) public String toString()
Here's the sample run:
After set1.insertElement(10), set1 = 0 2 8 10 default IntegerSet is = --- set1 = 0 2 4 6 8 10 12 95 100 set2 = 0 3 6 9 12 set1.union(set2) = 0 2 3 4 6 8 9 10 12 95 100 set1.intersection(set2) = 0 6 12 set1.deleteElement(2) = 0 4 6 8 10 12 95 100 set1.isEqualTo(set1) = true set1.isEqualTo(set2) = false
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
//IntegerSet.java
public class IntegerSet {
// array of booleans
private boolean set[];
// constructor
public IntegerSet() {
// initializing array of capacity 101, so that it can store values from
// 0-100 as booleans
set = new boolean[101];
}
// creates and returns a new set that is the set-theoretic union of the two
// existing sets (the calling object and the parameter). An element is in
// the union if it's in either of the starting two sets
public IntegerSet union(IntegerSet iSet) {
// creating a resultant set
IntegerSet result = new IntegerSet();
// looping through each index in boolean array
for (int i = 0; i < set.length; i++) {
// setting set[i] of result set to true if it is true in either
// this.set[i] OR iSet.set[i]
if (set[i] || iSet.set[i]) {
result.set[i] = true;
}
}
return result;
}
// creates and returns a new set that is the set-theoretic intersection of
// the two existing sets (the calling object and the parameter)
public IntegerSet intersection(IntegerSet iSet) {
// creating a resultant set
IntegerSet result = new IntegerSet();
for (int i = 0; i < set.length; i++) {
// setting set[i] of result set to true if it is true in BOTH
// this.set[i] AND iSet.set[i]
if (set[i] && iSet.set[i]) {
result.set[i] = true;
}
}
return result;
}
// adds the argument (an integer) to the set (the calling object), and also
// should return that set (so that calls can be cascaded)
public IntegerSet insertElement(int data) {
// validating the range of data before setting set[data] to true
if (data >= 0 && data < set.length) {
set[data] = true;
}
return this;
}
// removes the argument (an integer) from the set (the calling object), and
// also should return that set (so that calls can be cascaded)
public IntegerSet deleteElement(int data) {
// validating the range of data before setting set[data] to false
if (data >= 0 && data < set.length) {
set[data] = false;
}
return this;
}
// determines whether two sets are equal (i.e. they have all the same
// elements)
public boolean isEqualTo(IntegerSet iSet) {
for (int i = 0; i < set.length; i++) {
if (this.set[i] != iSet.set[i]) {
// mismatch found
return false;
}
}
// equal
return true;
}
// returns a string containing the set elements as a list of numbers, in
// ascending order
public String toString() {
String str = "";
// appending index values of set, in which the value is true, to str
for (int i = 0; i < set.length; i++) {
if (set[i]) {
str += i + " ";
}
}
// if str is still blank, returning "---" indicating empty
if (str.equals("")) {
return "---";
} else {
// otherwise returning str after stripping last stray white space
return str.trim();
}
}
// main method for testing
public static void main(String[] args) {
// creating two sets, demonstrating different methods
IntegerSet set1 = new IntegerSet();
set1.insertElement(1);
set1.insertElement(3);
set1.insertElement(4);
set1.insertElement(7);
IntegerSet set2 = new IntegerSet();
set2.insertElement(2);
set2.insertElement(3);
set2.insertElement(4);
set2.insertElement(8);
set2.insertElement(9);
System.out.println("set1: " + set1);
System.out.println("set2: " + set2);
System.out.println("set1 union set2: " + set1.union(set2));
System.out
.println("set1 intersection set2: " + set1.intersection(set2));
System.out.println("set1 is equal to set2: " + set1.isEqualTo(set2));
System.out.println("set1 delete element 1: " + set1.deleteElement(1));
System.out.println("set1 delete element 7: " + set1.deleteElement(7));
System.out.println("set2 delete element 2: " + set2.deleteElement(2));
System.out.println("set2 delete element 9: " + set2.deleteElement(9));
System.out.println("set2 delete element 8: " + set2.deleteElement(8));
System.out.println("set1 is equal to set2: " + set1.isEqualTo(set2));
}
}
/*OUTPUT*/
set1: 1 3 4 7
set2: 2 3 4 8 9
set1 union set2: 1 2 3 4 7 8 9
set1 intersection set2: 3 4
set1 is equal to set2: false
set1 delete element 1: 3 4 7
set1 delete element 7: 3 4
set2 delete element 2: 3 4 8 9
set2 delete element 9: 3 4 8
set2 delete element 8: 3 4
set1 is equal to set2: true
Using Java programming language, build a class called IntegerSet. Instructions - Create class IntegerSet An IntegerSet...
Intro to java project template with instructions below * IntegerSet.java */ /** * * @author StudentName */ public class IntegerSet { /** * Creates a new instance of IntegerSet */ // TODO: implement the constructor /** * Return a new IntegerSet containing the union of the two IntegerSet objects * passed as arguments */ // TODO: implement the union method /** * Return a new IntegerSet containing the intersection of the two IntegerSet objects * passed...
Using Java programming, The Set interface in the API has the methods addAll(Collection<? extends E> c) and retainAll(Collections<? > c), which acts like union and intersection respectively. However, these modify the set it is called on. This means after set1.addAll(set2) or set1.retainsAll(set2) is executed, set1 will be the union or intersection of the two sets, respectively. Choose ONE of the implementations below Ordered lists Binary search trees Hash tables Add a method in that implementation that returns the union of...
Use C++ programing language
Please don't use other Chegg solved solutions, make it new
thanks.
Assignment requirements
Pre-requisite: Please read zyBook
Chapter 6 Sets and understand the three operations of
Sets (union, intersection, and difference) .
You will be creating your custom Set (think a list of unique
elements) class in C++ or a language of your choice. Please do NOT
use STL, or any pre-defined library for this assignment.
1. The data type of the set collection: array (or...
I am having trouble with my C++ program.... Directions: In this lab assignment, you are to write a class IntegerSet that represents a set of integers (by definition, a set contains no duplicates). This ADT must be implemented as a singly linked list of integers (with no tail reference and no dummy head node), but it need not be sorted. The IntegerSet class should have two data fields: the cardinality (size) of the set, and the head reference to the...
import java.util.*; /** * A class that implements the ADT set by using a linked bag. * The set is never full. * * */ public class LinkedSetWithLinkedBag> implements SetInterface { private LinkedBag setOfEntries; /** * Creates a set from a new, empty linked bag. */ public LinkedSetWithLinkedBag() { //TODO Project1 } // end default constructor public boolean add(T newEntry) { //TODO Project1 // new node is at beginning of chain if(this.setOfEntries.isEmpty()) { if (!this.setOfEntries.contains(newEntry)) this.setOfEntries.add(newEntry); } return true; //...
can i please get help on this? i don't know how to do it and I'm so confused. This is in java. This is what I need to do. Thank you so much. In this lab assignment, you are to write a class IntegerSet that represents a set of integers (by definition, a set contains no duplicates). This ADT must be implemented as a singly linked list of integers (with no tail reference and no dummy head node) , but...
program will enter data into two single dimension arrays (do not store duplicate values in arrays) program will find the union and intersection of the two arrays using one function program will find the symmetric difference of two arrays program will display the union, intersection, and symmetric difference */ short* input_data(short size); // function to dynamically allocate and array and enter data into the array void display_data(short *data, short size); // function to display data in an array void get_union_intersection(short...
Create a LIFO class with following methods in java - public LifoList() The default constructor for LifoList class which will initialize your class variables maxSize to 0 and the int array to null. public LifoList(int maxSize) The constructor for LifoList class which takes the int input maxSize to initialize the size of the array. You should NOT reinitialize the array elsewhere. For example, creating an object as new LifoList(5) should create a LifoList whose maximum size is 5. If the...
Question from Object-Oriented Data Structures Using Java 4th Edition Chapter 5 Question 30 Add the following methods to the LinkedCollection class, and create a test driver for each to show that they work correctly. Code each of these methods by accessing the internal variables of the LinkedCollection, not by calling the previously defined methods of the class.String toString() creates and returns a string that correctly represents the current collection. 1. Such a method could prove useful for testing and debugging...
Part I: Create a program class named TestArrays This class should have a main() method that behaves as follows: Create an integer array of size 10. Using a loop, fill the elements with increments of 5, starting with 5 in the first element. Using the array elements, sum the second, fifth and seventh elements. Display the sum with a label. Change the fourth element to 86. Subtract 9 from the ninth element. Using a loop, display the elements of the...