advanced programming, Quizzes of Programming Languages

practise on advanced programming

Typology: Quizzes

2022/2023

Uploaded on 01/22/2023

omnia-nabil-gharieb-ghonem
omnia-nabil-gharieb-ghonem 🇪🇬

5 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Question 1
Name of this behavior-> overloading
pf3
pf4

Partial preview of the text

Download advanced programming and more Quizzes Programming Languages in PDF only on Docsity!

Name of this behavior-> overloading

Question 1 1; For the Calculator class below, complete the testAddLists test case that ensures that Calculator can add two lists element-wise. For example, Calculator.add([1, 2, 3], [4, 5, 6]) would equal [5, 7, 9]. import unittest class Calculator: def add(self, x, y): return x + y def subtract(self, x, y): pass def multiply(self, x, y): pass def divide(self, x, y): return x / y class TestCale(unittest.TestCase): def setUp(self): self.calc = Calculator() def testAddNumbers (self): self .assertEqual(self.calc.add(3, 2), 5) def testAddLists(self): Mention why the test case will fail and discuss. briefly how to fix the issue. If you fix the issue that Calculator.add() can now operate given two numbers or two number lists, state the name of this behavior. Click or tap here to enter text. “If we add the two lists directly with the plus sign, the output will be the concatenation of the two lists ->[1,2,3,4,5,6], it'll not add element by element to be [5,7,9]. .............. to fix this issue: make a small condition in the beginning of the add method to check first if the user's input is a list or a variable by using ‘type()’.........if it's not a list, deal with it, as the default case, as a number -> return X+y...... if yes, it's a list, do a simple for loop to iterate over the whole list elements, element by element with the same index, after checking first if they have the same lengths or not, as we can’t add lists’ elements if they aren't the same length......while iteration, add one by one in the first list with the one in the second list with the same index......then check with assert senctence if they're the same or not, which aren't not dealing as the concatiniation-> [5,7,9] not [1,2,3,4,5,6]. Name of this behavior-> overloading