Question

In: Computer Science

Exercise 1. Rectangle, Circle and Square Write three Python classes named Rectangle constructed by a length...

Exercise 1. Rectangle, Circle and Square Write three Python classes named Rectangle constructed by a length and width, a Circle constructed by a radius and a Square constructed by a side length. Both classes should have the methods that compute: - The area - The diagonal - The perimeter Use as much abstraction as you can. At the end of the file, use those classes to calculate the perimeter of a circle with radius the half of the diagonal of a rectangle with length 20 and width 10.

Solutions

Expert Solution

CODE:

from math import sqrt
#the circle class
class Circle():
def __init__(self, radius):
self.radius = radius
#the area function
def area(self):
return self.radius**2 *3.14
#the perimeter function
def perimeter(self):
return 2*self.radius *3.14
  
#the rectangle class
class Rectangle():
def __init__(self, length, width):
self.length = length
self.width = width
#the area function
def area(self):
return self.length*self.width
#the perimeter function
def perimeter(self):
return 2*self.length* self.width
#the diagonal function
def diagonal(self):
diagonal = float(sqrt(self.length**2 + self.width**2))
return diagonal
  
#the square class
class Square():
def __init__(self, length):
self.length = length
#the area function
def area(self):
return self.length * self.length
#the perimeter function
def perimeter(self):
return 4*self.length
#the diagonal function
def diagonal(self):
diagonal = 1.414 * self.length

##new rectangle object with required length and width
NewRectangle = Rectangle(20, 10)
#calculating the diagonal of the rectangle
NewRectangleDiagonal = NewRectangle.diagonal()
#instantiating the circle class with half the diagonal as the radius
NewCircle = Circle(NewRectangleDiagonal/2)
#print the perimeter of the circle
print(NewCircle.perimeter())

SCREENSHOT:

OUTPUT:


Related Solutions

Write a Python program that represents various geometric shapes. Each shape like rectangle, square, circle, and...
Write a Python program that represents various geometric shapes. Each shape like rectangle, square, circle, and pentagon, has some common properties, ex. whether it is filled or not, the color of the shape, and number of sides. Some properties like the area of the shapes are determined differently, for example, area of the rectangle is length * widthand area of a circle is ??2. Create the base class and subclasses to represent the shapes. Create a separate test module where...
Write a Python program that represents various geometric shapes. Each shape like rectangle, square, circle, and...
Write a Python program that represents various geometric shapes. Each shape like rectangle, square, circle, and pentagon, has some common properties, ex. whether it is filled or not, the color of the shape, and number of sides. Some properties like the area of the shapes are determined differently, for example, area of the rectangle is length * widthand area of a circle is ??2. Create the base class and subclasses to represent the shapes. Create a separate test module where...
Write a program to calculate the area of four shapes (Rectangle, triangle, circle and square). The...
Write a program to calculate the area of four shapes (Rectangle, triangle, circle and square). The program to present the user with a menu where one of the shapes can be selected. Based on the selection made, the user enters the proper input, the program validates the input (i.e all entries must be greater than zero). Once the input is entered and validated, the intended area is calculated and the entered information along with the area are displayed. Area of...
In this assignment, students should create five Java classes called Point, Circle, Square, Rectangle, and TestAll,...
In this assignment, students should create five Java classes called Point, Circle, Square, Rectangle, and TestAll, as well as a Java interface called FigureGeometry. The TestAll class should implement the main method which tests all of the other files created in the assignment. After the assignment has been completed, all six files should be submitted for grading into the Dropbox for Assignment 3, which can be found in the Dropbox menu on the course website. Students should note that only...
Following the example of Circle class, design a class named Rectangle to represent a rectangle. The...
Following the example of Circle class, design a class named Rectangle to represent a rectangle. The class contains: Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with specified width and height A method name getWidth() return the value of width A method named getHeight() returns value of...
Following the example of Circle class, design a class named Rectangle to represent a rectangle. The...
Following the example of Circle class, design a class named Rectangle to represent a rectangle. The class contains: Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with specified width and height A method name getWidth() return the value of width A method named getHeight() returns value of...
Write an inheritance hierarchy for classes Quadrilateral, Trapezoid, Parallelogram, Rectangle and Square. Use Quadrilateral as the...
Write an inheritance hierarchy for classes Quadrilateral, Trapezoid, Parallelogram, Rectangle and Square. Use Quadrilateral as the superclass of the hierarchy. Create and use a Point class to represent the points in each shape. Make the hierarchy as deep ( i.e., as many levels ) as possible. Specify the instance variables and methods for each class. The private instances variables of Quadrilateral should be the x-y coordinate pairs for the four endpoints of the Quadrilateral. Write a program that instantiates objects...
Circle Object Python program Compute the area and perimeter of a circle using Python classes. take...
Circle Object Python program Compute the area and perimeter of a circle using Python classes. take the radius from the user and find the area and perimeter of a circle please use comments to explain so i better understand
Base class: Polygon Derived classes: Rectangle, Triangle Make Square a derived class of Rectangle. There can...
Base class: Polygon Derived classes: Rectangle, Triangle Make Square a derived class of Rectangle. There can be several ways to represent the shapes. For example, a shape can be represented by an array of side lengths counted in the clock-wise order. For example, {2, 4, 2, 4} for a rectangle of width 2 and height 4, and {4, 4, 4, 4} for a square. You need to design your representation. Each polygon should have a function area() that returns its...
PYTHON! Exercise 3 - Total Line length Write a python function that will return the total...
PYTHON! Exercise 3 - Total Line length Write a python function that will return the total length of line that passes through any number of provided points ( (x,y) ). The points should be passed as individual tuples or lists. The function should also have a parameter (True or False) to indicate whether the line should start from the origin, and that parameter should default to False. If True, the returned value should include the distance from the origin to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT