You are on page 1of 4

1er Ejemplo

/*
* DESCRIPCION: Para recorrer un arreglo se utiliza un ciclo,
*por lo general el ciclo for y se seala el ndice de la posicin a evaluar.
*Vamos a observar un ejemplo de un vector que es un arreglo unidimensional,
* es decir de una sola Posicin, una fila n columnas.
*/
public class vector {
public static void main(String[] args) {
int[] n; //declaracion del tipo de arreglo, en este caso es un vector.
n = new int[5];//declaracin del numero de posiciones que tendr el vector.
for (int i = 0; i < 4; i++) { //ciclo para recorrer el vector, desde la posicion 0
del vector hasta la 4(si cuentas, hay 5 posiciones).
n[i] = i; //asignamos el contenido de la variable i al vector.
System.out.println(n[i]); //se imprime el contenido del vector.
}
}
}

2do Ejemplo
/* * DESCRIPCIN: Programa que al ingresar los numero indica cual es el
mayor, el menor y da la suma de los dos. */
public class vector {
public static void main(String[] args) {
int mayor,menor,suma;
int [] nums={3,4,8,2};//asignamos directamente los valores del vector
suma=0;
menor=nums[0];
mayor=nums[0];
for(int i=0;i<nums.length;i++){
if (nums[i]>mayor){
mayor=nums[i];
}

if (nums[i]<menor){
menor=nums[i];
}
suma+=nums[i];
}
System.out.println("El mayor es"+mayor);
System.out.println("El menor es"+menor);
System.out.println("La suma es"+suma);
}
}
3er Ejemplo
/* * DESCRIPCIN: Programa que captura las notas de un estudiante y las
promedia */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Notas {
private long codigo;
private String nombre;
double[] notas = new double[3];
private double defi;
Notas(long codigo, String nombre, double[] arreglo) {
this.codigo = codigo;
this.nombre = nombre;
this.notas = arreglo;
this.defi = defi;
}
public long getcodigo() {
return codigo;
}
public double[] getNotas() {
return notas;
}
public String toString() {
return " Codigo: " + codigo + " Nombre: " + nombre;
}
}

class OperaNotas {
BufferedReader entrada = new BufferedReader(new
InputStreamReader(System.in));
long codigo;
String nombre;
double[] notas = new double[3];
Notas objeto;
int def;
private int total;
public void setEntrarDatos() throws IOException {
System.out.println("Ingrese Codigo: ");
codigo = Integer.parseInt(entrada.readLine());
System.out.println("Ingrese Nombre: ");
nombre = entrada.readLine();
for (int j = 0; j < notas.length; j++) {
System.out.println("Ingrese la Nota: " + (j + 1) + " ");
notas[j] = Double.parseDouble(entrada.readLine());
}
for (int i = 0; i <= 1; i++) {
for (int j = i + 1; j < 2; j++) {
def = (int) (notas[i]/3);
total = def/3 ;
}
}
objeto = new Notas(codigo, nombre, notas);
}
public void imprimir() {
System.out.println(objeto.toString());
for (int i = 0; i < notas.length; i++) {
System.out.println("Nota " + (i + 1) + " " + objeto.getNotas()[i]);
}
System.out.println("La Definitiva es:"+total);
}
}
class Test {
public static void main(String[] arg) throws IOException {
OperaNotas n = new OperaNotas();
n.setEntrarDatos();
n.imprimir();
}

You might also like