Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python 3 Create and use classes Inherit from a previously defined class. Turtle to use the turtle module to draw on the screen a little.

Python 3
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
Create and use classes Inherit from a previously defined class. Turtle to use the turtle module to draw on the screen a little. Take a few minutes to familiar with turtle. >>>import turtle >> turtle.forward (100) >>> turtle.right (90) >>> turtle.forward (50) 25 Once you do the first turtle.forward0, you'll notice it opens a new window that has the line you've just drawn. Experiment with turning left and right, and drawing lines. Keep in mind that when we are drawing a box, or a circle, the direction of the turtle may not be at the angle we intend. Some of the skills we're going to need is are turtle.right( angle ) turtle.forward( distance) . .turtle.circle( radius) In some of the classes we want to create a class that draws a filled shape. In which case we need to use turtle.fillcolor(color), turtle.begin fill), and turtle.end fill) >>> turtle.fillcolor ("red") >>>turtle.begin fill) >>>turtle.forward (100) >>>turtle.right (120) >>turtle.forward (100) >>> turtle.right (120) turtle. torvard(100) turtle.end t310 Creating our super class Create a import turtle class Point (object) e a solution for the project and enter tollowing def init (selt, xy eolor) self.x self.color color def draw(self) turtle.penup ) turtle.goto (self.x, self.y) turtle. pendown turtle.color (self.color) turtle.setheading (O) self.draw action) def draw_action (self) turtle.dot ) Try running this program and create an instance of the point in the shell. p Point (-100, 100, "blue") >>> p.draw() The Box Subclass I a new class called Box that inherits from Point your solution create Now create an_init_method that has parameters; self, x1, y1, width, height, color. Since we are inherited from point, we can have the super class setup our our instance by calling super )._ init_ (x1, yl, color) After that in the init you set you can access the attributes for the class 110, 50, 40, "red") shel. Make sunt to set the width and height for the self instance. Test your new b.x Zoox 11o >>>b.width 50 >>b.height 40 >b.color red' You'll see we didn't we inherited from haye to repeat the actions of setting the atfibutes for x. y and color since to from Point and called its init. Now create a draw _action method for how to draw the box. def draw action(aelf) Draws a box ** pommber to draw the box, the turtle will already be heading to the right. So you should move urn right by 90 degrees again, draw a line for the width, turn right again and draw a line for the Rem width for the instance, turn right by 90 degrees, then move forward by the height, length of the height. Run the module and test creating a box object and calling the draw method. >b Box (-100, 100, 50, 20, "blue") >>>b.draw () 9> We didn't create a draw0 method for our object. When we call draw) it can't find draw in our box class, so it checks for draw in our super class (point). It calls draw in the super class which draws our specific draw action method in Box. Again, the super class did some of the work setting our turtle object up to draw consistently and all we had to do in draw_action was to draw the box. Create BoxFilled class. Create method that takes y1 method. Remember we are inheriting from Box this time so pass it the to be instantiated (we dont have to pass self to the super init) class in the solution called BoxFilled that inberits from Box. Then create an init x7, y1, width, height color and fillolor as parameters. Call supert)Init (0 Reme class in the shell. set the the attribute for fill color in the init. Test your (1, 2, 3, 4, "blue", "red") 2 b.width >>> .height b.color blue >>>b.fillcolor red Now create a draw_action method for the BoxFilled class. We already know how to draw a box and have that code in our Box class. So instead of repeating that code, we'll call our box class as shown below Box. draw action(self) Test creating a filled box in the shell BoxFilled(1, 2, 100, 200, "red", "Blue" >>b.draw() That should have drawn the box given at x 1, y 2, width 100 outline. However, it didn't draw the fill color in the box. Before calling the Box. draw need to set the fill color for turtle, then begin fill. After calling Box.dr call end fill() for turtle. , and height 200 with a red _action we aw_action you just need to Test your code in the shell. Creating a Circle class Create a Circle class that inherits from the Point class. The init parameters willbe x1 and color. Make sure you call the init for the super class. You'll have y1, radius to save the radius. In the draw_action called turtle.circle) and pass it the radius to draw a cirdie. Creating a CircleFilled class Create a CircleFilled class radius, color and fill ss that inherits from the Circle class. The init parameters will be x1.y1. color. Use the same tactics we learned from earlier to only save the new fill color in the instance and test your results. in the draw action, you should al he Circle draw, action method lIke we did with BoxFilled, but call fillcolor, begin fill first and then end fill afterwards. Turn in your solution on Canvas

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

Database Processing

Authors: David J. Auer David M. Kroenke

13th Edition

B01366W6DS, 978-0133058352

Students also viewed these Databases questions

Question

Name at least three common ways that contractors use BIM.

Answered: 1 week ago

Question

How do Excel Pivot Tables handle data from non OLAP databases?

Answered: 1 week ago