Please send the three edited codes and label them separately
Please send the three edited codes and label them separately.
Please answer question and send edited code
Turtle Example
import turtlegraphics.TurtleGraphics;
/*
* Example of using the Turtle Graphics class to create a Sierpinski triangle.
*/
public class TurtleExample {
// cut and paste into TurtleMove.java
public static void sierpinski(int n, int length, TurtleGraphics theTurtle){
if (n == 0) return;
for (int i = 0; i < 3; i++){
sierpinski(n-1,length/2, theTurtle);
theTurtle.forward(length);
theTurtle.right(120);
}
}
// cut and paste into TurtleMove.java
public static void tree(int r, TurtleGraphics theTurtle){
if (r<5){
theTurtle.forward(r);
theTurtle.backward( r);
} else {
theTurtle.forward( r/3);
theTurtle.left(30);
tree(r*2/3, theTurtle);
theTurtle.right(30);
theTurtle.backward( r/3);
theTurtle.forward(r/2);
theTurtle.right(25);
tree(r/2,theTurtle);
theTurtle.left(25);
theTurtle.backward(r/2);
theTurtle.forward( r*5/6);
theTurtle.right(25);
tree(r/2, theTurtle);
theTurtle.left(25);
theTurtle.backward(r*5/6);
}
}
// cut and paste into TurtleMove.java
public static void brocolli(int r, TurtleGraphics theTurtle){
// Leaf node just draw a stem
if (r<5){
theTurtle.forward(r);
theTurtle.backward( r);
} else {
// Make stem
theTurtle.forward( r/3);
// Make left branch
theTurtle.left(30);
brocolli(r*2/3, theTurtle);
// Make right branch
theTurtle.right(60);
brocolli(r*2/3, theTurtle);
// Return to center orientation
theTurtle.left(30);
// Return to base of stem
theTurtle.backward( r/3);
}
}
// cut and paste into TurtleMove.java
public static void koch(float r, float size, TurtleGraphics theTurtle) {
if (r <= 1) {
theTurtle.forward((int) (r*size));
theTurtle.left(60);
theTurtle.forward((int) (r*size));
theTurtle.right(120);
theTurtle.forward((int) (r*size));
theTurtle.left(60);
theTurtle.forward((int) (r*size));
} else {
koch(r-1, size/3, theTurtle);
theTurtle.left(60);
koch(r-1, size/3, theTurtle);
theTurtle.right(120);
koch(r-1, size/3, theTurtle);
theTurtle.left(60);
koch(r-1, size/3, theTurtle);
}
}
public static void main(String[] args) {
// Setup EZ graphics system.
TurtleGraphics aTurtle = new TurtleGraphics(1000,512);
TurtleGraphics bTurtle = new TurtleGraphics(1000,512);
aTurtle.penup();
aTurtle.moveTo(50, 500);
aTurtle.pendown();
koch(3, 300, aTurtle);
/*
aTurtle.setPenSize(2);
aTurtle.penup();
aTurtle.moveTo(500, 500);
aTurtle.left(90);
aTurtle.pendown();
brocolli(400,aTurtle);
*/
bTurtle.setPenSize(1);
bTurtle.penup();
bTurtle.moveTo(500, 500);
bTurtle.left(90);
bTurtle.pendown();
brocolli(50,bTurtle);
/*
aTurtle.setPenSize(1);
aTurtle.penup();
aTurtle.moveTo(500, 500);
aTurtle.left(90);
aTurtle.pendown();
tree(300,aTurtle);
*/
/*
aTurtle.penup();
aTurtle.moveTo(100, 50);
aTurtle.pendown();
sierpinski(5, 500, aTurtle);
*/
}
}
Turtle Graphics
package turtlegraphics;
import java.awt.Color;
/*
* Example of simple turtle graphics library.
*/
public class TurtleGraphics {
private EZImage turtlePicture;
private int oldx, oldy, x, y;
private Color lineColor;
private boolean penDown;
private int penSize;
private static boolean isTurtle=false;
public TurtleGraphics(int screenX, int screenY){
if(isTurtle==false){
EZ.initialize(1000, 512);
EZ.setBackgroundColor(new Color(0, 200, 250));
isTurtle=true;
}
turtlePicture = EZ.addImage(\"turtle.png\", EZ.getWindowWidth() / 2,
EZ.getWindowHeight() / 2);
lineColor = new Color (0,255,200);
penDown = true;
penSize = 5;
}
// 1) should this be public?
public void setPenSize(int size){
penSize = size;
}
// 2) should this be public?
public void pendown(){
penDown = true;
}
// 3) should this be public?
public void penup(){
penDown = false;
}
// 4) should this be public?
public void move(int distance){
oldx = turtlePicture.getXCenter();
oldy = turtlePicture.getYCenter();
turtlePicture.moveForward(distance);
x = turtlePicture.getXCenter();
y = turtlePicture.getYCenter();
if (penDown){
EZ.addLine(oldx,oldy,x,y,lineColor,penSize);
}
EZ.refreshScreen();
}
// 5) should this be public?
public void forward(int distance){
move(distance);
}
public void moveTo(int x, int y){
oldx = turtlePicture.getXCenter();
oldy = turtlePicture.getYCenter();
turtlePicture.translateTo(x,y);
x = turtlePicture.getXCenter();
y = turtlePicture.getYCenter();
if (penDown){
EZ.addLine(oldx,oldy,x,y,lineColor,5);
}
EZ.refreshScreen();
}
// 6) should this be public?
public void backward(int distance){
move(-distance);
}
// 7) should this be public?
public void left(int angle){
turtlePicture.turnLeft(angle);
EZ.refreshScreen();
}
// 8) should this be public?
public void right(int angle){
turtlePicture.turnRight(angle);
EZ.refreshScreen();
}
}
Turtle Move
package turtlegraphics;
public class TurtleMove {
// 1) add public static void sierpinski and remove from TurtleExample.java
// 2) add public static void tree and remove from TurtleExample.java
// 3) add public static void brocolli and remove from TurtleExample.java
// 4) add public static void koch and remove from TurtleExample.java
// 5) add public static void moveTo(int x, int y, int lrot, TurtleGraphics theTurtle)
}
Problem 2 n our final exercise, we are going to alter working code and reorganize it. We are going to alter the turtlegraphics package, which draws fractals. We will also alter the private, public and protected modifiers so that there is minimal access to class functions. We do this to make the code cleaner\" and allow us to make changes to the TurtleGraphics class with minimal impact on anyone that might be using the class outside of our package 1) Download tgraphics.zip, and import it into eclipse. Run the code and there should be two fractals drawn. We will maintain this functionality 2) Our goal is to move a TurtleGraphics object by using the TurtleMove class. In this step, move all the public static methods in Turtle Example.java into TurtleMove.java e., sierpinski, tree broccoli, and Koch. Also set an arbitrary pensize of your choice in each function. For example sierpinski should look like public static void sierpinski (int n int length TurtleGraphics theTurtle)t the Turtle.setPenSize (1); if (n 0) return for (int i 0; i K 3; i++){ sierpinski length/2 theTurtle); the Turtle.forward(length); theTurtle.right (120);Solution
public class TurtleExample {
// cut and paste into TurtleMove.java
public static void sierpinski(int n, int length, TurtleGraphics theTurtle){
if (n == 0) return;
for (int i = 0; i < 3; i++){
sierpinski(n-1,length/2, theTurtle);
theTurtle.forward(length);
theTurtle.right(120);
}
}
// cut and paste into TurtleMove.java
public static void tree(int r, TurtleGraphics theTurtle){
if (r<5){
theTurtle.forward(r);
theTurtle.backward( r);
} else {
theTurtle.forward( r/3);
theTurtle.left(30);
tree(r*2/3, theTurtle);
theTurtle.right(30);
theTurtle.backward( r/3);
theTurtle.forward(r/2);
theTurtle.right(25);
tree(r/2,theTurtle);
theTurtle.left(25);
theTurtle.backward(r/2);
theTurtle.forward( r*5/6);
theTurtle.right(25);
tree(r/2, theTurtle);
theTurtle.left(25);
theTurtle.backward(r*5/6);
}
}
// cut and paste into TurtleMove.java
public static void brocolli(int r, TurtleGraphics theTurtle){
// Leaf node just draw a stem
if (r<5){
theTurtle.forward(r);
theTurtle.backward( r);
} else {
// Make stem
theTurtle.forward( r/3);
// Make left branch
theTurtle.left(30);
brocolli(r*2/3, theTurtle);
// Make right branch
theTurtle.right(60);
brocolli(r*2/3, theTurtle);
// Return to center orientation
theTurtle.left(30);
// Return to base of stem
theTurtle.backward( r/3);
}
}
// cut and paste into TurtleMove.java
public static void koch(float r, float size, TurtleGraphics theTurtle) {
if (r <= 1) {
theTurtle.forward((int) (r*size));
theTurtle.left(60);
theTurtle.forward((int) (r*size));
theTurtle.right(120);
theTurtle.forward((int) (r*size));
theTurtle.left(60);
theTurtle.forward((int) (r*size));
} else {
koch(r-1, size/3, theTurtle);
theTurtle.left(60);
koch(r-1, size/3, theTurtle);
theTurtle.right(120);
koch(r-1, size/3, theTurtle);
theTurtle.left(60);
koch(r-1, size/3, theTurtle);
}
}
public static void main(String[] args) {
// Setup EZ graphics system.
TurtleGraphics aTurtle = new TurtleGraphics(1000,512);
TurtleGraphics bTurtle = new TurtleGraphics(1000,512);
aTurtle.penup();
aTurtle.moveTo(50, 500);
aTurtle.pendown();
koch(3, 300, aTurtle);
/*
aTurtle.setPenSize(2);
aTurtle.penup();
aTurtle.moveTo(500, 500);
aTurtle.left(90);
aTurtle.pendown();
brocolli(400,aTurtle);
*/










