Question

NEEDS TO BE IN PYTHON: (The Rectangle class) Following the example of the Circle class in...

NEEDS TO BE IN PYTHON:

(The Rectangle class)

Following the example of the Circle class in Section 9.2, design a class named Rectangle to represent a rectangle. The class contains:

- Two data fields named width and height.
- A constructor that creates a rectangle with the specified width and height. The default values are 1 and 2 for the width and height, respectively.
- A method named getArea() that returns the area of this rectangle.
- A method named getPerimeter() that returns the perimeter.

Draw the UML diagram for the class, and then implement the class. Write a test program that creates two Rectangle objects—one with width 4 and height 40 and the other with width 3.5 and height 35.7. Display the width, height, area, and perimeter of each rectangle in this order.

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

UML Diagram for the Rectangle Class:

________________________________________________________________________________________________

Python Program:

class Rectangle:
   """
       Rectangle class
   """
  
   def __init__(self, width=1, height=2):
       """
           Constructor
       """
       # Initializing private variables
       self.__width = width;
       self.__height = height;
      
   def getWidth(self):
       """
           Function that returns width of a rectangle
       """
       return self.__width;
      
   def getHeight(self):
       """
           Function that returns height of a rectangle
       """
       return self.__height;
  
   def getArea(self):
       """
           Function that returns area of a rectangle
       """
       return self.__width * self.__height;
      
   def getPerimeter(self):
       """
           Function that returns perimeter of a rectangle
       """
       return 2 * (self.__width + self.__height);
      

def main():
   """
       Main function
   """
   # Creating object
   rect = Rectangle(4, 40);
  
   # Printing data
   print("\n A " + str(rect.getWidth()) + " x " + str(rect.getHeight()) + " rectangle has an area of " + str(rect.getArea()) + " and a perimeter of " + str(rect.getPerimeter()) + ". \n");
  
  
   # Creating object
   rect1 = Rectangle(3.5, 35.7);
  
   # Printing data
   print("\n A " + str(rect1.getWidth()) + " x " + str(rect1.getHeight()) + " rectangle has an area of " + str(rect1.getArea()) + " and a perimeter of " + str(rect1.getPerimeter()) + ". \n");
  

# Calling main function
main();   

___________________________________________________________________________________________

Code Screenshot:

_________________________________________________________________________________________

Sample Run:

Add a comment
Know the answer?
Add Answer to:
NEEDS TO BE IN PYTHON: (The Rectangle class) Following the example of the Circle class 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
  • Following the example of the Circle class in Section 8.2, design a class named Rectangle to...

    Following the example of the Circle class in Section 8.2, design a class named Rectangle to represent a rectangle. The class contains: ■ Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. ■ A no-arg constructor that creates a default rectangle. ■ A constructor that creates a rectangle with the specified width and height. ■ A method named getArea() that returns the...

  • JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify...

    JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with the specified width and height. A method named getArea() that returns the area of this rectangle. design...

  • using c++ E2b:Design a class named Rectangle to represent a rectangle . The class contains: (1)...

    using c++ E2b:Design a class named Rectangle to represent a rectangle . The class contains: (1) Two double data members named width and height which specifies the width and height of the rectangle . (2) A no-arg constructor that creates a rectangle with width 1 and height 1. (3) A constructor that creates a rectangle with the specified width and height . (4) A function named getArea() that returns the area of this rectangle . (5) A function named getPerimeter()...

  • SOLVE IN PYTHON: Exercise #1: Design and implement class Circle to represent a circle object. The...

    SOLVE IN PYTHON: Exercise #1: Design and implement class Circle to represent a circle object. The class defines the following attributes (variables) and methods: A private variable of type double named radius to represent the radius. Set to 1. Constructor Methods: Python : An overloaded constructor method to create a default circle. C# & Java: Default Constructor with no arguments. C# & Java: Constructor method that creates a circle with user-specified radius. Method getRadius() that returns the radius. Method setRadius()...

  • Code a rectangle class extending the geometric obi.

    rectangle class contains tge following; -three data fields name recname,  height and width with default values"Ricky" for recName, 5 for the height and 15 for the width.-default constructor with no arguments -constructor that accept width  and height to create  a rectangle  -the accessor of all data feilds-methods that returns the area of the rectangle called getArea()-methods that returns the perimeter of thr rectangle called getPerimeter()-methods that returns the spring description of the rectangle called toString()    Note: the toString() method is...

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

  • (The Triangle class) Design a class named Triangle that extends the GeometricObject class. The Triangle class...

    (The Triangle class) Design a class named Triangle that extends the GeometricObject class. The Triangle class contains: - Three float data fields named side1, side2, and side3 to denote the three sides of the triangle. - A constructor that creates a triangle with the specified side1, side2, and side3 with default values 1.0. - The accessor methods for all three data fields. - A method named getArea() that returns the area of this triangle. - A method named getPerimeter() that...

  • Design and implement class Circle to represent a circle object. The class defines the following attributes...

    Design and implement class Circle to represent a circle object. The class defines the following attributes (variables) and methods: 1.      A private variable of type double named radius to represent the radius. Set to 1. 2.      Constructor Methods: •        Python : An overloaded constructor method to create a default circle. •        C# & Java: Default Constructor with no arguments. •        C# & Java: Constructor method that creates a circle with user-specified radius. 3.      Method getRadius() that returns the radius. 4.     ...

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

  • 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