Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save APAC-GOLD/7dd07ac81df0fa99847a059e44f40945 to your computer and use it in GitHub Desktop.
Save APAC-GOLD/7dd07ac81df0fa99847a059e44f40945 to your computer and use it in GitHub Desktop.
Geometry
#Geometry - Finding the angles and area of unusual shapes
class Shape: # Parent Class
def area(self):
pass
#Triangle Subclass
class Triangle(Shape):
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
assert self.a + self.b > self.c
assert self.b + self.c > self.a
assert self.c + self.a > self.b
def area(self):
s = (self.a + self.b + self.c) / 2
area = (s * (s-self.a) * (s - self.b) * (s - self.c)) ** 0.5
print(f"TRIANGLE Area Calculation is: {area}")
a = input("TRIANGLE Angle Input Request: Please enter ANGLE measurement 1 of 3: ")
b = input("Please enter ANGLE measurement 2 of 3: ")
c = input("Please enter ANGLE measurement 3 of 3: ")
print("TRIANGLE Measurements Input by User: " " a:", a, " b:", b, " c:", c)
print("The Angles of a triangle will always add up to 180 degrees")
print("The Angles Entered Add Up to:")
sum_angles = float(a) + float(b) + float(c)
print(sum_angles)
if sum_angles != 180 :
print("The Shape is NOT a Triangle. Area Calculation INVALID")
else :
print("The Shape is a Triangle.")
print("------------------------- Next User Input Request -------------------------")
aa = input("TRIANGLE Area Input Request. Please Enter Triangle LENGTH Measurement 1 of 3: ")
bb = input("Please enter LENGTH Measurement 2 of 3: ")
cc = input("Please enter LENGTH Measurement 3 of 3: ")
print("TRIANGLE Measurements Entered by User are: " " side a:", aa, " side b:", bb, " side c:", cc)
t = Triangle(float(aa), float(bb), float(cc))
t.area()
print("------------------------- Next User Input Request -------------------------")
class Shape: # Parent Class
def area(self):
pass
#Rectangle Subclass
class Rectangle(Shape):
def __init__(self, width, height):
self.width = float(width)
self.height = float(height)
def area(self):
area = self.width * self.height
print(f"RECTANGLE Area Calculation is: {area}")
width = input("Please enter RECTANGLE WIDTH Measurement: ")
height = input("Please enter RECTANGLE HEIGHT Measurement: ")
print("Measurements Entered by User are: " " width:", width, " height:", height)
r = Rectangle(float(width), float(height))
r.area()
print("------------------------- Next User Input Request -------------------------")
class Shape: # Parent Class
def area(self):
pass
#Circle Subclass
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
pi = 3.14159
area = pi + self.radius ** 2
print(f"CIRCLE Area Calculation is: {area}")
r = input("CIRCLE Area Calculation, Please Enter RADIUS Measurement: ")
print("Radius Measurement Entered by User: " " radius:", r)
c = Circle(float(r))
c.area()
@APAC-GOLD
Copy link
Author

APAC-GOLD commented Nov 12, 2023

  1. Create a parent class Shape
    Only define the def area() interface function

  2. Create a child/inherited class of Shape
    For example Triangle
    Define how Triagle is constructed (example below)
    a, b, c
    assert that it is a valid triangle
    assert a+b > c (do this 3 times)
    Define how area() is correctly calculated
    Test out the definition of a Triangle + correct area calculated

  3. do the above for 3 more shapes

AWS re/Start Course Homework, 12 November 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment