Python Exercise 5

Mannan Ul Haq
0

 Objective:

  • Classes and Objects in Python

Activity:

Task 1:

1. Create a class named `Rectangle`.
2. Define its private variables `length`, `width`, and `area`.
3. Create an initializer (`__init__` method) to initialize these variables.
4. Create these methods:
    - `set_length()`: Define a method that sets the length. Check if the value is greater than zero, else print “length should be greater than zero”.
    - `set_width()`: Define a method that sets the width. Check if the value is greater than zero, else print “width should be greater than zero”.
    - `calculate_area()`: Define a method that sets the area = length*width.
    - `get_length()`: Return the length.
    - `get_width()`: Return the width.
    - `get_area()`: Return the area.
    - `set_values()`: Define a method that takes both length and width as input and sets those values and also sets the area.
    - `is_square()`: Return `True` if the rectangle is a square, else return `False`.
    - `display()`: Display the rectangle using zeros.

Task 2:

1. Create a class named `Cuboid`.
2. Define its private variables `length`, `width`, and `height`.
3. Create an initializer with default arguments to initialize these variables.
4. Create these methods:
    - `set_length()`: Define a method that sets the length. The length cannot be zero.
    - `set_width()`: Define a method that sets the width. The width cannot be zero.
    - `set_height()`: Define a method that sets the height. The height cannot be zero.
    - `get_length()`: Return the length.
    - `get_width()`: Return the width.
    - `get_height()`: Return the height.
    - `calculate_area()`: Calculate and return the surface area.
    - `calculate_volume()`: Calculate and return the volume.
    - `display()`: Display the length, width, height, and volume.
    - `is_cube()`: Return `True` if the cuboid is a cube, else return `False`.


Task 3:

Create a main function that does the following:
1. Create an instance of the `Rectangle` class.
2. Set the length and width of the rectangle.
3. Calculate and display the area of the rectangle.
4. Display the rectangle.
5. Check and print if the rectangle is a square.
6. Create an instance of the `Cuboid` class.
7. Set the length, width, and height of the cuboid.
8. Calculate and display the area and volume of the cuboid.
9. Display the properties of the cuboid.
10. Check and print if the cuboid is a cube.


Solution:

class Rectangle:
    def __init__(self, length=0, width=0):
        self.__length = length
        self.__width = width
        self.__area = 0

    def set_length(self, length=None):
        if length is not None:
            self.__length = length
        if self.__length < 1:
            print("Length should be Greater than Zero!")

    def set_width(self, width=None):
        if width is not None:
            self.__width = width
        if self.__width < 1:
            print("Width should be Greater than Zero!")

    def calculate_area(self):
        self.__area = self.__length * self.__width

    def get_length(self):
        return self.__length

    def get_width(self):
        return self.__width

    def get_area(self):
        return self.__area

    def set_values(self):
        self.__length = int(input("Enter Length: "))
        self.__width = int(input("Enter Width: "))
        self.calculate_area()

    def is_square(self):
        return self.__length == self.__width

    def display(self):
        for _ in range(self.__length):
            print('0' * self.__width)

    def display2(self):
        for _ in range(self.__length):
            print('*' * self.__width)


class Cuboid:
    def __init__(self, length=5, width=5, height=4):
        self.__length = length
        self.__width = width
        self.__height = height

    def __copy__(self, other):
        print("Copy Constructor Called!!!")
        self.__length = other.__length
        self.__width = other.__width
        self.__height = other.__height

    def set_length(self):
        while True:
            self.__length = int(input("Enter Length greater than Zero: "))
            if self.__length > 0:
                break
            else:
                print("Error!")

    def set_width(self):
        while True:
            self.__width = int(input("Enter Width greater than Zero: "))
            if self.__width > 0:
                break
            else:
                print("Error!")

    def set_height(self):
        while True:
            self.__height = int(input("Enter Height greater than Zero: "))
            if self.__height > 0:
                break
            else:
                print("Error!")

    def get_length(self):
        return self.__length

    def get_width(self):
        return self.__width

    def get_height(self):
        return self.__height

    def calculate_area(self):
        area = self.__length * self.__width
        print("Area:", area)

    def calculate_volume(self):
        return self.__length * self.__width * self.__height

    def display(self, volume):
        print("Length:", self.__length, "Width:", self.__width, "Height:", self.__height, "Volume:", volume)

    def is_cube(self):
        return self.__length == self.__width == self.__height


def main():
    while True:
        choice2 = int(input("Press 1: For Task 1.\nPress 2: For Task 2.\nPress 3: For Task 3.\n"))
        if choice2 == 1:
            area1 = Rectangle()
            area2 = Rectangle(10, 5)

            print("For Default: length =", area1.get_length(), "and width =", area1.get_width())
            area1.calculate_area()
            print("Area =", area1.get_area())

            print("For Parametrized: length =", area2.get_length(), "and width =", area2.get_width())
            area2.calculate_area()
            print("Area =", area2.get_area())

            area1.set_values()

            if area1.is_square():
                print("Length is equal to Width.")
            else:
                print("Length is not equal to Width.")

            area1.display()

        elif choice2 == 2:
            cube1 = Cuboid()
            cube2 = Cuboid(6, 6, 3)

            print("For Default: length =", cube1.get_length(), "width =", cube1.get_width(), "height =", cube1.get_height())
            print("For Parametrized: length =", cube2.get_length(), "width =", cube2.get_width(), "height =", cube2.get_height())

            cube3 = Cuboid(cube2.get_length(), cube2.get_width(), cube2.get_height())

            cube1.set_length()
            cube1.set_width()
            cube1.set_height()

            cube1.calculate_area()

            volume = cube1.calculate_volume()
            cube1.display(volume)

            if cube1.is_cube():
                print("Length is equal to Width and Height.")
            else:
                print("Length is not equal to Width and Height.")

        elif choice2 == 3:
            area3 = Rectangle()
            area4 = Rectangle(3, 9)

            print("For Default: length =", area3.get_length(), "and width =", area3.get_width())
            print("For Parametrized: length =", area4.get_length(), "and width =", area4.get_width())

            print("When Length = 0 and Width = -1.")
            area3.set_length()
            area3.set_width()

            print("When Length = 3 and Width = 9.")
            area4.set_length()
            area4.set_width()
            area4.calculate_area()
            print("Area =", area4.get_area())

            area3.set_values()

            print("Length:", area3.get_length(), "Width:", area3.get_width(), "Area:", area3.get_area())

            if area3.is_square():
                print("Length is equal to Width.")
            else:
                print("Length is not equal to Width.")

            area3.display2()

            cube3 = Cuboid()
            cube4 = Cuboid(6, 6, 3)

            print("For Default: length =", cube3.get_length(), "width =", cube3.get_width(), "height =", cube3.get_height())
            print("For Parametrized: length =", cube4.get_length(), "width =", cube4.get_width(), "height =", cube4.get_height())

            cube5 = Cuboid(cube3.get_length(), cube3.get_width(), cube3.get_height())

            cube3.set_length()
            cube3.set_width()
            cube3.set_height()

            cube3.calculate_area()

            volume = cube3.calculate_volume()
            print("Volume:", volume)

            print("Length:", cube3.get_length(), "Width:", cube3.get_width(), "Height:", cube3.get_height())

            print("\nCalling Display Function!")
            cube3.display(volume)

            if cube3.is_cube():
                print("Length is equal to Width and Height.")
            else:
                print("Length is not equal to Width and Height.")

        repeat_program = input("\nPress y to Repeat Program! Otherwise press any key: ")
        if repeat_program.lower() != 'y':
            break

main()


Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !