In this lab, you will write a program to draw regular polygons. But this time, you will incorporate custom blocks using abstraction to write a more efficient program.
-
Write a script (or find one you've already written) to draw a square.
-
Write a script (or find one you've already written) to draw an equilateral triangle.
-
Write a script (or find one you've already written) to draw a regular pentagon.
-
Write a script (or find one you've already written) to draw a regular octagon.
-
Look over the four scripts from above. Do you notice sections that are very similar? What sections might be able to be abstracted into a separate block?
-
Take your script from above that draws a square and turn it into a custom block.
-
Modify your custom block to use a variable for the number of sides instead of the number 4. Set that variable's value to be 4 so that you still draw a square.
-
Now, see if you can modify your block so that it will work correctly for any number of sides greater than 2. Look closely at the angles in your four scripts section 1 and see if you can spot a pattern.
-
Use your custom block in a script that asks the user for a number and then draws a regular polygon with that many sides. If the number given is less than 3, give an error message and do not draw anything.
-
Save your project as Lab3.1.
Modify your custom block and script from the previous section so that the user can specify both the number of sides and the length of each side. Be sure to utilize generalization and detail removal to make your script and block as concise as possible.
Criteria | Points |
---|---|
2.1 Custom block draws square | 0.25 |
2.2 Use variable for number of sides | 0.5 |
2.3 Works for all sides > 2 | 0.5 |
2.4a script asks for sides, calls block | 0.5 |
2.4b script handles input <= 2 | 0.5 |
Bonus: script and block handle size of sides | 0.5 |
script clear and concise | 0.25 |
Totals | 2.5 |