Recap - Shape Calculator
Part of the Object Oriented Programming section of Coddy's GDScript journey. Lesson 25 of 65.
Challenge
MediumBuild a shape calculator that handles real shapes, a shape-like object from outside the hierarchy, and objects it cannot measure.
Shapeis abstract, with the abstract methodsarea() -> float,perimeter() -> floatandshape_name() -> String. Its normal methoddescribe()returnsNAME: area A, perimeter Pwith two decimals.Circle(radius),Rectangle(width,height) andTriangle(sidesa,b,c) extendShape. Their names areCircle,RectangleandTriangle. A circle's area isPI * r * rand its perimeter2 * PI * r. For the triangle, use Heron's formula: withs = (a + b + c) / 2, the area issqrt(s * (s - a) * (s - b) * (s - c)).CustomShapedoes NOT extendShape. It has acustom_nameand asize;area()issize * size,perimeter()is4 * size, anddescribe()uses the same format asShape, with its custom name.ShapeCalculatorhas two static functions.process(thing)returns[Shape]plusdescribe()for aShape,[Duck]plusdescribe()for any other object that has adescribe()method, andCannot measure this objectotherwise.total_area(things)adds uparea()of every object that has anarea()method.
The supplied code reads how many objects follow, then one per line: circle R, rectangle W H, triangle A B C, custom NAME SIZE or unknown (a plain RefCounted object). It prints process() for each one and the total area. For circle 5, custom Hexa 3 and unknown it prints:
[Shape] Circle: area 78.54, perimeter 31.42
[Duck] Hexa: area 9.00, perimeter 12.00
Cannot measure this object
Total area: 87.54Your code goes in shape.gd, circle.gd, rectangle.gd, triangle.gd, custom_shape.gd and shape_calculator.gd. main.gd holds the supplied input/output code and cannot be edited.
Try it yourself
extends Node
func _ready():
# Supplied input/output code: keep it as it is
var count := int(OS.read_string_from_stdin())
var things := []
for i in count:
var p := OS.read_string_from_stdin().split(" ")
if p[0] == "circle":
things.append(Circle.new(float(p[1])))
elif p[0] == "rectangle":
things.append(Rectangle.new(float(p[1]), float(p[2])))
elif p[0] == "triangle":
things.append(Triangle.new(float(p[1]), float(p[2]), float(p[3])))
elif p[0] == "custom":
things.append(CustomShape.new(p[1], float(p[2])))
else:
things.append(RefCounted.new())
for thing in things:
print(ShapeCalculator.process(thing))
print("Total area: %.2f" % ShapeCalculator.total_area(things))
All lessons in Object Oriented Programming
1Fundamentals of OOP
External FilesIntroduction to OOPClasses vs ObjectsThe self KeywordMethodsPropertiesConstructor (_init)Recap - Simple Calculator4Polymorphism
Method Overriding RevisitedDuck TypingAbstract Classes and MethodsInterface DesignRecap - Shape Calculator2Class Properties
Instance vs Static VariablesStatic FunctionsProperty SettersProperty GettersPrivate by ConventionRecap - Bank Account Manager5Encapsulation
Public and Private MembersValidation with SettersInformation HidingRead-Only PropertiesRecap - Student Records System8Advanced OOP Concepts
Composition vs InheritanceInner ClassesNodes as ComponentsRefCounted vs NodeEnums in Classes11Project: Library Management
Project OverviewBook and User Classes3Inheritance
Basic InheritanceThe super KeywordMethod OverridingInheritance ChainsType Checks with isRecap - Employee Hierarchy6Special Methods
Virtual Methods IntroductionThe _to_string MethodCustom IteratorsRecap - Custom List9Design Patterns Part 1
Intro to Design PatternsSingleton PatternFactory PatternObserver PatternStrategy PatternPractice on your own: Online GDScript compiler