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.
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.
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.
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.
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.
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 :-
Please refer to the Screenshot of the code given below to understand indentation of Python code.
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 :-
Please refer to the Screenshot of the code given below to understand indentation of the Python code.
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 refer to the Screenshot of the code given below to understand indentation of the Python code
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 :-
Please refer to the Screenshot of the code given below to understand indentation of the Python Code.
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 :-
Please refer to the Screenshot of the code given below to understand indentation of Python code.
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 :-
Please refer to the Screenshot of the code given below to understand indentation of the Python code.
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 refer to the Screenshot of the code given below to understand indentation of the Python code
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 :-
Please refer to the Screenshot of the code given below to understand indentation of the Python Code.
Write a Python class, Circle, constructed by a radius and two methods which will compute 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 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 (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 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 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 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: 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 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 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 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...