Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

*PYTHON CODE* You will create, and test, a class to model rectangles, the Rectangle class, with the following requirements (1 point each): This class will

*PYTHON CODE*

You will create, and test, a class to model rectangles, the Rectangle class, with the following requirements (1 point each):

This class will contain two (private) data members to represent the width and height of the rectangle; your class will always make sure that these values are always positive (i.e. greater than 0).

Your class will also provide the corresponding setters and getters for these values, and the properties Width and Height to make it easier for the user to change these values (see "I am tired of typing getXXX and setXX" in lab 6).

Your class will provide methods that calculate and return the area of the rectangle (A=width*height) and its perimeter (P=2*width + 2*height).

Your class will also provide the following operations (1 point each):

rectangle1 + rectangle2: returns the smallest rectangle that can fit both rectangle1 and rectangle2. That is, this operation returns a new Rectangle object whose width and height are the largest of rectangle1 and rectangle2. For example, if r1 is a 3x5 rectangle, and r2 is a 4x2 rectangle, then r3 = r1 + r2 will make r3 a 4x5 rectangle.

rectangle1 - rectangle2: returns the largest rectangle that we can fit inside both rectangle1 and rectangle2. That is, this operation returns a new Rectangle object whose width and height is the smallest of rectangle1 and rectangle2. For example, if r1 is a 7x3 rectangle, and r2 is a 4x5 rectangle, then r3 = r1 - r2 will make r3 a 4x3 rectangle.

rectangle *= number: updates the state of the objectrectangle by changing its width and height to the result of multiplying number by these values, respectively. For example, if rectangle is 3x4, the statement rectangle *= 2 doubles the width and height of rectangle.

rectangle1 < rectangle2: returns True if rectangle1 fits inside rectangle2.

rectangle1 == rectangle2 : returns True if both rectangles have the same dimensions.

Save your class in file named rectangle.py

(5 points) Create the test program testRectangle.py in which you show all the features of the Rectangle class. Make sure to add proper print showing what is going on during the execution of the program.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Object Oriented Databases Prentice Hall International Series In Computer Science

Authors: John G. Hughes

1st Edition

0136298745, 978-0136298748

More Books

Students also viewed these Databases questions