jueves, 13 de octubre de 2011

Vectores en Java


Clase Vector… Aquí realizamos todos los procesos
package vector;
import java.io.*;
import java.util.Random;
public class Vector {
    public static InputStreamReader Leer = new InputStreamReader (System.in);
        public static BufferedReader Teclado = new BufferedReader (Leer);
     private int [] a=new int [5];
     private int [] b=new int [5];
    public Vector() {
        this.a[0]=0;
        this.b[0]=0;
    }
    public void LLenar(){
        int n=0,t=0;
    Random rnd =new Random();
while(t<5){
    int r=0;
    n = rnd.nextInt(10);
        for(int i=0;i<5;i++){
       if(this.a[i]!=n){
   r++;
    }  }
    if(r==5){
      a[t]=n; 
        t++;}
        }
int o=0;
    while(o<5){
    int r=0;
    n = rnd.nextInt(10);
    for(int i=0;i<5;i++){
       if(this.b[i]!=n){
   r++;
    }  }
    if(r==5){
      b[o]=n; 
        o++;}
        } }
public void Mostrar() throws IOException{
        System.out.println("primer vector ");
    for(int i=0;i<5;i++){
    System.out.println(this.a[i]);
    }
    System.out.println("segundo vector ");
    for(int o=0;o<5;o++){
    System.out.println(this.b[o]);
    }
    }
   public void Producto(){
    int [] c=new int [5];

    for (int h=0;h<5;h++){
    for(int i=0;i<5;i++){
     c[h]+=this.a[h]*this.b[i];
    }  }
    //return s;
   for (int h=0;h<5;h++){
      System.out.println(c[h]);
      }}
   public void iguales()throws IOException{
       int []l=new int[10];
int u=0;
     for (int h=0;h<5;h++){
    for(int i=0;i<5;i++){
        if(this.a[h]==this.b[i]){
        l[u]=this.a[h];
      u++;break;
        }  } }
     System.out.println("Numeros repetidos en los dos vectores");
    for (int h=0;h<10;h++){
        if(l[h]!=0){
        System.out.println(l[h]);
    } }
   }
      public void Union()throws IOException{
       int l=0;
       int []f=new int[5];
       for (int h=0;h<5;h++){
           f[h]=b[h];}
  for (int h=0;h<5;h++){    for(int i=0;i<5;i++){
        if(this.a[h]==f[i]){
        f[i]=11;
        l++;
        }  } }
int [] c=new int [10-l];     int i;
      for( i=0;i<5;i++){if(i<=5){
          c[i]=a[i];}
      }int v=5;
        for(int m=0;m<5;m++){
      if(f[m]<11){
          c[v]=f[m];v++;}}
         System.out.println("union de  vector ");
    for(int d=0;d<10-l;d++){
    System.out.println(c[d]);
    }}}



Clase principal… Aquí llamamos las operaciones realizadas en la clase anterior y es donde se va a ejecutar el programa
package vector;

import java.io.*;
public class Principal {

    public static InputStreamReader Leer = new InputStreamReader (System.in);
        public static BufferedReader Teclado = new BufferedReader (Leer);

    public static void main(String[] args) throws IOException {

        Vector miercoles = new Vector ();
       


       miercoles.LLenar();
       miercoles.Mostrar();
      System.out.println("producto de plano");
     
       miercoles.Producto();
miercoles.iguales();


       System.out.println("union");
     
     
 miercoles.Union();

    }

}
Ventana de la ejecucion



No hay comentarios:

Publicar un comentario