Python Questions. Send Code via .py files please
Python Questions. Send Code via .py files please
Week 3: Python Assignment 3 1) Write a python program to create a class definition for rectangle: The rectangle has two variables: height and width and two methods area and perimeter. W rite a main program to get instance of the class rectangle, then compute its area and perimete r by calling the methods. Main program sample : r = Rectangle( 5, 4 ) # width 5 and height 4 area = r.area() perimeter = r.perimeter() # print area and perimeter Add the following methods to the rectangle class : 1) getHeight() that returns the height of the rectangle 2) getWidth that returns the width of the rectangle 3) setHeight(h) that sets the height of the rectangle to a new value h 4) setWidth(w) that sets the width of the rectangle to a new value w Test the above methods with something like: r = Rectangle(5, 4) area = r.area() perimeter = r.perimeter() r.setWidth(10) r.setHeight(15) area = r.area() perimeter = r.perimeter() # print h eight and width by calling the methods getHeight and getWidth # print area and perimeter Write a separate function (not inside the rectangle) , area_difference , that takes two Rectangle instances as parameters and returns the signed difference in area between them. “signed difference” means that rather than always returning a positive number, the sign of the return value should be negative if the first rectangle is smaller. Test your code with: r1 = Rectangle (10, 10) r2 = Rectangle (15, 20) # print with a suitable message : the area difference by calling the # method area_difference(r 1, r2) 2) Write a python program to read a file shape.txt that contains a maze shape as follows : “********** ” “* *” “* *** * *** *” “* *” “**********” Each star is a boxes of width=40 and height=40. Your program should draw for each star a small square of size (40, 40) using create_rectangle in t kinter The output should be something like: Extend your program to include P for pla yer at any location in the maze; like: “********** ” “* P *” “* *** * *** *” “* *” “**********” And the output is: One way to do Pacman is to use create_arc and create_oval; the syntax as follows: canvas. create_ arc(top_left_col umn , top_left_row , bottom_right_col um n, bottom_right_row, start , extends , outline, fill , width): use the f ollowing parameters: start = 25 for start angle , extends =315 for end angle , fill=”#ffff00″ for yellow color , and outlin e=”#000″ fo r black color and width= 2 to draw the eye for pacman, use: canvas. create_oval (top_left_column , top_right_row , bottom_right_column, bottom_right_row , fill =fill_color , width ) use fill color fill=”#000″ for black color, and width=0.1
Python Questions. Send Code via .py files please
Python Dr. Omar EL Khatib Classes A class creates a new local namespace where all its attributes are defined. Attributes may be data (variables) or functions . class Person(): “This is a person class” # used for __doc__ functions def greet(self): self.name = “James” # class variables print (‘Hello ‘, self.name) harry = Person() # Output: print( harry.greet ) # Output: Hello James print( harry.greet ()) # Output: ‘This is a person class ‘ print( harry.__ doc __) 2 Classes class ComplexNumber : def __ init __(self, r=0, i=0): self .real = r # local variables self .imag = i def get_data (self): print (self.real , ‘ + ‘, self.imag + ‘j’) # Create anew ComplexNumber instance num1 = ComplexNumber (2, 3) # calling method get_data () # output: 2+3j num1.get_data() # create another ComplexNumber instance: num2 = ComplexNumber (5) # both output: 5+j0 num2.get_data() print (num2.real, “+”, num2.imag, “j”) 3 Class Inheritance • Inheritance is a powerful feature in object oriented programming. • It refers to defining a new class with little or no modification to an existing class. The new class is called derived (or child) class and the one from which it inherits is called the base (or parent) class . class BaseClass : Body of base class class DerivedClass (BaseClass ): Body of derived class 4 Base class Child class Class Inheritance class Polygon: def __ init __(self, no_of_sides ): self.n = no_of_sides self.sides = [0 for i in range( no_of_sides )] def inputSides (self): self.sides = [float(input(“Enter side “+ str (i+1)+” : “)) for i in range( self.n )] def dispSides (self): for i in range( self.n ): print (“Side”,i+1,”is”,self.sides[i]) 5 Class Inheritance class Triangle(Polygon): def __ init __( self): Polygon.__ init __(self, 3) def findArea (self ): a, b, c = self.sides # calculate the semi -perimeter s = (a + b + c) /2 area = (s*(s -a)*(s -b)*(s -c))**0.5 print(‘The area of the triangle is %0.2f’ %area) t = Triangle() # Assume the values entered: 3 5 and 4 t.inputSides () # Output: Side 1 is 3.0 Side 2 is 5.0 Side 3 is 4.0 t.dispSides () # Output: The area of the triangle is 6.00 t.findArea () 6 Python Operator Overloading class Point: def __ init __(self, x=0, y=0): self.x = x self.y = y p1 = Point(1, 2) p2 = Point(2, 3) print(p1+p2) Output: Traceback (most recent call last): File “”, line 9, in print(p1+p2 ) TypeError : unsupported operand type(s) for +: ‘Point’ and ‘Point’ 7 Python Special Functions • Begin and end with double underscore __ • __ str __: print function calls it. class Point: def __ init __(self, x = 0, y = 0): self.x = x self.y = y def __ str __(self): return ‘(‘ + str (self.x ) + ‘,’ + str (self.y ) + ‘)’ p1 = Point(2, 3) print(p1) Output: (2, 3) 8 Overloading the + operator class Point: def __ init __(self, x=0, y=0): self.x = x self.y = y def __ str __(self): return ‘(‘ + str (self.x ) + ‘,’ + str (self.y ) + ‘)’ def __add__(self, other): x = self.x + other.x y = self.y + other.y return Point(x, y ) p1 = Point(1, 2) p2 = Point(2, 3) print (p1+p2) Output: (3, 5) 9 Operator overloading 10 They are special methods with fixed names. Overloading Comparison Operators class Point: def __ init __(self, x=0, y=0): self.x = x self.y = y def __ str __(self): return ‘(‘ + str (x) + ‘,’ + str (y) + ‘)’ def __ lt__(self, other): self_mag = ( self.x ** 2) + ( self.y ** 2) other_mag = ( other.x ** 2) + ( other.y ** 2) return self_mag < other_mag p1 = Point(1,1) p2 = Point( -2, -3) # use less than print(p1




Why Choose Us

  • 100% non-plagiarized Papers
  • 24/7 /365 Service Available
  • Affordable Prices
  • Any Paper, Urgency, and Subject
  • Will complete your papers in 6 hours
  • On-time Delivery
  • Money-back and Privacy guarantees
  • Unlimited Amendments upon request
  • Satisfaction guarantee

How it Works

  • Click on the “Place Order” tab at the top menu or “Order Now” icon at the bottom and a new page will appear with an order form to be filled.
  • Fill in your paper’s requirements in the "PAPER DETAILS" section.
  • Fill in your paper’s academic level, deadline, and the required number of pages from the drop-down menus.
  • Click “CREATE ACCOUNT & SIGN IN” to enter your registration details and get an account with us for record-keeping and then, click on “PROCEED TO CHECKOUT” at the bottom of the page.
  • From there, the payment sections will show, follow the guided payment process and your order will be available for our writing team to work on it.