Question

Write a Python class, Circle, constructed by a radius and two methods which will compute the...

  1. Write a Python class, Circle, constructed by a radius and two methods which will compute the area and the perimeter of a circle. Include the constructor and other required methods to set and get class attributes. Create instances of Circle and test all the associated methods.

  2. Write a Python class, Rectangle, constructed by length and width and a method which will compute the area of a rectangle. Include the constructor and other required methods to set and get class attributes. Also, include a methods isSquare(), which returns a Boolean value indicating if the shape is a square. Create instances of Rectangle and test all the associated methods.

  3. Write a Python class, Printer, constructed by a string to be printed. The class has two method; (a) setString() which accepts a string from the user, and (b) printString() which prints the string in upper case or lower case, as the user chooses. Create instances of the class and test the associated methods.

  4. From the previous assignment in Lab 2, Assignment 1, and Question 1, where a Laptop was modeled using UML class diagram, create a Python class with respective attributes and methods. Create instances of the classes designed and test all the associated methods.

  5. Write a Python class, Person, constructed by name, surname, birthdate, address, telephone number, email. The class has methods to calculate the age of the person using his birthdate and also to display the details of a person. Create instances of the class and test the associated methods.

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

Circle.py :- implementation of Circle class in Python.

import math


class Circle:

    def __init__(self, radius):
        """
        initializes radius of circle.
        :param radius: radius of circle
        """
        self.radius = radius

    # get method for class attribute

    def get_radius(self):
        return self.radius

    # set method for class attribute
    def set_radius(self, radius):
        self.radius = radius

    def get_area(self):
        """
        calculate and return area of circle.
        :return: area of circle
        """
        area = math.pi * self.radius ** 2
        return round(area, 2)  # round area up to two decimal point and return

    def get_perimeter(self):
        """
        calculate and return perimeter of circle
        :return: perimeter of circle.
        """
        perimeter = 2 * math.pi * self.radius
        return round(perimeter, 2)  # round perimeter up to two decimal point and return


# Program entry point
if __name__ == '__main__':
    circle = Circle(5)
    print("Testing Circle methods (radius = 5)\n")
    print("get_area(): ", circle.get_area())
    print("get_perimeter(): ", circle.get_perimeter())

Sample Output :-

Testing Circle methods (radius 5) get_area(): 78.54 get_perimeter(): 31.42|| Process finished with exit code 0

Please refer to the Screenshot of the code given below to understand indentation of Python code.

1 import math 3 4 class Circle: 5 definit__(self, radius): 6 7 8 initializes radius of circle. :param radius: radius of circlA return round(area, 2) # round area up to two decimal point and return 28 29 def get_perimeter(self): 30 31 calculate and re

Rectangle.py :- implementation of Rectangle class in Python

class Rectangle:

    def __init__(self, length, width):
        """
        initializes length and width of Rectangle.
        :param length: length of rectangle
        :param width: width of rectangle
        """
        self.length = length
        self.width = width

    # set methods for class attributes

    def set_length(self, length):
        self.length = length

    def set_width(self, width):
        self.width = width

    # get methods for class attributes.

    def get_length(self):
        return self.length

    def get_width(self):
        return self.width

    def get_area(self):
        """
        calculate and return area of Rectangle.
        using formula area = length * width
        :return: area of rectangle.
        """
        area = self.length * self.width

        return area

    def is_square(self):
        """
        check if Rectangle is Square or not
        :return: True if Rectangle is Square. otherwise False.
        """
        if self.length == self.width:
            return True
        else:
            return False


# program entry point
if __name__ == '__main__':
    """
    creating instance of rectangle
    and testing method of Rectangle class.
    """
    rect = Rectangle(4, 4)
    print("Rectangle methods(length=4,height=4)")
    print("get_area(): ", rect.get_area())
    print("is_square(): ", rect.is_square())

Sample Output :-

Rectangle methods (length=4, height=4) get_area(): 16 is_square(): True Process finished with exit code 0

Please refer to the Screenshot of the code given below to understand indentation of the Python code.

