Drawing A Regular Polygon using java with comments Write the
Drawing A Regular Polygon, using java, with comments,
Write the code to have the turtle draw a regular polygon with the specified number of sides and specified side length.
Solution
import turtlegraphics.*;
import SmartTurtle;
public class DrawPolygon
{
void drawPolygon(int n, int s)
throws TurtleException
{
for (int side = 1; side <= n; side++)
{
this.move(s);
this.turnRight(360 / n);
}
}
public static void main(String[] args)
throws TurtleException
{
SmartTurtle myTurtle = new SmartTurtle();
int num_sides, size;
Scanner scn = new Scanner(System.in);
System.out.println(\"Enter the number of sides\");
num_sides = scn.nextInt();
System.out.println(\"Enter the size\");
size = scn.nextInt();
int s = 360 % num_sides ;
if (num_sides == 0)
{
myTurtle.drawPolygon(num_sides, size);
}
else
System.out.println(\"The number of sides must divide into 360 evenly\");
}
}
