Question

I could really use the help. Problem Develop a script to demonstrate an understanding of the...

I could really use the help.

Problem Develop a script to demonstrate an understanding of the overload (overwrite) methods of using Python operators. This lesson will focus on the “+” operator and the “__add__” method. The program must have the following: Demonstration of an understanding of how to use the + operator for basic addition of two integer numbers Demonstration of an understanding of how to use the + operator for a concatenation of lists Demonstration of an understanding of how to use the + operator for a concatenation of strings Demonstration of an understanding of how to overload the __add__ method Solving the Problem:

Step 1 Create a basic set of Python commands to demonstrate the addition of two integer numbers, and display the results in the operator interface window in PyCharm.

Step 2 Next, review the first example of an overloaded operator. In Python, the __add__ method that is being used to sum the two integer numbers above can also accept a list of numbers and concatenate them.

Step 3 Now, review the second example of an overloaded operator. In Python, the __add__ method that is being used to sum the two integer numbers above can also accept two strings and concatenate them.

Step 4 Lastly, overload the __add__ method, the method that has been used to sum two numbers, combine lists, and concatenate strings. Overload the method to format the output using the following code: return ‘%s plus %s’ % (self, x). Step 5 Run the scripts, and verify that the output matches the screenshot below. I'm really lost on how to finish this

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

For step 1 to 3(__add__ method has not been modified yet):

class Item:
    def __init__(self, x):
        self.x = x

    def __add__(self, otherItem):
        return self.x + otherItem.x

# Step 1
int1 = Item(5)
int2 = Item(6)
print(int1 + int2)

# Step 2
list1 = Item([1, 2, 3])
list2 = Item([4, 5, 6])
print(list1 + list2)

# Step 3
string1 = Item("Hello ")
string2 = Item("World")
print(string1 + string2)

OUTPUT:

Code after modified __add__ method:

class Item:
    def __init__(self, x):
        self.x = x

    # Step 4
    def __add__(self, otherItem):
        return '%s plus %s = ' % (self.x, otherItem.x) + str(self.x + otherItem.x)

# Step 1
int1 = Item(5)
int2 = Item(6)
print(int1 + int2)

# Step 2
list1 = Item([1, 2, 3])
list2 = Item([4, 5, 6])
print(list1 + list2)

# Step 3
string1 = Item("Hello ")
string2 = Item("World")
print(string1 + string2)

OUTPUT:

FOR ANY HELP JUST DROP A COMMENT

Add a comment
Know the answer?
Add Answer to:
I could really use the help. Problem Develop a script to demonstrate an understanding of the...
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
  • Hello! I need help understanding this question. I would really appreciate it if anybody could help...

    Hello! I need help understanding this question. I would really appreciate it if anybody could help me. thank you! 20. (Problem 16) in the bacterium Escherichia coll, the amino acid tryptophan is synthesized in a multistep pathway, beginning with an organic precursor called chorismate: Chorismate ——Y-Tryptophan 2 3 The numbers denote steps in the pathway, each catalyzed by an enzyme. There are five bacterial genes that encode polypeptides associated with these enzymes, called trpa, trp8, trpc, trp, and trpe. A...

  • Could anyone please help with this Python code assignment? In this programming assignment you are to...

    Could anyone please help with this Python code assignment? In this programming assignment you are to create a program called numstat.py that reads a series of integer numbers from a file and determines and displays the following: The name of the file. The sum of the numbers. The count of how many numbers are in the file. The average of the numbers. The average is the sum of the numbers divided by how many there are. The maximum value. The...

  • Can someone help me with this code, I not really understanding how to do this? import...

    Can someone help me with this code, I not really understanding how to do this? import java.util.ArrayList; import java.util.HashMap; import java.util.Map; /** * @version Spring 2019 * @author Kyle */ public class MapProblems { /** * Modify and return the given map as follows: if the key "a" has a value, set the key "b" to * have that value, and set the key "a" to have the value "". Basically "b" is confiscating the * value and replacing it...

  • PLEASE DO THIS IN PYTHON!! 1.) Goal: Become familiar with The Python Interpreter and IDLE. Use...

    PLEASE DO THIS IN PYTHON!! 1.) Goal: Become familiar with The Python Interpreter and IDLE. Use IDLE to type, compile, and run (execute) the following programs. Name and save each program using the class name. Save all programs in one folder, call it Lab1-Practices. Save all programs for future reference as they illustrate some programming features and syntax that you may use in other labs and assignments. ================================== Program CountDown.py =================================== # Program CountDown.py # Demonstrate how to print to...

  • Java please answer A to I please dont type the answer on paper please use the...

    Java please answer A to I please dont type the answer on paper please use the computer A.   Explain why alpha cannot be accessed by other members of its class. B.   In the program, how can you determine the type of access modifier? C.   Describe the differences and similarities of beta and gamma in program, within the context of access specifiers and class member accessibility. D.   Explain how objects, a and b, are passed to the method. E.    Why...

  • Could anyone help add to my python code? I now need to calculate the mean and...

    Could anyone help add to my python code? I now need to calculate the mean and median. In this programming assignment you are to extend the program you wrote for Number Stats to determine the median and mode of the numbers read from the file. You are to create a program called numstat2.py that reads a series of integer numbers from a file and determines and displays the following: The name of the file. The sum of the numbers. The...

  • I really need help with this python programming assignment Program Requirements For part 2, i need...

    I really need help with this python programming assignment Program Requirements For part 2, i need the following functions • get floats(): It take a single integer argument and returns a list of floats. where it was something like this def get_floats(n):   lst = []   for i in range(1,n+1):     val = float(input('Enter float '+str(i)+': '))     lst.append(val)   return lst • summer(): This non-void function takes a single list argument, and returns the sum of the list. However, it does not use...

  • This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation...

    This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation inside a class. Task One common limitation of programming languages is that the built-in types are limited to smaller finite ranges of storage. For instance, the built-in int type in C++ is 4 bytes in most systems today, allowing for about 4 billion different numbers. The regular int splits this range between positive and negative numbers, but even an unsigned int (assuming 4 bytes)...

  • This is a C++ probelm, I am really struggling with that...... Can anyone help me?? Write...

    This is a C++ probelm, I am really struggling with that...... Can anyone help me?? Write a fraction class whose objects will represent fractions. For this assignment you aren't required to reduce your fractions. You should provide the following member functions: A set() operation that takes two integer arguments, a numerator and a denominator, and sets the calling object accordingly. Arithmetic operations that add, subtract, multiply, and divide fractions. These should be implemented as value returning functions that return a...

  • Program Purpose In this program you will demonstrate your knowledge in programming OOP concepts, such as classes, encapsulation, and procedural programming concepts such as lınked lists, dynamic me...

    Program Purpose In this program you will demonstrate your knowledge in programming OOP concepts, such as classes, encapsulation, and procedural programming concepts such as lınked lists, dynamic memory allocation, pointers, recursion, and debugging Mandatory Instructions Develop a C++ object oriented solution to the Towers of Hanoi puzzle. Your solution will involve designing two classes one to represent individual Disk and another to represent the TowersOfHanoi game. TowersOfHanoi class will implement the game with three linked lists representing disks on each...

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