1 class Rectangle: 3 def __init__(self, length, width) : 5 6 initializes length and width of Rectangle. :param length: lengthdef get_area(self): 28 29 30 31 calculate and return area of Rectangle. using formula area = length * width :return: area of48 49 # program entry point Pif -_name__ == __main__: 50 51 creating instance of rectangle and testing method of Rectangle

Printer.py :- implementation of Printer class in Python

class Printer:

    def __init__(self, string):
        self.string = string

    # method to set string
    def setString(self):
        """
        set string to be print
        :return: nothing
        """
        # ask string to be printed from user
        string = input("Please enter string to be printed: ")
        self.string = string

    # method to print string in uppercase of lowercase
    def printString(self):
        """
        print string in uppercase of lowercase as selected by user
        :return: nothing
        """
        # ask for case (uppercase or lowercase)
        print("1. for lowercase")
        print("2. for uppercase")
        option = input("Enter your choice:")

        if option == '1':
            print(self.string.lower())
        elif option == '2':
            print(self.string.upper())
        else:
            print("Invalid Input")


# Program entry point
if __name__ == '__main__':
    """
    creating instance of Printer class and calling methods
    """
    printer = Printer("Default String")
    printer.setString()
    printer.printString()

Sample Output :-

Please enter string to be printed: Python is a Programming Language 1. for lowercase 2. for uppercase Enter your choice: 2 PY

Please refer to the Screenshot of the code given below to understand indentation of the Python code

class Printer: 2 3 4 def _init__(self, string): self.string = string 5 6 # method to set string def setString(self): set stri28 29 30 print(self.string. lower() elif option == 2: print(self.string.upper() else: print(Invalid Input) 31 32 33 34 #

Person.py :- implemetation of Person class in Python

from datetime import date


class Person:

    def __init__(self, name, surname, birthday, address, telephone, email):
        self.name = name
        self.surname = surname
        self.birthday = birthday
        self.address = address
        self.telephone = telephone
        self.email = email

    def calculate_age(self):
        today = date.today()
        age = today.year - self.birthday.year - ((today.month, today.day) < (self.birthday.month, self.birthday.month))
        return age

    def display(self):
        """
        display details of Person
        :return: None.
        """
        print("Name: ", self.name)
        print("Surname: ", self.surname)
        print("Age: ", self.calculate_age())
        print("Address: ", self.address)
        print("Telephone Number: ", self.telephone)
        print("Email: ", self.email)


# Program entry point

if __name__ == '__main__':
    """
    Creating instance of Person class and testing it's methods
    """
    person = Person("Anonymous", "Surname", date(1998, 11, 12), "This is the Person's address", "123456",
                    "[email protected]")
    print("calculate_age()", person.calculate_age())
    print("Person information")
    person.display()

Sample Output :-

calculate_age() 21 Person information Name: Anonymous Surname: Surname Age: 21 Address: This is the persons address Telephon

Please refer to the Screenshot of the code given below to understand indentation of the Python Code.

1 from datetime import date class Person: 6 7 8 9 definit__(self, name, surname, birthday, address, telephone, email): self.nprint(Telephone Number: , self.telephone) print(Email: , self.email) # Program entry point Gif __name__ == __main__: 28


answered by: ANURANJAN SARSAM
Add a comment
Answer #2

Circle.py :- implementation of Circle class in Python.

import math


class Circle:

    def __init__(self, radius):
        """
        initializes radius of circle.
        :param radius: radius of circle
        """
        self.radius = radius

    # get method for class attribute

    def get_radius(self):
        return self.radius

    # set method for class attribute
    def set_radius(self, radius):
        self.radius = radius

    def get_area(self):
        """
        calculate and return area of circle.
        :return: area of circle
        """
        area = math.pi * self.radius ** 2
        return round(area, 2)  # round area up to two decimal point and return

    def get_perimeter(self):
        """
        calculate and return perimeter of circle
        :return: perimeter of circle.
        """
        perimeter = 2 * math.pi * self.radius
        return round(perimeter, 2)  # round perimeter up to two decimal point and return


