Menú

Mostrar Mensajes

Esta sección te permite ver todos los mensajes escritos por este usuario. Ten en cuenta que sólo puedes ver los mensajes escritos en zonas a las que tienes acceso en este momento.

Mostrar Mensajes Menú

Mensajes - ITenZangetsuB

#1
Java / Ayuda para el metodo Wait()
20 Enero 2018, 18:28 PM
Estoy intentando hacer este ejercicio:

Implementar una carrera por relevos:
-Tenemos 4 Atletas dispuestos a correr
-Tenemos una clase principal Carrera
-Tenemos un objeto estático testigo
-Todos los atletas empiezan parados, uno comienza a correr (tarda entre 9 y 11s) y al terminar su
carrera pasa el testigo a otro que comienza a correr, y así sucesivamente
-Pistas:
- Thread.sleep y Math.random para simular la carrera
- synchronized, wait y notify para el paso del testigo o utlizar un Semaphore como testigo
- System.currentTimeMillis o Calendar para ver tiempos



Se que tengo que hacer el metodo wait de alguna forma para que los atletas se quedan esperando hasta que el otro llegue.

Si me podéis decir como llegar a la solucion o ayudar os la agradeceria

Esto es lo que llevo:


public class Carrera extends Thread{

   private static int testigo;
   private String[] relevos=new String[5];
   
   @Override
   public void run() {
      // TODO Auto-generated method stub
      
      for (int i = 1; i < relevos.length; i++) {
         
         int numeroAleatorio=(int)(Math.random()*(11-9+1) + 9);
         try {
            
            Thread.sleep(numeroAleatorio);
         System.out.println("Tiempo de espera");
         
      
         
         } catch (InterruptedException e)
         
         {
            System.out.println (e);
            
         
         }
         
              
           System.out.println (i+" Comienza a correr"); 
      }
            
      //Fin del metodo run
   }
   
   
   
   
}
#2
Java / Haciendo Sopa de Letras
28 Octubre 2017, 14:04 PM
A ver estoy intentando hacer una Sopa de letras.

Y tengo algunas dudas  la primera no consigo que lo que tengo en el fichero sopa.data se imprima en la consola estado realizando varias pruebas para intentarlo y nada.

Se tambien que tengo que hacer una clase Position para que me pase por consola la palabra en determinada posicion.

Se me  puedes echar algún cable lo agradeceria porque estado intentando pruebas y buscando por internet ejemplos que me ayudaran Gracias de todas formas.

Código (java) [Seleccionar]
public class SopaFileHunder {

protected char sopa[][];
private int punt=0;
private int maximo;

public SopaFileHunder(int largo,int ancho){
this.maximo=largo;
sopa=new char[largo][ancho];

}

public void Cargar(String cargar) {
try(BufferedReader car=new BufferedReader(new InputStreamReader
(new FileInputStream(cargar)))){
Object[] lines = car.lines().toArray();

if(lines.length>0) {
sopa= new char[lines.length][((String)lines[0]).length()];

for (int i = 0; i < lines.length; i++) {
sopa[i]=((String)lines[i]).toCharArray();
}
}

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}




/* if(punt!=maximo) {
sopa[punt]=cargar.toCharArray();
punt++;
}
*/

}


public char getChar(int fila, int col) {
return sopa[fila][col];

}

public String getRow(int posicion) {
String sor="";
for (int i = 0; i < sopa[posicion].length; i++) {
sor=sor+sopa[posicion][i];
}

return sor;

}

public String getCol(int posicion) {
String sor="";
for (int i = 0; i < sopa[posicion].length; i++) {
sor=sor+sopa[posicion][i];
}

return sor;

}

public String getDiagonal(int xoy, int yex, boolean dire) {
String sor="";
if(dire==true) {
int i= xoy; int j=yex;
while((i<sopa.length)&&(j<sopa[i].length)) {
sor+=sopa[i][j];
i++;
j++;

}

}else {
int i= xoy; int j=yex;
while((i<sopa.length)&&(j<=0)){
sor+=sopa[i][j];
i++;
j--;
}
}

return sor;

}

public void printSopa() {
for (int i = 0; i < sopa.length; i++) {
System.out.println(sopa[i].toString());
}
}



}


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.concurrent.Callable;

public class WordSopaThread implements Callable<Integer> {

public String word;
public File f;


public WordSopaThread(String word, File f) {
super();
this.word=word;
this.f=f;
}


@Override
public Integer call(){
// TODO Auto-generated method stub
try(BufferedReader in=new BufferedReader(new InputStreamReader(
new FileInputStream(f)))) {
String line=in.readLine();
return line.indexOf(word);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
return -1;
} catch (IOException e1) {
// TODO Auto-generated catch block
return -1;
}

}


}


public class SopaMain {

static String[] words= {"cazuela","sarten","martes","lunes","pepe"};

public static void main(String[] args) {
// TODO Auto-generated method stub

List<Future> list=new ArrayList<>();
ExecutorService executor=Executors.newFixedThreadPool(4);
for(int i=0;i<10;i++) {
list.add(executor.submit(new WordSopaThread(words[i], new File("sopa.data"))));
}

for(Future f:list) {
try {
System.out.println(f.get());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}


public class Position {

public int x;
public int y;

public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}



}


public class SopaApp {

public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub

SopaFileHunder zw=new SopaFileHunder(200,200);
WordSopaThread wd=new WordSopaThread(null, null);
zw.printSopa();
System.out.println(zw.getChar(0, 2));
System.out.println(zw.getRow(0));
System.out.println(zw.getCol(1));
System.out.println(zw.getDiagonal(1, 0, true));

}

}


Los códigos deben ir en etiquetas GeSHi