Question

In: Computer Science

C# Create class IntegerSet. Each IntegerSet object can hold integers in the range 0-100 inclusively. The...

C#

Create class IntegerSet. Each IntegerSet object can hold integers in the range 0-100 inclusively. The set is represented by an array of bools. Array element a[i] is true if integer i is in the set. Array element a[j] is false if integer j is not in the set. The parameterless constructor initializes the array to the empty set (all false). Provide the following methods:

  1. Method Union creates a third set that the set-theoretic union of two existing sets. ie. if an element is in either set, it will be in the new set.
  2. Method Intersection creates a third set which is the set-theoretic intersection of the two existing sets. ie. if an element is in both sets, it will be in the new set
  3. Method InsertElement inserts integer i into the set
  4. Method DeleteElement deletes integer i from the set
  5. Method ToStringreturns a string containing the set as a list of numbers separated by spaces. Include only elements present in the set. Use --- to represent the empty set
  6. Method IsEqualTo determines whether two sets are equal

Write a console app to test your IntegerSet class. Instantiate several  IntegerSet objects and ensure the above functions work correctly

Solutions

Expert Solution

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Rextester
{
    public class Set
    {
        private bool[] setArr=new bool[101];
        public Set()
        {
            for(int i=0;i<101;i++)
            {
                setArr[i]=false;
            }
        }
        public bool getElement(int element)
        {
            if(element>=0&&element<=100)
            {
                return setArr[element];
            }
            return false;
        }
        public Set intersection(Set s2)
        {
            Set s3=new Set();
            for(int i=0;i<=100;i++)
            {
                if(this.getElement(i)==true&&s2.getElement(i)==true)
                {
                    s3.insertElement(i);
                }
            }
            return s3;
        }
        public bool insertElement(int element)
        {
            if(element>=0&&element<=100)
            {
                setArr[element]=true;
                return true;
            }
            return false;
        }
        public bool deleteElement(int element)
        {
            if(element>=0&&element<=100)
            {
                setArr[element]=false;
                return true;
            }
            return false;
        }
        public string toString()
        {
            string setStr="{ ";
            for(int i=0;i<101;i++)
            {
                if(setArr[i]==true)
                {
                    setStr+=i.ToString()+" ";
                }
            }
            setStr+="}";
            return setStr;
        }
    }
    public class Program
    {
        public static void Main(string[] args)
        {
            //Your code goes here
            Set s1=new Set();
            Set s2=new Set();
            s1.insertElement(10);
            s1.insertElement(20);
            s1.insertElement(17);
            s1.insertElement(2);
            Console.WriteLine("Set-1: "+s1.toString());
            
            s2.insertElement(100);
            s2.insertElement(20);
            s2.insertElement(27);
            s2.insertElement(21);
            s2.insertElement(2);
            Console.WriteLine("Set-2: "+s2.toString());
            
            Set s3=s1.intersection(s2);
            Console.WriteLine("Set-1 intersection Set-2: "+s3.toString());
            
            s3.deleteElement(20);
            Console.WriteLine("Set-3: "+s3.toString());
        }
    }
}


Related Solutions

A Bookstore Application C++ Design the class Book. Each object of the class Book can hold...
A Bookstore Application C++ Design the class Book. Each object of the class Book can hold the following information about a book: title, authors, publisher, ISBN Include the member functions to perform the various operations on the objects of Book. For example, the typical operations that can be performed on the title are to show the title, set the title. Add similar operations for the publisher, ISBN , and authors. Add the appropriate constructors and a destructor (if one is...
Use c++ programming Construct an array of 1000 random integers within range [0, 100] An input...
Use c++ programming Construct an array of 1000 random integers within range [0, 100] An input file input.txt is provide. Each line of input.txt is a query integer that you need to check how many of that number is in your random integer array. For each query integer, fork a new child process to do the counting. The output is for each input query, output the count and child process id. For example: $> query: 13 count: 5 pid: 13342...
Write a program IN C that reads all integers that are in the range of 0...
Write a program IN C that reads all integers that are in the range of 0 to 100, inclusive from an input file named: a.txt and counts how many occurrences of each are in the file. After all input has been processed, display all the values with the number of occurrences that were in are in the input file. Note: The program ignores any number less than 0 or greater than 100. Note: Do not display zero if a number...
CS 211 Q.Adding BIG Integers In C++ an int is in the range 0 to 65535....
CS 211 Q.Adding BIG Integers In C++ an int is in the range 0 to 65535. But what if we need to add larger integers? Say we want to compute the sum 2345566777844567+ 9999988777765768009998. Your task in this assignment is to make this happen. Write a function string add(string a, string b)
In object C Define a class called XYPoint that will hold a Cartesian coordinate (x, y),...
In object C Define a class called XYPoint that will hold a Cartesian coordinate (x, y), where x and y are integers. Define methods to individually set the x and y coordinates of your new point and retrieve their values. Write an Objective-C program to implement your new class and test it (main section of the file). Your test program needs to create two instances of your class. Make sure your program test prints out the values that you have...
Construct an array of 1000 random integers within range [0, 100] An input file input.txt is...
Construct an array of 1000 random integers within range [0, 100] An input file input.txt is provide. Each line of input.txt is a query integer that you need to check how many of that number is in your random integer array. For each query integer, fork a new child process to do the counting. The output is for each input query, output the count and child process id. For example: $> query: 13    count: 5    pid: 13342 $> query: 22   ...
write a c++ program in which for loop iterates over all integers in the range 0...
write a c++ program in which for loop iterates over all integers in the range 0 to 99 inclusive and prints out all of those numbers dividible by both 2 and 5.
C++ Question Create a class for a card in a deck of playing cards. The object...
C++ Question Create a class for a card in a deck of playing cards. The object must contain methods for setting and retrieving the suit and the type of card (type of card meaning 2,3,4,5,6,7,8,9,10,J,Q,K,A). Separate the declaration of the class from its implementation by using the declaration in a header file and the implementation in a .cpp file. Also create a driver program and a makefile which complies the code and makes it able to run. This is more...
c++ Create a class named EvolutionTree which will hold a list of species and their expected...
c++ Create a class named EvolutionTree which will hold a list of species and their expected age, and allow a user to search for a particular species or an age. EvolutionTree should have private data members: species (a string array of size 400 with all the species) speciesAges (an int array of size 400 for all the species' ages). EvolutionTree should have a default constructor which should initialize the species array to empty strings ("") and initialize the speciesAges array...
Create a C++ class with a static member item so that whenever a new object is...
Create a C++ class with a static member item so that whenever a new object is created, the total number of objects of the class can be reported.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT