Assignment 12 - ldkvd/CS101L GitHub Wiki
Welcome to the CS101L Wiki!
Turtle
Create a solution for the project and enter the following code to create the superclass. In your solution, create a new class called Box that inherits from Point. Now, create a draw_action method for how to draw the box. Create another class in the solution called BoxFilled that inherits from Box. Now, create a draw_action method for the BoxFilledclass. Create a Circle class that inherits from the Point class. Create a CircleFilled class that inherits from the Circle class. In the draw action, you should call the Circle draw_action method like we did with BoxFilled, but call fillcolor, begin_fill first and then end_fill afterwards.
Answers:
The image above is code 1/4.
The image above is code 2/4.
The image above is code 3/4.
The image above is code 4/4.
The image above is the output.
In this program, the module Turtle was imported. A parent class called Point was created. The init method had parameters self, x, y, and color. Instance attributues were set. Then, a draw method and draw_action method were created. Next, a class called Box that inherits from Point was created. The init method had parameters self, x1, y1, width, height, and color. Used a superclass and set instance attributes. A draw_action method that draws the box was created. After that, a class called BoxFilled was created which had parameters self, x1, y1, width, height, color, and fill color. Set instance attributes and used a superclass. A draw_action method that fills in the box was created. Aclass called Circle that inherits from Point was created. The init method had parameters self, x1, y1, radius, and color. Set instance attributes and used a superclass. A draw_action method that draws the circle was added. Finally, aclass called CircleFilled was created. The init method that has parameters self, x1, y1, radius, color, and fill color. Set instance attributes and used a superclass. A draw_action method that fills in the circle was created. The object (such as Point, Box, BoxFilled, Circle, CircleFilled) and the draw method was called to run. By using inheritance, we can create classes that are built upon existing classes which allows for the reuse of cose.