Question

1. Pleae write the following code in Python 3. Also, please show all the outputs. ....

1. Pleae write the following code in Python 3. Also, please show all the outputs.

. Write a simple Rectangle class. It should do the following:

Accepts length and width as parameters when creating a new instance

Has a perimeter method that returns the perimeter of the rectangle

Has an area method that returns the area of the rectangle

Don't worry about coordinates or negative values, etc.

Python provides several modules to allow you to easily extend some of the basic objects in the language. Among these modules are UserDict, UserList, and UserString. (Refer to the chart in Topic 10.3 to see what the methods you need to override look like. Also, since UserDict and UserList are part of the collection module, you can import them using from collections import UserDict and from collections import UserList.)

2. Using the UserList module, create a class called Ulist, and override the __add__, append, and extend methods so that duplicate values will not be added to the list by any of these operations.

3. (Extra Credit) Using the UserDict module, create a class called Odict, which will be just like a dictionary but will "remember" the order in which key/value pairs are added to the dictionary. (Hint: Override the built-in __setitem__method.) Create a new method for the Odict object called okeys, which will return the ordered keys.

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

1. Rectangle Class in python3, this will return area and perimeter:

class Rectangle:
        def __init__(self,length,width):
                self.length=length
                self.width=width
        def perimeter(self):
                perimeter_value= 2*(self.length+self.width)
                return perimeter_value
        def area(self):
                return self.length*self.width
rect_obj=Rectangle(5,10)
print("The perimeter of rectangle is: ",rect_obj.perimeter())
print("The are of rectangle is: ",rect_obj.area())

Output: length =5 and width=10

2 &3) UserDict and UserList:

from collections import UserDict

class Odict(UserDict):
    def __init__(self, data = {}, **kw):
        UserDict.__init__(self)
        self.update(data)
        self.update(kw)
        self.keylist = []

    def __setitem__(self,key,value):
        self.data[key] = value
        self.keylist.append(key)

    def okeys(self):
        return self.keylist


myDict = Odict()
myDict['d'] = 'value5'
myDict['a'] = 'value4'
myDict['e'] = 'value6'

print(myDict.okeys())

from collections import UserList
class Ulist(UserList):
    def __init__(self, data = [], **kw):
        UserList.__init__(self)
        self.data = data

    def __add__(self, newvalue):
        for item in newvalue:
            if item in self.data:
                print ("%r already exists, not adding." % item)
            else:
                self.data += [item]
        return self.data

    def append(self,newvalue = None):
        if newvalue in self.data:
            print ("%r already exists, not adding." % newvalue)
        else:
            return self.data.append(newvalue)

    def extend(self, newvalue = []):
        for item in newvalue:
            if item in self.data:
                print ("%r already exists, not adding." % item)
            else:
                return self.data.extend(item)
        return self.data

mylist = Ulist([1,2,3])
#mylist.__add__([5,6])
#mylist.append('a')
#mylist.append([3])
#mylist.extend([1,2,3,'a'])
print( mylist)

output:

you can give inputs in program or you can uncomment few inputs above

Add a comment
Know the answer?
Add Answer to:
1. Pleae write the following code in Python 3. Also, please show all the outputs. ....
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
  • Write a Python class, Circle, constructed by a radius and two methods which will compute the...

    Write a Python class, Circle, constructed by a radius and two methods which will compute the area and the perimeter of a circle. Include the constructor and other required methods to set and get class attributes. Create instances of Circle and test all the associated methods. Write a Python class, Rectangle, constructed by length and width and a method which will compute the area of a rectangle. Include the constructor and other required methods to set and get class attributes....

  • Using Python Write the following well-documented (commented) program. Create a class called Shape that has a...

    Using Python Write the following well-documented (commented) program. Create a class called Shape that has a method for printing the area and the perimeter. Create three classes (Square, Rectangle, and Circle) which inherit from it. Square will have one instance variable for the length. Rectangle will have two instance variables for the width and height. Circle will have one instance variable for the radius. The three classes will have methods for computing the area and perimeter of its corresponding shape....

  • PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE IN VISUAL STUDIO Exercise #1:...

    PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE IN VISUAL STUDIO Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods: 1. Two Class variables of type double named height and width to represent the height and width of the rectangle. Set their default values to 1.0 in the default constructor. 2. A non-argument constructor method to create a default rectangle. 3. Another constructor method to...

  • Python only please, thanks in advance. Type up the GeometricObject class (code given on the back...

    Python only please, thanks in advance. Type up the GeometricObject class (code given on the back of the paper). Name this file GeometricObjectClass and store it in the folder you made (xxlab14) 1. Design a class named Rectangle as a subclass of the GeometricObject class. Name the file RectangleClass and place it in the folder. 2. The Rectangle class contains Two float data fields named length and width A constructor that creates a rectangle with the specified fields with default...

  • java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectang...

    java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectangle, Triangle which extend the Shape class and implements the abstract methods. A circle has a radius, a rectangle has width and height, and a triangle has three sides. Software Architecture: The Shape class is the abstract super class and must be instantiated by one of its concrete subclasses: Circle, Rectangle, or...

  • 1. Please write the following code in Python 3. 2. Please also show all outputs. Create...

    1. Please write the following code in Python 3. 2. Please also show all outputs. Create a dictionary named letter_counts that contains each letter and the number of times it occurs in string1. Challenge: Letters should not be counted separately as upper-case and lower-case. Intead, all of them should be counted as lower-case. string1 = "There is a tide in the affairs of men, Which taken at the flood, leads on to fortune. Omitted, all the voyage of their life...

  • Q2) Interface Create a program that calculates the perimeter and the area of any given 2D...

    Q2) Interface Create a program that calculates the perimeter and the area of any given 2D shape. The program should dynamically assign the appropriate calculation for the given shape. The type of shapes are the following: • Quadrilateral 0 Square . Perimeter: 4xL • Area:LXL O Rectangle • Perimeter: 2(L+W) • Area:LxW Circle Circumference: I x Diameter (TT = 3.14) Area: (TT xD')/4 Triangle (assume right triangle) o Perimeter: a+b+c O Area: 0.5 x base x height (hint: the base...

  • ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class...

    ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used...

  • Project Objectives: To develop ability to write void and value returning methods and to call them...

    Project Objectives: To develop ability to write void and value returning methods and to call them -- Introduction: Methods are commonly used to break a problem down into small manageable pieces. A large task can be broken down into smaller tasks (methods) that contain the details of how to complete that small task. The larger problem is then solved by implementing the smaller tasks (calling the methods) in the correct order. This also allows for efficiencies, since the method can...

  • Type up the GeometricObject class (code given on the back of the paper). Name this file Geometric...

    Python only please, thanks in advance. Type up the GeometricObject class (code given on the back of the paper). Name this file GeometricObjectClass and store it in the folder you made (xxlab14) 1. Design a class named Rectangle as a subclass of the GeometricObject class. Name the file RectangleClass and place it in the folder. 2. The Rectangle class contains Two float data fields named length and width A constructor that creates a rectangle with the specified fields with default...

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