Inicio
/
Tecnología
/
1. Can You Make Yertle Draw a Square and Change the Pen Color for Each Side of the Square? Try Something Like:

Problemas

1. Can you make yertle draw a square and change the pen color for each side of the square? Try something like: yertle,setColor(Color:red): This uses the Color class in Java which has some colors predefined like red, yellow, blue, magenta cyan. You can also use more specific methods like setPenColor, setBodyColor, and setShellColor. 2. Can you draw a triangle? The turnRight(method always does 90 degree turns, but you'll need external angles of 120 degree for an equilateral triangle.Use the turn method which has a parameter for the angle of the turn in degrees. For example, turn(90)is the same as turnRight(). Try drawing a triangle with different colors. square public static void main(String[] args) f

Roztwór

Vicente maestro · Tutor durante 5 años
Weryfikacja ekspertów
4 (299 Votos)

Respuesta

1. To make Yertle draw a square and change the pen color for each side of the square, you can use the following code:```javapublic class Yertle { public static void main(String[] args) { // Set the initial pen color setColor(Color.blue); // Draw the first side of the square forward(100); // Turn right to draw the second side of the square turn(90); // Set the pen color for the second side setColor(Color.green); // Draw the second side of the square forward(100); // Turn right to draw the third side of the square turn(90); // Set the pen color for the third side setColor(Color.red); // Draw the third side of the square forward(100); // Turn right to draw the fourth side of the square turn(90); // Set the pen color for the fourth side setColor(Color.yellow); // Draw the fourth side of the square forward(100); } public static void setColor(Color color) { // Code to set the pen color } public static void forward(int distance) { // Code to move forward by the specified distance } public static void turn(int angle) { // Code to turn by the specified angle }}```In this code, we first set the initial pen color to blue. Then, we draw the first side of the square by moving forward by 100 units. We turn right by 90 degrees to draw the second side of the square. We set the pen color for the second side to green and draw the second side of the square. We turn right again to draw the third side of the square and set the pen color to red. Finally, we turn right one more time to draw the fourth side of the square and set the pen color to yellow.2. To draw a triangle, you can use the following code:```javapublic class Yertle { public static void main(String[] args) { // Set the initial pen color setColor(Color.blue); // Draw the first side of the triangle forward(100); // Turn right to draw the second side of the triangle turn(120); // Set the pen color for the second side setColor(Color.green); // Draw the second side of the triangle forward(100); // Turn right to draw the third side of the triangle turn(120); // Set the pen color for the third side setColor(Color.red); // Draw the third side of the triangle forward(100); } public static void setColor(Color color) { // Code to set the pen color } public static void forward(int distance) { // Code to move forward by the specified distance } public static void turn(int angle) { // Code to turn by the specified angle }}```In this code, we first set the initial pen color to blue. Then, we draw the first side of the triangle by moving forward by 100 units. We turn right by 120 degrees to draw the second side of the triangle. We set the pen color for the second side to green and draw the second side of the triangle. We turn right again to draw the third side of the triangle and set the pen color to red. Finally, we turn right one more time to draw the third side of the triangle and set the pen color to yellow.