Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

public class Turtle { private double x, y; private double angle; public Turtle(d

ID: 3546014 • Letter: P

Question

public class Turtle {

private double x, y;

private double angle;

public Turtle(double x0, double y0, double a0) {

x = x0;

y = y0;

angle = a0;

}

public void turnLeft(double delta) {

angle += delta;

}

public void goForward(double step) {

double oldx = x;

double oldy = y;

x += step * Math.cos(Math.toRadians(angle));

y += step * Math.sin(Math.toRadians(angle));

StdDraw.line(oldx, oldy, x, y);

}

public static void main(String[] args)

{

int N = Integer.parseInt(args[0]);

double angle = 360.0/N

double step = Math.sin(Math.toRadians(angle/2));

Turtle turtle = new turtle(.5, .0, angle/2);

for (int i = 0; i < N; i++)

{

turtle.goforward(step);

turtle.turnleft(angle);

}

}

}

Modify the test client in the above program to produce stars with N points for odd N?

Explanation / Answer

public class Turtle {

private double x, y;

private double angle;

public Turtle(double x0, double y0, double a0) {

x = x0;

y = y0;

angle = a0;

}

public void turnLeft(double delta) {

angle += delta;

}

public void goForward(double step) {

double oldx = x;

double oldy = y;

x += step * Math.cos(Math.toRadians(angle));

y += step * Math.sin(Math.toRadians(angle));

StdDraw.line(oldx, oldy, x, y);

}

public static void main(String[] args)

{

int N = Integer.parseInt(args[0]);

double angle = 360.0/N

double step = Math.sin(Math.toRadians(angle/2));

Turtle turtle = new turtle(.5, .0, angle/2);

for (int i = 1; i < N; i+2)

{

turtle.goforward(step);

turtle.turnleft(angle);

}

}

}