Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a Python Program: Defines two classes, Point() and Triangle(). An object for the second class is created by passing named arguments, point_1, point_2 and

Write a Python Program:

Defines two classes, Point() and Triangle().

An object for the second class is created by passing named arguments, point_1, point_2 and point_3, to its constructor.

Such an object can be modified by changing one point, two or three points thanks to the method change_point_or_points().

At any stage, the object maintains correct values for perimeter and area.

Below is a half cooked solution which needs to be completed

# Defines two classes, Point() and Triangle(). # An object for the second class is created by passing named arguments, # point_1, point_2 and point_3, to its constructor. # Such an object can be modified by changing one point, two or three points # thanks to the method change_point_or_points(). # At any stage, the object maintains correct values # for perimeter and area.

from math import sqrt

class PointError(Exception): def __init__(self, message): self.message = message

class Point(): def __init__(self, x = None, y = None): if x is None and y is None: self.x = 0 self.y = 0 elif x is None or y is None: raise PointError('Need two coordinates, point not created.') else: self.x = x self.y = y def collinear(self, p2, p3): if (p2.y - self.y) * (p3.x - p2.x) ==\ (p3.y - p2.y) * (p2.x - self.x): return True return False

class TriangleError(Exception): def __init__(self, message): self.message = message

class Triangle: def __init__(self, *, point_1, point_2, point_3): if point_1.collinear(point_2, point_3): self.error_message('Initialisation') else: self._initialise(point_1, point_2, point_3) def change_point_or_points(self, *, point_1 = None,point_2 = None, point_3 = None): if not self._change_point_or_points(point_1, point_2, point_3): print('Could not perform this change.') return

def _initialise(self, p1, p2, p3): self.p1 = p1 self.p2 = p2 self.p3 = p3 length_12 = self._side_length(p1, p2) length_13 = self._side_length(p1, p3) length_23 = self._side_length(p2, p3) self.perimeter = length_12 + length_13 + length_23 half_perimeter = self.perimeter / 2 self.area = sqrt(half_perimeter * (half_perimeter - length_12) * (half_perimeter - length_13) * (half_perimeter - length_23))

def _change_point_or_points(self, p1, p2, p3): if not p1: p1 = self.p1 if not p2: p2 = self.p2 if not p3: p3 = self.p3 if p1.collinear(p2, p3): self.error_message('Update') return False else: self._initialise(p1, p2, p3) return True

def _side_length(self, p, q): return sqrt((q.x - p.x) ** 2 + (q.y - p.y) ** 2)

# Possibly define other methods

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

Marketing Database Analytics

Authors: Andrew D. Banasiewicz

1st Edition

0415657881, 978-0415657884

More Books

Students also viewed these Databases questions

Question

e. What do you know about your ethnic background?

Answered: 1 week ago

Question

Know what customers expect from the firm when they complain.

Answered: 1 week ago

Question

Understand why customers complain.

Answered: 1 week ago