// Lab06.java (c) 2007 adolfo@di-mare.com /** @(#)Lab06.java 2007 Ejemplo de una clase que mantiene un vector ordenado. @author Adolfo Di Mare */ /** Clase para mantener un vector de valores enteros ordenados. */ class Lab06 { /** Capacidad máxima del vector */ public final static int Mx = 5; /** Vector de valores almacenados */ private int m_VEC[]; /** Ultimo componente libre en el vector {@code "m_VEC"} */ private int m_idx; /** Constructor por defecto */ public Lab06() { m_idx = 0; m_VEC = new int[Mx]; } /** Retorna {@code true} cuando ya no le cabe ningún valor más al vector. */ public boolean lleno() { return m_idx >= Mx; } /** Agrega una copia de {@code "v"} al vector. * No verifica si queda más campo en el vector. */ public void agregue(int v) { m_VEC[m_idx++] = v; reordene(); } /** Retorna la cantidad de valores almacenados en el vector. */ public int cardinalidad() { return m_idx; } /** Acceso al valor {@code "V[n]"}. */ public int at(int n) { return m_VEC[n]; } /** Reordena el valor de {@code "this->m_VEC[]"}, de manera que quede ordenado. * Precondición Se supone que los valores * m_VEC[0]...v[m_idx-2] ya están en orden. */ private void reordene() { for (int i=m_idx-2; i>=0; --i) { if (m_VEC[i] > m_VEC[i+1]) { // intercambia int tmp = m_VEC[i]; m_VEC[i] = m_VEC[i+1]; m_VEC[i+1] = tmp; } } } // reordene() /** Programa principal. */ public static void main( String args[] ) { int semilla = 1234; Random32 rand = new Random32( semilla ); Lab06 V = new Lab06(); System.out.println( "Comienzo..." ); while ( !V.lleno() ) { int m = rand.nextInt( 100 ); System.out.println( "Agrego otro número: " + (m+30) ); V.agregue( m+30 ); } for (int i=0; i0 ? test : test+m ); return m_seed; } /* Implementación Pascal original: *************************************************************** * Park, Stephen K. && Miller, Keith W.: * * "Random Number Generators Good Ones Are Hard To Find", * * Computing Practices, Communications of the ACM, * * Volume 31 Number 10, pp 1192-1201, October 1988 * *************************************************************** The following integer version of Random is correct on any system for which maxint is 2^31 - 1 or larger. First declare var seed : integer and then use function Random : real; (* Integer Version 2 *) const a = 16807; m = 2147483647; q = 127773; (* m div a *) r = 2836; (* m mod a *) var lo, hi, test : integer; begin hi := seed div q; lo := seed mod q; test := a * lo - r * hi; if test > 0 then seed := test else seed := test + m; Random := seed / m end; */ /** Prueba informal de que la implementación funciona. */ public static boolean test_Random32() { /* Implementación Pascal de Park && Miller pp 1195 seed := 1; for n := 1 to 10000 do u := Random; Writeln('The current value of seed is : ', seed); produces 1043618065. */ int u=-1; Random32 rand = new Random32(1); for ( int i=0; i<10000; ++i ) { u = rand.nextInt(); } return (u==1043618065); } } // NOTA: Para ejecutar use el comando: // > java Lab06 // [en la pantalla de interacciones del DrJava] // Jeliot: // - http://cs.joensuu.fi/jeliot/javaws/jeliot.jnlp // EOF: Lab06.java