You are on page 1of 2

Funciones: /* * To change this template, choose Tools | Templates * and open the template in the editor.

*/ package coffeearreglo; import java.io.*;

public class Arreglo{ //atributos private int tamanio; private int elemento[]; //metodos: public Arreglo(int t) {tamanio = t; elemento= new int[tamanio]; } public void cargar(){ String dato; try{ InputStreamReader lectura = new InputStreamReader(System.in); BufferedReader teclado = new BufferedReader(lectura); for(int k=0; k<tamanio; k++ ) {System.out.print("Elemento[" + (k + 1) + "]="); dato = teclado.readLine(); elemento[k]=Integer.parseInt(dato);} } catch(IOException e) {System.out.println(e.toString());}} public void mostrar(){ for(int i=0;i>tamanio ; i++){ System.out.print(elemento[i]+" "); System.out.println("\n"); } } public int sumar() {int suma=0; for(int i=0;i<tamanio ; i++){ suma+= elemento[i]; } return suma; } }

Main: /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package coffeearreglo; import java.io.*; public class Main {

public static void main(String[] args) { Arreglo a; String dato; int n=0; try {InputStreamReader lectura = new InputStreamReader(System.in); BufferedReader teclado = new BufferedReader(lectura); System.out.print("cuantos elementos son"); dato =teclado.readLine(); n=Integer.parseInt(dato); } catch(IOException e) {System.out.println(e.toString());} a = new Arreglo(n); System.out.println("digite los elementos del arreglo"); a.cargar(); System.out.println("los elementos almacenados son"); a.mostrar(); System.out.println("la suma de los elementos son:" + a.sumar()); } }

You might also like