elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Recuerda que debes registrarte en el foro para poder participar (preguntar y responder)


  Mostrar Temas
Páginas: [1]
1  Programación / Java / Ayuda para el metodo Wait() en: 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  Programación / Java / Haciendo Sopa de Letras en: 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
  1. public class SopaFileHunder {
  2.  
  3. protected char sopa[][];
  4. private int punt=0;
  5. private int maximo;
  6.  
  7. public SopaFileHunder(int largo,int ancho){
  8. this.maximo=largo;
  9. sopa=new char[largo][ancho];
  10.  
  11. }
  12.  
  13. public void Cargar(String cargar) {
  14. (new FileInputStream(cargar)))){
  15. Object[] lines = car.lines().toArray();
  16.  
  17. if(lines.length>0) {
  18. sopa= new char[lines.length][((String)lines[0]).length()];
  19.  
  20. for (int i = 0; i < lines.length; i++) {
  21. sopa[i]=((String)lines[i]).toCharArray();
  22. }
  23. }
  24.  
  25. } catch (FileNotFoundException e) {
  26. // TODO Auto-generated catch block
  27. e.printStackTrace();
  28. } catch (IOException e) {
  29. // TODO Auto-generated catch block
  30. e.printStackTrace();
  31. }
  32.  
  33.  
  34.  
  35.  
  36. /* if(punt!=maximo) {
  37. sopa[punt]=cargar.toCharArray();
  38. punt++;
  39. }
  40. */
  41.  
  42. }
  43.  
  44.  
  45. public char getChar(int fila, int col) {
  46. return sopa[fila][col];
  47.  
  48. }
  49.  
  50. public String getRow(int posicion) {
  51. String sor="";
  52. for (int i = 0; i < sopa[posicion].length; i++) {
  53. sor=sor+sopa[posicion][i];
  54. }
  55.  
  56. return sor;
  57.  
  58. }
  59.  
  60. public String getCol(int posicion) {
  61. String sor="";
  62. for (int i = 0; i < sopa[posicion].length; i++) {
  63. sor=sor+sopa[posicion][i];
  64. }
  65.  
  66. return sor;
  67.  
  68. }
  69.  
  70. public String getDiagonal(int xoy, int yex, boolean dire) {
  71. String sor="";
  72. if(dire==true) {
  73. int i= xoy; int j=yex;
  74. while((i<sopa.length)&&(j<sopa[i].length)) {
  75. sor+=sopa[i][j];
  76. i++;
  77. j++;
  78.  
  79. }
  80.  
  81. }else {
  82. int i= xoy; int j=yex;
  83. while((i<sopa.length)&&(j<=0)){
  84. sor+=sopa[i][j];
  85. i++;
  86. j--;
  87. }
  88. }
  89.  
  90. return sor;
  91.  
  92. }
  93.  
  94. public void printSopa() {
  95. for (int i = 0; i < sopa.length; i++) {
  96. System.out.println(sopa[i].toString());
  97. }
  98. }
  99.  
  100.  
  101.  
  102. }
  103.  
  104.  
  105. import java.io.BufferedReader;
  106. import java.io.File;
  107. import java.io.FileInputStream;
  108. import java.io.FileNotFoundException;
  109. import java.io.IOException;
  110. import java.io.InputStreamReader;
  111. import java.util.concurrent.Callable;
  112.  
  113. public class WordSopaThread implements Callable<Integer> {
  114.  
  115. public String word;
  116. public File f;
  117.  
  118.  
  119. public WordSopaThread(String word, File f) {
  120. super();
  121. this.word=word;
  122. this.f=f;
  123. }
  124.  
  125.  
  126. @Override
  127. public Integer call(){
  128. // TODO Auto-generated method stub
  129. new FileInputStream(f)))) {
  130. String line=in.readLine();
  131. return line.indexOf(word);
  132. } catch (FileNotFoundException e) {
  133. // TODO Auto-generated catch block
  134. return -1;
  135. } catch (IOException e1) {
  136. // TODO Auto-generated catch block
  137. return -1;
  138. }
  139.  
  140. }
  141.  
  142.  
  143. }
  144.  
  145.  
  146. public class SopaMain {
  147.  
  148. static String[] words= {"cazuela","sarten","martes","lunes","pepe"};
  149.  
  150. public static void main(String[] args) {
  151. // TODO Auto-generated method stub
  152.  
  153. List<Future> list=new ArrayList<>();
  154. ExecutorService executor=Executors.newFixedThreadPool(4);
  155. for(int i=0;i<10;i++) {
  156. list.add(executor.submit(new WordSopaThread(words[i], new File("sopa.data"))));
  157. }
  158.  
  159. for(Future f:list) {
  160. try {
  161. System.out.println(f.get());
  162. } catch (InterruptedException e) {
  163. // TODO Auto-generated catch block
  164. e.printStackTrace();
  165. } catch (ExecutionException e) {
  166. // TODO Auto-generated catch block
  167. e.printStackTrace();
  168. }
  169. }
  170. }
  171.  
  172. }
  173.  
  174.  
  175. public class Position {
  176.  
  177. public int x;
  178. public int y;
  179.  
  180. public int getX() {
  181. return x;
  182. }
  183. public void setX(int x) {
  184. this.x = x;
  185. }
  186. public int getY() {
  187. return y;
  188. }
  189. public void setY(int y) {
  190. this.y = y;
  191. }
  192.  
  193.  
  194.  
  195. }
  196.  
  197.  
  198. public class SopaApp {
  199.  
  200. public static void main(String[] args) throws FileNotFoundException {
  201. // TODO Auto-generated method stub
  202.  
  203. SopaFileHunder zw=new SopaFileHunder(200,200);
  204. WordSopaThread wd=new WordSopaThread(null, null);
  205. zw.printSopa();
  206. System.out.println(zw.getChar(0, 2));
  207. System.out.println(zw.getRow(0));
  208. System.out.println(zw.getCol(1));
  209. System.out.println(zw.getDiagonal(1, 0, true));
  210.  
  211. }
  212.  
  213. }
  214.  

Los códigos deben ir en etiquetas GeSHi
Páginas: [1]
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines