Advertisement
Ferarias30

MDPYPC-TP02-6

Apr 7th, 2020
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.08 KB | None | 0 0
  1. MAIN:
  2. package ar.edu.unju.arreglo;
  3.  
  4. public class main {
  5.    
  6.     public static void main(String[] args) {
  7.         claseOperaciones op = new claseOperaciones();
  8.         int[] arreglo = new int[1000];
  9.         int min,max;
  10.         min = 50;
  11.         max = 100;
  12.        
  13.         op.cargar_arreglo(arreglo,min,max);
  14.         op.suma1_for(arreglo);
  15.         op.suma2_for(arreglo);
  16.         op.suma3_while(arreglo);
  17.         op.suma4_do_while(arreglo);
  18.        
  19.     }
  20.    
  21. }
  22. CLASS:
  23. package ar.edu.unju.arreglo;
  24.  
  25.  
  26. public class claseOperaciones {
  27.    
  28.     public static void suma1_for(int[] arreglo) {
  29.         int suma = 0;
  30.         int cont=0;
  31.         for(int i=0; i<(arreglo.length-750); i++) {
  32.             suma += arreglo[i];
  33.             cont++;
  34.         }
  35.         System.out.print("Suma desde posicion 0 hasta posicion 249 es: "+suma);
  36.         System.out.println(". Y se sumaron "+cont+" osiciones.");
  37.     }
  38.    
  39.     public static void suma2_for(int[] arreglo) {
  40.         int suma = 0;
  41.         int cont=0;
  42.         for(int i=250; i<(arreglo.length-500); i++) {
  43.             suma += arreglo[i];
  44.             cont++;
  45.         }
  46.         System.out.print("Suma desde posicion 250 hasta posicion 499 es: "+suma);
  47.         System.out.println(". Y se sumaron "+cont+" osiciones.");
  48.     }
  49.    
  50.     public static void suma3_while(int[] arreglo) {
  51.         int suma = 0;
  52.         int i=500;
  53.         int cont=0;
  54.         while(i<(arreglo.length-250)) {
  55.             suma += arreglo[i];
  56.             i++;
  57.             cont++;
  58.         }
  59.         System.out.print("Suma desde posicion 500 hasta posicion 749 es: "+suma);
  60.         System.out.println(". Y se sumaron "+cont+" osiciones.");
  61.     }
  62.    
  63.     public static void suma4_do_while(int[] arreglo) {
  64.         int suma = 0;
  65.         int i=750;
  66.         int cont=0;
  67.         do {
  68.             suma += arreglo[i];
  69.             i++;
  70.             cont++;
  71.         }while(i<arreglo.length);
  72.         System.out.print("Suma desde posicion 750 hasta la ultima posicion es: "+suma);
  73.         System.out.println(". Y se sumaron "+cont+" osiciones.");
  74.     }
  75.    
  76.     public static void cargar_arreglo(int[] arreglo,int min,int max) {
  77.         for(int i=0; i<arreglo.length; i++) {
  78.             int num = generar_numeros_aleatorios(min, max);
  79.             arreglo[i] = num;
  80.         }
  81.     }
  82.    
  83.     public static int generar_numeros_aleatorios(int min, int max) {
  84.        
  85.         int num=(int)Math.floor(Math.random()*(min-(max+1))+(max+1));
  86.         return num;
  87.        
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement