Question

In: Computer Science

My visual stuido class IntegerSet Assignment Due: 1:00p, Wednesday, 10/28/2020 Create a class to represent a...

My visual stuido class

IntegerSet Assignment Due: 1:00p, Wednesday, 10/28/2020 Create a class to represent a Set of Integers called IntegerSet, to hold a integers in the range 0 <= x <= 100. Recall that a set cannot contain duplicates. Thus the set will be represented as an array of bool elements. If array element a[i] is true, then the set contains integer i. If array element a[j] is false, then the set does not contain integer j. Provide the following set of methods in your class: 1. A parameterless constructor, that initializes the set to the empty set – a set with no members – that is, a set whose array representation contains all false values. 2. A method IntegerSet Union(IntegerSet) that creates the set-theoretic union third set from two existing sets, one of which is the current object, the other of which is passed it – that is, an element of the third set’s array is set to true if the corresponding element is true in either or both of the existing sets, otherwise the element in the third set is set to false. 3. A method IntegerSet Intersection(IntegerSet) that creates the set-theoretic intersection third set from two existing sets – that is, an element of the third set’s array is set to true if the corresponding element is true in both of the existing sets, but false otherwise. 4. A method bool InsertElement(int) that inserts a new integer k into the set by setting a[k] to true. Return true if the element was inserted, return false if the element already existed. 5. A method bool DeleteElement(int) that deletes integer m from the set by setting a[m] to false. Return true if the element was found and deleted, return false if m was not found in the set. 6. A method string ToString() that returns a string representing the set as a sequence of the numbers present in the set, with a comma and a space between each number. Use “---” to represent an empty set. 7. A method bool IsEqualTo(IntegerSet) that determines whether two sets are equal. You are responsible for only the class. I will write a program that uses your class, to exercise your class’s capabilities and test it’s functionality.

Solutions

Expert Solution

Below is the code that is satisfying your requirement and also sample code in main for using the functions of class.

#include <bits/stdc++.h>
using namespace std;

#define fr(i, a, b) for (i = a; i < b; i++)
const int MAX = 101;
class IntegerSet
{
private:
bool num[MAX];
int size;

public:
IntegerSet(); //constructor

IntegerSet intersectionOfSets(IntegerSet sc1, IntegerSet sc2);
IntegerSet unionOfSets(IntegerSet sc1, IntegerSet sc2);
void insertElement(int element);
void deleteElement(int element);
void toString(); //prints the set
bool isEqualTo(IntegerSet sc1, IntegerSet sc2); //function determines whether two sets are equal
};
IntegerSet::IntegerSet() //creates empty set
{
int i;
fr(i, 0, MAX)
num[i] = 0;
size = 0;
}
IntegerSet IntegerSet::intersectionOfSets(IntegerSet sc1, IntegerSet sc2)
{
IntegerSet sc3;
int i;
fr(i, 0, MAX) if (sc1.num[i] == 1 && sc2.num[i] == 1)
sc3.num[i] = 1;
return sc3;
}

IntegerSet IntegerSet::unionOfSets(IntegerSet sc1, IntegerSet sc2)
{
IntegerSet sc3;
int i;
fr(i, 0, MAX) if (sc1.num[i] == 1 || sc2.num[i] == 1)
sc3.num[i] = 1;
return sc3;
}
void IntegerSet::insertElement(int element)
{
if (element < MAX && element >= 0)
num[element] = 1;
size++;
}

void IntegerSet::deleteElement(int element)
{
if (element < MAX && element >= 0)
num[element] = 0;
size--;
}

void IntegerSet::toString()
{
int i;
if (size == 0)
cout << "---";
else
fr(i, 0, MAX) if (num[i] == 1)
cout
<< i << ", ";
cout << "\n";
}

bool IntegerSet::isEqualTo(IntegerSet sc1, IntegerSet sc2)
{
int i;
fr(i, 0, MAX) if (sc1.num[i] == sc2.num[i]) continue;
else return false;
return true;
}

int main()
{
/*
Use the above and test your input like below
IntegerSet sett;
sett.insertElement(3);
  
sett.toString();
*/
return 0;
}


Related Solutions

Java program. Need to create a class names gradesgraph which will represent the GradesGraphtest program. Assignment...
Java program. Need to create a class names gradesgraph which will represent the GradesGraphtest program. Assignment Create a class GradesGraph that represents a grade distribution for a given course. Write methods to perform the following tasks: • Set the number of each of the letter grades A, B, C, D, and F. • Read the number of each of the letter grades A, B, C, D, and F. • Return the total number of grades. • Return the percentage of...
Use Visual Basic Language In this assignment you will need to create a program that will...
Use Visual Basic Language In this assignment you will need to create a program that will have both a “for statement” and an “if statement”. Your program will read 2 numbers from the input screen and it will determine which is the larger of the 2 numbers. It will do this 10 times. It will also keep track of both the largest and smallest numbers throughout the entire 10 times through the loop. An example of the program would be...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
Invoice Class - Create a class called Invoice that a hardware store might use to represent...
Invoice Class - Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables—a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initializes the four instance variables. If the quantity passed to the constructor is...
Invoice Class - Create a class called Invoice that a hardware store might use to represent...
Invoice Class - Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables—a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initializes the four instance variables. If the quantity passed to the constructor is...
The assignment is to write a class called data. A Date object is intented to represent...
The assignment is to write a class called data. A Date object is intented to represent a particuar date's month, day and year. It should be represented as an int. -- write a method called earlier_date. The method should return True or False, depending on whether or not one date is earlier than another. Keep in mind that a method is called using the "dot" syntax. Therefore, assuming that d1 and d2 are Date objects, a valid method called to...
For this assignment, you will create flowchart using Flowgorithm to represent the logic of a program...
For this assignment, you will create flowchart using Flowgorithm to represent the logic of a program that allows the user to enter a number of dollars and convert it to Euros and Japanese yen. You will have to do some research on current rates of monetary exchange for this one. Don't forget to declare your variables and use output statements to prompt the user to enter specific values prior to including an input statement. You will use an assignment statement...
USE VISUAL STUDIO/VB In this assignment, you will create an array of ten elements, one for...
USE VISUAL STUDIO/VB In this assignment, you will create an array of ten elements, one for name, one for hours worked,and the last for hourly rate. The arrays will receive the information from inputs from the screen.For example, you will ask the following three questions: a) Employee name: b) Hours worked: c) Hourly rate: After you have received all ten records and have inserted them into the array, you will then calculate the hourly rate times the hours worked to...
Objective: Create a program that has a class to represent a cup of coffee that combines...
Objective: Create a program that has a class to represent a cup of coffee that combines the properties: name and caffeine content. The class should also have a method that calculates the number of cups that would be maximally risky to consume in a short period of time. The program should create two instances of the class coffee where the user enters their properties and should output the amount of coffees that consumed would be risky. Requirements: Write a class...
Objective: Create a program that has a class to represent a cup of coffee that combines...
Objective: Create a program that has a class to represent a cup of coffee that combines the properties: name and caffeine content. The class should also have a method that calculates the number of cups that would be maximally risky to consume in a short period of time. The program should create two instances of the class coffee where the user enters their properties and should output the amount of coffees that consumed would be risky. Requirements: Write a class...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT