Ejer java 1 de 10
1) PainterPlus.java
import org.code.neighborhood.*;
import java.lang.Object;
import java.util.regex.Pattern;
public class PainterPlus extends Painter {
// Turns the Painter to the right
public void turnRight() {
turnLeft();
turnLeft();
turnLeft();
}
// Choice A: Takes all paint from a paint bucket
public void takeAllPaint() {
while (isOnBucket()) {
takePaint();
}
}
// Choice B: Moves forward while a Painter object can move
public void moveFast() {
while (canMove()) {
move();
}
}
// Choice C: Paints and moves while the Painter object has paint
public void paintToEmpty(String color) {
while (hasPaint()) {
paint(color);
move();
}
}
// Choice D: Moves, turns, and paints in a donut shape
public void paintDonut(String color) {
while (hasPaint()) {
move();
turnRight();
paint(color);
move();
paint(color);
}
}
}
Ejer java 2 de 10
2) NeighborhoodRunner.java (prog principal)
import org.code.neighborhood.*;
import java.lang.Object;
import java.util.regex.Pattern;
PROG PRINCIPAL
// programa principal ALGO PINTE 14 Veces de gris
public class NeighborhoodRunner { Y de longitud 30 → bucle con un
Contador entero (0,1,2,...15).
public static void main(String[] args)
{
// instancia de una clase mural painter
Se ejecuta 14 veces
MuralPainter mp = new MuralPainter();
Color gray
Longitud 30
// pinta un fondo gris . funcion mp.paintBackground no funciona
for(int k=0;k<=14;k++){
mp.paintBackground2("gray",30); - una linea de ida y otra de vuelta
} declaro una variable entera k
Vuelvo pos
k ; condicion k<=14 ;k++
k=0 → llama funcion bkg2 k=0+1
mp.resetPosition(); // posiciona en 1, 1 k=1 → “” k= 1+1
FigurePainter perro = new FigurePainter();
perro.paint();
k=14 → “” k=14+1
} // main
} // clase k=15 → NO SE CUMPLE COND
Mural painter crea mp
mp pinta background
De gris , long 30
Figure painter
Creo perro,
Llamo perro.paint()
Ejer java 3 de 10
3) MuralPainter.java
import org.code.neighborhood.*;
/* Desde prog principal
* MuralPainter is a PainterPlus that paints Llamo 14 veces a esto
* murals in The Neighborhood
*/ Color = gris
public class MuralPainter extends PainterPlus { Size = 30
public void paintBackground2(String color, int size) {
// 32, dibuja ida y vuelta
for(int i=1;i<=size;i++){ LINEA DE IDA
paint(color); Bucle I =0..30, 31 se sale
move(); - pinta
} - adelanta 1 posicion Baja una linea y
se orienta al otro lado
turnToWest();
for(int j=1;j<=size;j++){ LINEA DE VUELTA
paint(color); Bucle J
move(); J=0..30 , 31 sale
}
turnToEast();
}
----------------------------------------------------------------------------------------------------
/*
* Paints each row of The Neighborhood with the color
* where size is the size of the grid
*/
public void paintBackground(String color, int size) {
while (canMove("south")) {
paintLine(color, size);
turnToWest();
if (canMove("south")) {
paintLine(color, size);
turnToEast();
}
}
paintLine(color, size);
turnAround();
moveToCorner();
}
--------------------------------------------------------------------------------------------------
Ejer java 4 de 10
/*
* Paints a line with the color where the length
* of the line is specified by spaces
*/
public void paintLine(String color, int spaces) {
setPaint(spaces);
while (hasPaint()) {
paint(color);
if (canMove()) {
move();
}
}
}
------------------------------------------------------------------------------------------
/*
* Turns the MuralPainter to the next row to
* face west if it is currently facing east
*/ Baja una linea
public void turnToWest() { Y se gira al otro lado
if (isFacingEast()) {
turnRight(); Si esta apuntado a la derecha, hace un giro,
move(); avanza y hace otro giro
turnRight();
}
}
-----------------------------------------------------------------------
/*
* Turns the MuralPainter to the next row to
* face east if it is currently facing west
*/
public void turnToEast() {
if (isFacingWest()) {
turnLeft();
move();
turnLeft();
}
}
-------------------------------------------------------------------------------
/*
* Turns the MuralPainter around to face the opposite direction
*/
public void turnAround() {
turnLeft();
turnLeft();
}
-------------------------------------------------------------------------------------------
Ejer java 5 de 10
/*
* Resets the MuralPainter object to the starting location
*/
public void resetPosition() {
if (isFacingEast()) {
turnLeft();
while (canMove()) { Vuelve a la posicion 1,1
move();
}
turnLeft();
while (canMove()) {
move();
}
turnAround();
}
}
-------------------------------------------------------------------------------------------
/*
* Moves the MuralPainter to the bottom right corner
*/
public void moveToCorner() {
while (canMove()) {
move();
}
turnRight();
while (canMove()) {
move();
}
}
}
Ejer java 6 de 10
4) FigurePainter.java
import org.code.neighborhood.*;
public class FigurePainter extends MuralPainter {
public void paint(){ Columna 8
// pinta el cuadrado naranaja de la cara, no se puede llamar a una función que dentro Fila 1
// tenga otra llamada a otra función. da error
int cont=0;
posColFila(8,1);
while (cont < 10)
{
pintar2Lineas("orange",10);
move();
turnToEast();
cont++; Pintar background
} Cara color naranja
Y longitud 10
resetPosition();
pintarPelo("black",6); Vuelvo pos inicial 1,1
pintarOjos("white");
pintarNariz("blue",3);
pintarBoca("red",6);
resetPosition();
pintarBarbilla("yellow");
pintarCuello("orange",3);
}
---------------------------------------------------------------------------------------------
public void pintar2Lineas(String color,int largo){ Color orange
Largo 10
for(int i=1;i<=largo;i++){ Pinto linea de ida
paint(color); Bucle i
move();
} Giro , avanzo y
turnToWest(); doy la vuelta
move();
for(int j=1;j<=largo;j++){
paint(color); Pinto linea de vuelta
move(); Bucle j
}
turnToEast();
}
------------------------------------------------------------------------------------------------
Ejer java 7 de 10
public void pintarBarbilla(String color){
posColFila(13,18); // aprox mitad (12) y final de la cara (18) Se posiciona
turnLeft(); Gira
paint(color); Pinta 1 pto amarillo
}
-------------------------------------------------------------------------------------------------------
public void pintarCuello(String color,int largo){
turnLeft();
turnLeft();
Avanzar(2);
// k++ es igual a k=k+1 , se añade a k el valor 1
for (int k=1;k<=largo;k++)
{
paint(color);
move(); Gira hacia abajo
} Avanza 2 despues de pintar barbilla
} Pinta el cuello
--------------------------------------------------------------------------------------------------------
public void pintarBoca(String color, int largo){
Avanzar(3);
turnRight();
Avanzar(2);
turnLeft();
turnLeft();
for(int i=0;i<=largo;i++){
paint(color); Después pintar la nariz
move(); Avanza 3
}
}
--------------------------------------------------------------------------------------------------------
public void pintarNariz(String color, int largo){
// despues de pintar los ojos nos posicionamos aprox mitad cara y bajamos 2 espacios
Avanzar(2); Mediante avances
Pinta 3 de largo de color azul
turnRight(); se posiciona y mira hacia abajo
Avanzar(2);
turnLeft(); // hacia abajo
for(int i=0;i<=largo;i++){
paint(color);
move();
}
}
Ejer java 8 de 10
--------------------------------------------------------------------------------------------
public void pintarPelo(String colorNegro, int largo)
{
posColFila(8,1); // columna 6 , fila 3 // linea 3
paint(colorNegro);
move();
Fila 1
paint(colorNegro);
2) Avanzo 10 4)
posCol(largo); Sin pintar resetposition
paint(colorNegro);
move(); 1) Me posiciono 3) Pinto 2
paint(colorNegro); 2)
sobrescribo cuadrados
Avanzo 10
en negro, 2 cuadrados negros
resetPosition();
posColFila(7,2); // linea 4
paint(colorNegro);
move();
paint(colorNegro);
posCol(largo+2); Fila 2
paint(colorNegro);
move();
paint(colorNegro);
resetPosition();
posColFila(6,3); // linea 4
paint(colorNegro);
move();
paint(colorNegro); Fila 3
paint(colorNegro);
posCol(largo+3);
paint(colorNegro);
Pos col 7,
resetPosition(); Avanzo 12 Pinto 1 resetposition
Fila 2
}
Posiciono Avanzo 13
Col 6, fila 3 Pinto 1 resetposition
Ejer java 9 de 10
--------------------------------------------------------------------------------------
public void pintarOjos(String colorBlanco){
posColFila(10,3);
paint(colorBlanco); Pinta blanco Gira
move(); Mueve Avanza linea abajo
paint(colorBlanco); Pinta blanco
turnRight(); posiciona
move();
paint(colorBlanco);
turnRight();
move();
paint(colorBlanco);
Gira
// pintar otro ojo Se desplaza 4 sin pintar
turnToEast();
posCol(4);
turnLeft();
move();
paint(colorBlanco);
move();
paint(colorBlanco);
turnRight();
move();
paint(colorBlanco);
turnRight();
move();
paint(colorBlanco);
posiciona
Ejer java 10 de 10
-----------------------------------------------------------------------------------------
public void posColFila(int posx, int posy){
for (int i=0; i <= posx; i++) // columna Posiciono 8,1
{
move();
}
turnRight();
for (int j=0;j <= posy; j++) // fila
{
move();
}
turnLeft();
}
------------------------------------------------------------------------------------
public void posCol(int num){ // = columna
for (int i=0; i <= num; i++)
Avanzar sin pintar
{
Con el valor num,
move();
Primera vez 10 posiciones, se mantiene en la misma linea
}
// se mantiene en la misma fila y se desplaza n columnas
}
public void Avanzar(int num){ //
for (int i=0; i <= num; i++)
{
move();
}
Despues pintar negro
// se mantiene en la misma fila y se desplaza n columnas
Avanza unas posiciones
}
}