Monday, June 13, 2005

5 Estudiantes 4 Materias 3 Notas y ordenacion de los promedios

/*
* Created on 06/11/2005
*
*/
package estructura;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
* @author vns
*
*/
public class Array543 {

int[][][] notas = new int[5][4][4];

BufferedReader linea = new BufferedReader( new InputStreamReader( System.in ) );

/**
*
*/
public Array543() {
super();
}

public static void main(String[] args) {
Array543 prg = new Array543();

prg.leerNotas();
prg.crearProNotas();
prg.escribirNotas();
prg.ordenar();
}

public void leerNotas(){
for (int i = 0; i < notas.length; i++) {
for (int j = 0; j < notas[i].length; j++) {
for(int k = 0; k < notas[i][j].length - 1 ; k++ )
try {
notas[i][j][3] = Integer.parseInt ( linea.readLine() );
} catch (Exception e) {
System.out.println(" Error en la lectura...");
}
}
}
}

private void escribirNotas(){
for (int i = 0; i < notas.length; i++) {
for (int j = 0; j < notas[i].length; j++) {
System.out.println("Estudiante " + i + "nota 1 " + notas[i][j][0]
+ "nota 2 " + notas[i][j][1] + "nota 3 " + notas[i][j][2] +
"Promedio " + notas[i][j][3]);
}
}
}

private void crearProNotas(){
for (int i = 0; i < notas.length; i++) {
for (int j = 0; j < notas[i].length; j++) {
notas[i][j][3] = notas[i][j][0] + notas[i][j][1] + notas[i][j][2];
}
}
}

//copiado del codigo ya hecho en MayorN.java
public static int mayor( int[] num ){
int mayor = num[0];

for (int i = 0; i < num.length; i++) {
if (num[i] > mayor) {
mayor = num[i];
}
}

return mayor;
}

public void ordenar(){
int[] temp = new int[16];
int k = 0;

//obtener promedios de las materias
for (int i = 0; i < notas.length; i++) {
for (int j = 0; j < notas[i].length; j++) {
temp[k++] = notas[i][j][3];
}
}

//ordenar promedios
for (int i = 0; i < temp.length; i++) {
for (int j = 0; j < temp.length; j++) {
if( temp[j] < temp[j+1] ){
k = temp[j];
temp[j] = temp[j+1];
temp[j+1] = k;
}
}
}

//Imprimir ordenados los promedios
for (int i = 0; i < temp.length; i++) {
System.out.println( temp[i] );
}
}
}

0 Comments:

Post a Comment

<< Home