Está en la página 1de 10

FORO U2

DESARROLLO DE APLICACIONES (MSC G9B)


ROBERTO IVAN SANTIAGO HERNANDEZ MATRICULA A0123111915
A partir de los contenidos de la unidad responde:
 

 Aspecto 1:

Escriba una aplicación que permita implementar una carrera por relevos:
Se tienen 4 Atletas dispuestos a correr y  una clase principal Carrera.
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.
Sugerencias:
Utiliza
- Thread.sleep y Math.random para simular la carrera
- synchronized, wait y notify para el paso del testigo

Codigo:
package dau2a1;

import java.util.Scanner;

public class RELEVOS {


public static void main(String args[])
{
Scanner leer = new Scanner(System.in);
Carrera a = new Carrera();

System.out.println("Cuantos equipos participan: ");


a.setTe(leer.nextInt());
System.out.println("En sus marcas....");
System.out.println("Listo!!!....... ");
System.out.println("fuera....... ");
System.out.println("Inicia Carrera: ");
a.Iniciar();

package dau2a1;
public class Atleta extends Thread{
private final int equipo;
Carrera sender;

Atleta(int equipo, Carrera obj)


{
this.equipo = equipo;
sender = obj;
}

@Override
public void run()
{
synchronized(sender)
{
while(sender.activo != false)
{
try
{
this.wait();
}
catch(InterruptedException e)
{}
}
sender.correr(equipo);
}
}

   package dau2a1;

import java.util.Random;
public class Carrera {
boolean activo = false;
int turno = 1;
int tc = 0;
int te=0;

public void Iniciar() {


Carrera relevos[] = new Carrera[this.te];

for(int c=0; c<this.te; c++)


{
relevos[c] = new Carrera();
for(int e=0; e<4; e++)
{
new Atleta(c,relevos[c]).start();
}
}

private static int getRandomNumberInRange(int min, int max)


{
if (min >= max)
{
throw new IllegalArgumentException("Tiempo es mayor al normal");
}

Random r = new Random();


return r.nextInt((max - min) + 1) + min;
}

public void correr(int equipo)


{
activo = true;
System.out.println("Atleta"+turno+"E"+ (equipo+1) +" arranca...");
try
{
//tiempo promedio de corredor de 9 a 11 segundos.
int tiempo = getRandomNumberInRange(9,11)*200;
System.out.println("Tiempo por A"+turno+"E"+ (equipo+1) + ": " + (tiempo/200));
Thread.sleep(tiempo);
this.setTc(tiempo + this.getTc());
}
catch (InterruptedException e)
{
System.out.println("Equipo no Concluyo.");
}
if(turno!=4){
System.out.println("A"+turno+"E"+ (equipo+1) +" pasa Estafeta");

}else if(turno==4){
System.out.println("A"+turno+"E"+ (equipo+1) +" Llega a la Meta");
System.out.println("Tiempo por Equipo " + (equipo+1) + ": " + (double) (this.getTc()/200));
}

activo = false;
turno++;
this.notify();
}

public int getTc() {


return tc;
}

public void setTc(int tc) {


this.tc = tc;
}

public int getTe() {


return te;
}
public void setTe(int te) {
this.te = te;
}

}
Pantalla de Salida

 Aspecto 2:

Resuelve el siguiente ejercicio relacionado con el lenguaje de programación orientada a objetos:


Given:
1. public class WaitTest {
2. public static void main(String [] args) {
3. System.out.print("1 ");
4. synchronized(args){
5. System.out.print("2 ");
6. try {
7. args.wait();
8. }
9. catch(InterruptedException e){}
10. }
11. System.out.print("3 ");
12. }
13. }
What is the result of trying to compile and run this program?
A. It fails to compile because the IllegalMonitorStateException of wait() is not dealt
with in line 7
B. 1 2 3
C. 1 3
D. 1 2
E. At runtime, it throws an IllegalMonitorStateException when trying to wait
F. It will fail to compile because it has to be synchronized on the this object
 

La respuesta

 Aspecto 3: 

Resuelve el siguiente ejercicio relacionado con el lenguaje de programación orientada a objetos:


Given:
class MyThread extends Thread {
MyThread() {
System.out.print("MyThread ");
}
public void run() {
System.out.print("bar ");
}
public void run(String s) {
System.out.print("baz ");
}
}
public class TestThreads {
public static void main (String [] args)
{
Thread t = new MyThread() {
public void run() {
System.out.print("foo ");
}
};
t.start();
}}
What is the result?
A. foo
B. MyThread foo
C. MyThread bar
D. foo bar
E. foo bar baz
F. bar foo
G. Compilation fails
H. An exception is thrown at runtime
LA RESPUESTA ES B
Referencias
 Reese, R. M., & Reese, J. L. (2012). Java 7 New Features Cookbook. Birmingham B3 2PB,
UK.: Packt Publishing.
 Sznajdleder, P. A. (2013). Java a fondo. Estudio del lenguaje y desarrollo de
aplicaciones. Buenos Aires: Alfaomega Grupo Editor Argentino.

También podría gustarte