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
Solución
Vicentemaestro · Tutor durante 5 años
Verificación de expertos
4.0 (299 votos)
Responder
1. To make Yertle draw a square and change the pen color for each side of the square, you can use the following code:<br />```java<br />public class Yertle {<br /> public static void main(String[] args) {<br /> // Set the initial pen color<br /> setColor(Color.blue);<br /><br /> // Draw the first side of the square<br /> forward(100);<br /><br /> // Turn right to draw the second side of the square<br /> turn(90);<br /><br /> // Set the pen color for the second side<br /> setColor(Color.green);<br /><br /> // Draw the second side of the square<br /> forward(100);<br /><br /> // Turn right to draw the third side of the square<br /> turn(90);<br /><br /> // Set the pen color for the third side<br /> setColor(Color.red);<br /><br /> // Draw the third side of the square<br /> forward(100);<br /><br /> // Turn right to draw the fourth side of the square<br /> turn(90);<br /><br /> // Set the pen color for the fourth side<br /> setColor(Color.yellow);<br /><br /> // Draw the fourth side of the square<br /> forward(100);<br /> }<br /><br /> public static void setColor(Color color) {<br /> // Code to set the pen color<br /> }<br /><br /> public static void forward(int distance) {<br /> // Code to move forward by the specified distance<br /> }<br /><br /> public static void turn(int angle) {<br /> // Code to turn by the specified angle<br /> }<br />}<br />```<br />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.<br /><br />2. To draw a triangle, you can use the following code:<br />```java<br />public class Yertle {<br /> public static void main(String[] args) {<br /> // Set the initial pen color<br /> setColor(Color.blue);<br /><br /> // Draw the first side of the triangle<br /> forward(100);<br /><br /> // Turn right to draw the second side of the triangle<br /> turn(120);<br /><br /> // Set the pen color for the second side<br /> setColor(Color.green);<br /><br /> // Draw the second side of the triangle<br /> forward(100);<br /><br /> // Turn right to draw the third side of the triangle<br /> turn(120);<br /><br /> // Set the pen color for the third side<br /> setColor(Color.red);<br /><br /> // Draw the third side of the triangle<br /> forward(100);<br /> }<br /><br /> public static void setColor(Color color) {<br /> // Code to set the pen color<br /> }<br /><br /> public static void forward(int distance) {<br /> // Code to move forward by the specified distance<br /> }<br /><br /> public static void turn(int angle) {<br /> // Code to turn by the specified angle<br /> }<br />}<br />```<br />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.
Haz clic para calificar: