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...

  • 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...

  • RAPTOR PROGRAMMING Write a program which will receive two integer values as input. Then prompt the...

    RAPTOR PROGRAMMING Write a program which will receive two integer values as input. Then prompt the user to choose one of the following operations and produce an output with the selected operation. 1. Addition 2. Subtraction 3. Multiplication 4. Division 5. Modulo Division Example output with response: Please enter an input: 2 Please enter another input: 3 Please choose from the follow: 1. Addition 2. Subtraction 3. Multiplication 4. Division 5. Modulo Division Output: 2 % 3 is 2.

  • Write another program called calculator that inherits from class average and also has methods that has:...

    Write another program called calculator that inherits from class average and also has methods that has: two methods multiply() to calculate the product of the numbers. One method will have two parameters which are both ints. The second method has three parameters: two ints and a Double. Another method “Power” that finds the power of a number using methods. For example, if the user types in: 5 and 3, the program should print out 125. 3. And another method “Factorial”...

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
ADVERTISEMENT