Run the following console application. This program should produce the union of two sets, but has several errors. Use a variety of debugging techniques to locate and correct the errors.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

In C#:

Run the following console application. This program should produce the union of two sets, but has several errors. Use a variety of debugging techniques to locate and correct the errors. 

NOTE: A set is a collection of like elements that does not contain any duplicates. The union of two sets is the set that contains all the elements in both sets. Formally:

A union B = {x : x Î A or x Î B}
read: A union B is the set of all x such that x is in A or x is in B

Example: A = {1,2,3,4,5} and B = {2,4,6,8}, then A union B = {1,2,3,4,5,6,8} (notice, no duplicates!)

Debug the program. When you find an error, comment out the offending line, give an explanation of the error, write a corrected line, and include a screenshot of your program running with successful output. Submit the modified files and screenshots of any breakpoints you use.

Program

using System;

using System.Collections.Generic;

using System.Linq;          

using System.Text;

using System.Threading.Tasks;

 

namespace Activity_10

{

    class Program

    {

        static void Main( string[] args )

        {

            //make some sets

            Set A = new Set( );

            Set B = new Set( );

 

            //put some stuff in the sets

            Random r = new Random( );

            for(int i = 0; i < 10; i++)

            {

                A.addElement( r.Next( 4 ) );

                B.addElement( r.Next( 12 ) );

            }

 

            //display each set and the union

            Console.WriteLine("A: " + A);

            Console.WriteLine("B: " + B);

            Console.WriteLine("A union B: " + A.union(B));

 

            //display original sets (should be unchanged)

            Console.WriteLine("After union operation");

            Console.WriteLine("A: " + A);

            Console.WriteLine("B: " + B);

          

        }

    }

}

Set

Describe several debugging techniques and scenarios for their use.

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

//find the errors!

namespace Activity_10

{

    class Set

    {

        private List<int> elements;

      

 

        public Set( )

        {

            elements = new List<int>( );           

        }

 

        public bool addElement(int val )

        {

            if (containsElement( val )) return false;

            else

            {

                elements.Add( val );

                return true;

            }

        }

 

        private bool containsElement(int val )

        {

            for(int i = 0; i < elements.Count; i++)

            {

                if (val == elements[ i ])

                    return true;

                else

                    return false;          

            }

            return false;          

        }

 

        public override string ToString( )

        {

            string str = "";

            foreach (int i in elements)

            {

                str += i + " ";

            }

            return str;

        }

 

        public void clearSet( )

        {

            elements.Clear( );

        }

 

        public Set union(Set rhs )

        {

          

            for(int i = 0; i < rhs.elements.Count; i++)

            {

                this.addElement( rhs.elements[ i ] );

            }          

            return rhs;

        }

    }

}

 

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 6 images

Blurred answer
Knowledge Booster
Map
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education