Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Inheritance - Polymorphism One advantage of using subclasses is the ability to use polymorphism. The idea behind polymorphism is that several different types of objects

Inheritance - Polymorphism

One advantage of using subclasses is the ability to use polymorphism.

The idea behind polymorphism is that several different types of objects can have the same methods, and be treated in the same way.

For example, have a look at the code weve included for this problem. Weve defined Shape as an abstract base class. It doesnt provide any functionality by itself, but it does supply an interface (in the form of .area() and .vertices() methods) which are meaningful for any type of 2D Shape.

The total_area function makes use of this to calculate the area of any kind of Shape. Weve provided an example of this with two Square instances.

We want you to write RightAngledTriangle and Rectangleclasses which implement this interface.

The constructor for RightAngledTriangle accepts one argument, vertices, being a list of the points of the triangle relative to its origin. The first vertex will be at the right angle.

The constructor for Rectangle accepts width and height.

class Shape(): """ A representation of a shape.

""" def __init__(self, origin=(0, 0)): """Construct a shape object Parameters: origin(tuple): origin of the shape """

self.origin = origin

def area(self): """ (int) Return the area of the shape.

""" raise NotImplementedError()

def vertices(self): """ Return the vertices of the shape.

Return: list>: The vertices of the shape, as a list of tuples representing two dimensional points. This list may be returned in any order. """ raise NotImplementedError()

class Square(Shape): """A Square object""" def __init__(self, side_length, origin=(0, 0)): """ Construct a square object Parameters: side_length (int): Length of the sides of the square origin (tuple): Coordinate of topleft corner of square """ super().__init__(origin=origin)

self.side_length = side_length

def area(self): """ (int) Return the area of the shape. """ return self.side_length * self.side_length

def vertices(self): """ Return the vertices of the shape.

Return: list>: The vertices of the shape, as a list of tuples representing two dimensional points. This list may be returned in any order. """ x, y = self.origin

return [ (x, y), (x, y + self.side_length), (x + self.side_length, y + self.side_length), (x + self.side_length, y), ]

def total_area(shapes): """ Return the total area of the given list of shapes.

Parameters: shapes (list): The list of shapes to sum the area for.

Return: int: The total area of the list of shapes, being the sum of the area of each individual shape.

""" area = 0.

for shape in shapes: area += shape.area()

return area

def outer_bounds(shapes): """ Return the outer bounds of the given list of shapes.

Parameters: shapes (list): The list of shapes to return the outer bounds for.

Return: tuple, tuple>:

The first element of the tuple is the top-left corner of a rectangle which could enclose every shape in the given list. The second element of the tuple is the bottom-right corner of that same rectangle.

The top-left corner of the rectangle will be, at minimum, (0, 0).

""" vertices = []

for shape in shapes: for vertex in shape.vertices(): vertices.append(vertex)

top_left_x = 0 top_left_y = 0 bottom_right_x = 0 bottom_right_y = 0

for x, y in vertices: if x < top_left_x: top_left_x = x elif x > bottom_right_x: bottom_right_x = x

if y < top_left_y: top_left_y = y elif y > bottom_right_y: bottom_right_y = y

return (top_left_x, top_left_y), (bottom_right_x, bottom_right_y)

# example usage # note that total_area doesn't know nor care that we used instances of Square shapes = [Square(2), Square(4, origin=(2, 2))] area = total_area(shapes)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Main Memory Database Systems

Authors: Frans Faerber, Alfons Kemper, Per-Åke Alfons

1st Edition

1680833243, 978-1680833249

More Books

Students also viewed these Databases questions

Question

What is DDL?

Answered: 1 week ago