# Program entry point
if __name__ == '__main__':
    circle = Circle(5)
    print("Testing Circle methods (radius = 5)\n")
    print("get_area(): ", circle.get_area())
    print("get_perimeter(): ", circle.get_perimeter())

Sample Output :-

Testing Circle methods (radius 5) get_area(): 78.54 get_perimeter(): 31.42|| Process finished with exit code 0

Please refer to the Screenshot of the code given below to understand indentation of Python code.

1 import math 3 4 class Circle: 5 definit__(self, radius): 6 7 8 initializes radius of circle. :param radius: radius of circlA return round(area, 2) # round area up to two decimal point and return 28 29 def get_perimeter(self): 30 31 calculate and re

Rectangle.py :- implementation of Rectangle class in Python

class Rectangle:

    def __init__(self, length, width):
        """
        initializes length and width of Rectangle.
        :param length: length of rectangle
        :param width: width of rectangle
        """
        self.length = length
        self.width = width

    # set methods for class attributes

    def set_length(self, length):
        self.length = length

    def set_width(self, width):
        self.width = width

    # get methods for class attributes.

    def get_length(self):
        return self.length

    def get_width(self):
        return self.width

    def get_area(self):
        """
        calculate and return area of Rectangle.
        using formula area = length * width
        :return: area of rectangle.
        """
        area = self.length * self.width

        return area

    def is_square(self):
        """
        check if Rectangle is Square or not
        :return: True if Rectangle is Square. otherwise False.
        """
        if self.length == self.width:
            return True
        else:
            return False


# program entry point
if __name__ == '__main__':
    """
    creating instance of rectangle
    and testing method of Rectangle class.
    """
    rect = Rectangle(4, 4)
    print("Rectangle methods(length=4,height=4)")
    print("get_area(): ", rect.get_area())
    print("is_square(): ", rect.is_square())

Sample Output :-

Rectangle methods (length=4, height=4) get_area(): 16 is_square(): True Process finished with exit code 0

Please refer to the Screenshot of the code given below to understand indentation of the Python code.

1 class Rectangle: 3 def __init__(self, length, width) : 5 6 initializes length and width of Rectangle. :param length: lengthdef get_area(self): 28 29 30 31 calculate and return area of Rectangle. using formula area = length * width :return: area of48 49 # program entry point Pif -_name__ == __main__: 50 51 creating instance of rectangle and testing method of Rectangle

Printer.py :- implementation of Printer class in Python

class Printer:

    def __init__(self, string):
        self.string = string

    # method to set string
    def setString(self):
        """
        set string to be print
        :return: nothing
        """
        # ask string to be printed from user
        string = input("Please enter string to be printed: ")
        self.string = string

    # method to print string in uppercase of lowercase
    def printString(self):
        """
        print string in uppercase of lowercase as selected by user
        :return: nothing
        """
        # ask for case (uppercase or lowercase)
        print("1. for lowercase")
        print("2. for uppercase")
        option = input("Enter your choice:")

        if option == '1':
            print(self.string.lower())
        elif option == '2':
            print(self.string.upper())
        else:
            print("Invalid Input")


# Program entry point
if __name__ == '__main__':
    """
    creating instance of Printer class and calling methods
    """
    printer = Printer("Default String")
    printer.setString()
    printer.printString()

Sample Output :-

Please enter string to be printed: Python is a Programming Language 1. for lowercase 2. for uppercase Enter your choice: 2 PY

Please refer to the Screenshot of the code given below to understand indentation of the Python code

class Printer: 2 3 4 def _init__(self, string): self.string = string 5 6 # method to set string def setString(self): set stri28 29 30 print(self.string. lower() elif option == 2: print(self.string.upper() else: print(Invalid Input) 31 32 33 34 #

Person.py :- implemetation of Person class in Python

from datetime import date


class Person:

    def __init__(self, name, surname, birthday, address, telephone, email):
        self.name = name
        self.surname = surname
        self.birthday = birthday
        self.address = address
        self.telephone = telephone
        self.email = email

    def calculate_age(self):
        today = date.today()
        age = today.year - self.birthday.year - ((today.month, today.day) < (self.birthday.month, self.birthday.month))
        return age

    def display(self):
        """
        display details of Person
        :return: None.
        """
        print("Name: ", self.name)
        print("Surname: ", self.surname)
        print("Age: ", self.calculate_age())
        print("Address: ", self.address)
        print("Telephone Number: ", self.telephone)
        print("Email: ", self.email)


# Program entry point

if __name__ == '__main__':
    """
    Creating instance of Person class and testing it's methods
    """
    person = Person("Anonymous", "Surname", date(1998, 11, 12), "This is the Person's address", "123456",
                    "[email protected]")
    print("calculate_age()", person.calculate_age())
    print("Person information")
    person.display()

Sample Output :-

calculate_age() 21 Person information Name: Anonymous Surname: Surname Age: 21 Address: This is the persons address Telephon

Please refer to the Screenshot of the code given below to understand indentation of the Python Code.

1 from datetime import date class Person: 6 7 8 9 definit__(self, name, surname, birthday, address, telephone, email): self.nprint(Telephone Number: , self.telephone) print(Email: , self.email) # Program entry point Gif __name__ == __main__: 28

Add a comment
Know the answer?
Add Answer to:
Write a Python class, Circle, constructed by a radius and two methods which will compute the...
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
  • Write a Python program that creates a class which represents a candidate in an election. The...

    Write a Python program that creates a class which represents a candidate in an election. The class must have the candidates first and last name, as well as the number of votes received as attributes. The class must also have a method that allows the user to set and get these attributes. Create a separate test module where instances of the class are created, and the methods are tested. Create instances of the class to represent 5 candidates in the...

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

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

  • Create a class Circle with one instance variable of type double called radius. Then define an...

    Create a class Circle with one instance variable of type double called radius. Then define an appropriate constructor that takes an initial value for the radius, get and set methods for the radius, and methods getArea and getPerimeter. Create a class RightTriangle with three instance variables of type double called base, height, and hypotenuse. Then define an appropriate constructor that takes initial values for the base and height and calculates the hypotenuse, a single set method which takes new values...

  • Please write in Java Language Write an abstract class Shape with an attribute for the name...

    Please write in Java Language Write an abstract class Shape with an attribute for the name of the shape (you'll use this later to identify a particular shape). Include a constructor to set the name of the shape and an abstract calculateArea method that has no parameters and returns the area of the shape. Optionally, include a constructor that returns the name of the shape and its area (by calling the calculateArea method). Write a Rectangle class that inherits from...

  • PYTHON Write a Python class which has two methods get_String and print_String. get_String accept a string...

    PYTHON Write a Python class which has two methods get_String and print_String. get_String accept a string from the user and print_String print the string in upper case

  • Write an ADT named circle in java containing the following. Include data: radius (double) Include methods:...

    Write an ADT named circle in java containing the following. Include data: radius (double) Include methods: A no-argument constructor A constructor that sets the data to passed values Getters and setters for each piece of data getArea (Math.PI * radius * radius … try using a Math method here) getDiameter (radius * 2) getCircumference (2 * Math.PI * radius) A toString( ) method to display the object data Include Javadoc comments for the class and every method, and generate the...

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

  • Using Python Write the following well-documented (commented) program. Create a class called Shape that has a...

    Using Python Write the following well-documented (commented) program. Create a class called Shape that has a method for printing the area and the perimeter. Create three classes (Square, Rectangle, and Circle) which inherit from it. Square will have one instance variable for the length. Rectangle will have two instance variables for the width and height. Circle will have one instance variable for the radius. The three classes will have methods for computing the area and perimeter of its corresponding shape....

  • Java code for the following inheritance hierarchy figure.. 1. Create class Point, with two private instance...

    Java code for the following inheritance hierarchy figure.. 1. Create class Point, with two private instance variables x and y that represented for the coordinates for a point. Provide constructor for initialising two instance variables. Provide set and get methods for each instance variable, Provide toString method to return formatted string for a point coordinates. 2. Create class Circle, its inheritance from Point. Provide a integer private radius instance variable. Provide constructor to initialise the center coordinates and radius for...

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