The Circle Object

The Circle Object

Define a class called ‘Circle’ with private data members: ‘radius’  and ‘color‘ where ‘radius’  is a float value and   ‘color‘  is a string value.  This class should contain a parameterized constructor with 2 arguments to initialize these data members.  The arguments should be radius and color.  The ‘Circle’ class also contain the following methods:

Getter and setter methods for the private variables ‘radius’ and ‘color’.  Method signature should be: get_radius(), set_radius( radius), get_color(), set_color(color)

calculate_area()  –  This function is used to calculate and return the area of the circle.  The formula to calculate area of a circle is :3.14 *radius*radius.

calculate_perimeter() –  This function is used to calculate and return the perimeter of the circle.  The formula to calculate perimeter is : 2*3.14*radius.

Get the values for radius and color from the user and store it into the variables with the names  ‘my_radius‘  and ‘my_color ‘ respectively.  Both variables should be in the global scope. 

Create a method ‘check_validity(my_radius,my_color)’ in the global scope (should not be a class member function) for checking the validity of the radius, the user has entered.  This method should take 2 parameters; radius and the color entered by the user.  If the radius is ‘0’ or a negative value, then this function shouldreturn the Boolean value ‘False’ else it should create a circle object using 2 arguments parameterized constructor.

Store the object returned from the function ‘check_validity(my_radius,my_color)‘ (boolean value ‘False‘ or the circle object) into a reference variable named ‘circle_obj‘ which should be in the global scope.

If the returned value is a ‘Circle’ class object,  then perform 2 operations:
1.    Calculate the area of the circle by invoking the ‘Circle‘ class’s ‘calculate_area()‘ method using the object ‘circle_obj’ and display it.
2.    Calculate the perimeter of the circle by invoking the ‘Circle‘ class’s ‘calculate_perimeter()’ method using the object ‘circle_obj’ and display it.

If the ‘check_validity(my_radius,my_color)’ returns the Boolean value ‘False‘ then display the message “Invalid Radius” and stop the program.

Note:  

  • The values for the variables my_radius and my_color should be user inputs
  • Strictly follow the naming conventions for variables and functions as specified in the problem description.

Sample input 1:
Enter the radius of the circle: 6
Enter the color of the circle: Red

Sample Output 1:
Area is: 113.04
Perimeter is: 37.68

Sample input 2:
Enter the radius of the circle:-8

Sample output 2:
Invalid Radius

Code :-

class Circle:
    def __init__(self,radius,color):
        self.__radius=radius
        self.__color=color
    
    def get_radius(self):
        return self.__radius
    def get_color(self):
        return self.__color
    def set_radius(self,radius):
        self.__radius=radius
    def set_color(self,color):
        self.__color=color
        
    
    def calculate_area(self):
        return 3.14*(self.__radius*self.__radius)
    def calculate_perimeter(self):
        return 2*3.14*self.__radius
        
    
my_radius=float(input("Enter the radius of the circle: "))
my_color=input("Enter the color of the circle: ")

def check_validity(my_radius,my_color):
    if my_radius<=0:
        return False
    else:
        return Circle(my_radius,my_color)
    
circle_obj=check_validity(my_radius,my_color)
if isinstance(circle_obj,bool):
    print("Invalid Radius")
else:
    print("Area is: %.2f" % circle_obj.calculate_area())
    print("Perimeter is: %.2f" % circle_obj.calculate_perimeter())

Leave a Comment

Your email address will not be published. Required fields are marked *