Question

Write a program to create a set operation calculator applying object oriented programming principles discussed in...

Write a program to create a set operation calculator applying object oriented programming principles discussed in the class so far. The set operation calculator need to be enclosed inside one class with different methods responsible for performing the different operations listed below. When the constructor (def __init__() ) is called during object instantiation, two separate objects of this class need to be instantiated two sets S1 and S2. These two set objects can be instantiated from two python lists taken from user input. Objects need to be instantiated from a main function and the program will ask the user for what operation he/she wants to perform. A menu with options (eg. option 1 : for addition, option 2: union etc.) should be made available to the user. The set calculator should perform the following operations with those :

1. union.

2. Intersection.

3.subtraction (S1-S2).

The operations listed above (eg. union, intersection, subtraction etc.) should be enclosed in separate python methods with appropriate parameters. There should be a separate main function from where objects will be instantiated and functions will be called with appropriate parameters.

Sample input and outputs for the operations

:Sample input given to the program:

A= [2,4,6]B= [3,6,9]Output:1.Instantiate two ‘set operation calculator’ object from lists A and B2.Union:Union : {2,3,4,5,6,9}3.Intersection:intersection : {6}4.Subtraction:subtraction : {2,4}

I will Like, comment, and subscribe the answers :)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

it n not in self.data: self.data.append(n) def union(self, otherSet): result = self.data[:: ] + otherSet.data[::] return mySe

class mySet:

        def __init__(self, nums = []):
                self.data = []
                
                for n in nums:
                        if n not in self.data:
                                self.data.append(n)

        def union(self, otherSet):
                result = self.data[::] + otherSet.data[::]
                return mySet(result)

        def intersection(self, otherSet):
                result = []
                
                for x in self.data:
                        if x in otherSet.data:
                                result.append(x)
                return mySet(result)

        def subtraction(self, otherSet):
                result = []
                
                for x in self.data:
                        if x not in otherSet.data:
                                result.append(x)
                return mySet(result)

        def __str__(self):
                return str(self.data)
        
def menu(setA, setB):
        print('SetA: ', setA)
        print('SetB: ', setB)
        print('Menu => 1. Union, 2. Intersection, 3. Difference (A - B), 4. Quit')
        return int(input('Enter choice: '))

def main():
        setA = mySet([3, 4, 5, 6, 7, 8, 9])
        setB = mySet([3, 5, 7, 9, 11, 13, 15])

        while True:
                choice = menu(setA, setB)
                if choice == 1:
                        print('result: ', setA.union(setB))
                if choice == 2:
                        print('result: ', setA.intersection(setB))
                if choice == 3:
                        print('result: ', setA.subtraction(setB))
                if choice == 4:
                        break
                print()

main()  
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.

Add a comment
Know the answer?
Add Answer to:
Write a program to create a set operation calculator applying object oriented programming principles discussed in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • It is required to write a Matrix Calculator Program using C Programming Language that could only...

    It is required to write a Matrix Calculator Program using C Programming Language that could only perform addition, subtraction, and multiplication of two matrices of appropriate dimensions. Your program should prompt the user to select a matrix operation from a list of options that contains the aforementioned operations. Then, the user should be prompted to enter the dimensions of the two operand matrices. Next, your program should prompt the user to enter the elements of each matrix row-wise. Your program...

  • Problem 3 (20 pts) It is required to write a Matrix Calculator Program using C Programming...

    Problem 3 (20 pts) It is required to write a Matrix Calculator Program using C Programming Language that could only perform addition, subtraction, and multiplication of two matrices of appropriate dimensions. Your program should prompt the user to select a matrix operation from a list of options that contains the aforementioned operations. Then, the user should be prompted to enter the dimensions of the two operand matrices. Next, your program should prompt the user to enter the elements of each...

  • Description Create an object-oriented program that uses inheritance to perform calculations on a rectangle or a...

    Description Create an object-oriented program that uses inheritance to perform calculations on a rectangle or a square. Sample Output (Your output should be similar to the text in the following box) Rectangle Calculator Rectangle or square? (r/s): r Height: 5 Width: 10 Perimeter: 30 Area: 50 Continue? (y/n): y Rectangle or square? (r/s): s Length: 5 Perimeter: 20 Area: 25 Continue? (y/n): n Thank you for using my app Specifications Use a Rectangle class that provides attributes to store the...

  • Write a C++ Program that simulates a basic calculator using functions which performs the operations of...

    Write a C++ Program that simulates a basic calculator using functions which performs the operations of Addition, Subtraction, multiplication, and Division. ( make sure you cover the case to avoid division by a zero) Display a menu for list of operations that can be calculated and get the input from user about his choice of calculation. Based on user choice of operation, take the input of number/numbers from user. Assume all input values are of type double. Calculations must be...

  • Write a C++ Program that simulates a basic calculator using functions which performs the operations of...

    Write a C++ Program that simulates a basic calculator using functions which performs the operations of Addition, Subtraction, multiplication, and Division. ( make sure you cover the case to avoid division by a zero) Display a menu for the list of operations that can be calculated and get the input from the user about his choice of calculation. Based on user choice of operation, take the input of number/numbers from user. Assume all input values are of type double. Calculations...

  • Write an ARM program that implements a simple four-function calculator and prompts the user to enter...

    Write an ARM program that implements a simple four-function calculator and prompts the user to enter a pair of decimal integers (A and B) followed by a character that specifies one of the operators: ‘+’ for addition to compute A+B                             ‘-‘ for subtraction to compute A-B ‘*’ for multiplication to produce the product A*B ‘/’ for division to produce the quotient A/B. Input should be the pair of numbers followed by the operand followed by a return. For example...

  • A Simple Calculator Summary: Write a console program (character based) to do simple calculations (addition, subtraction,...

    A Simple Calculator Summary: Write a console program (character based) to do simple calculations (addition, subtraction, multiplication and division) of two numbers, using your understanding of Java. Description: You need to write a program that will display a menu when it is run. The menu gives five choices of operation: addition, subtraction, multiplication, division, and a last choice to exit the program. It then prompts the user to make a choice of the calculation they want to do. Once the...

  • Write a C++ program that takes two sets ’A’ and ’B’ as input read from the...

    Write a C++ program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the...

  • Write an ARM program that implements a simple four-function calculator and prompts the user to enter...

    Write an ARM program that implements a simple four-function calculator and prompts the user to enter a pair of decimal integers (A and B) followed by a character that specifies one of the operators: ‘+’ for addition to compute A+B                             ‘-‘ for subtraction to compute A-B ‘*’ for multiplication to produce the product A*B ‘/’ for division to produce the quotient A/B. Input should be the pair of numbers followed by the operand followed by a return. For example...

  • (code in Python using import tkinter) Write a GUI program to develop a simple math calculator....

    (code in Python using import tkinter) Write a GUI program to develop a simple math calculator. The user should be able to enter two numbers, and click one of four buttons to do the +, -, *, or / operations. Then the program will display the result. Note: if the divisor in the / operation is 0, display ‘N/A’ in the result.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT