Created
April 28, 2012 08:46
-
-
Save ronbeltran/2517257 to your computer and use it in GitHub Desktop.
Array Test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package edu.upittc.training; | |
import javax.swing.JOptionPane; | |
public class ArrayTest { | |
public static int max(int[] x){ | |
//get max integer in array | |
int max = x[0]; | |
for(int i=0;i<x.length;i++){ | |
if( x[i] > max) { | |
max = x[i]; | |
} | |
} | |
return max; | |
} | |
public static int min(int[] x){ | |
//get min integer in array | |
int min = x[0]; | |
for(int i=0;i<x.length;i++){ | |
if( x[i] < min) { | |
min = x[i]; | |
} | |
} | |
return min; | |
} | |
public static int average(int[] x){ | |
//get average integer in array | |
int sum = 0; | |
for(int i=0;i<x.length;i++){ | |
sum += x[i]; | |
} | |
return sum/x.length; | |
} | |
public static int total(int[] x){ | |
//get total integer in array | |
int sum = 0; | |
for(int i=0;i<x.length;i++){ | |
sum += x[i]; | |
} | |
return sum; | |
} | |
public static void print(int[] x){ | |
//print array elements | |
int sum = 0; | |
System.out.println("Contents of the array with size "+ x.length+" :"); | |
for(int i=0;i<x.length;i++){ | |
System.out.println("Array[" + i + "]: "+x[i]); | |
} | |
} | |
public static void main(String[] args){ | |
int array_size = 0; | |
int max = 0; | |
try { | |
array_size = Integer.parseInt(JOptionPane.showInputDialog("Enter array size: ")); | |
} catch(NumberFormatException e){ | |
System.out.println("Error. Pls input numbers only."); | |
} | |
int[] array = new int[array_size]; | |
for(int i=0;i<array_size;i++){ | |
try { | |
array[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter array value "+(i+1)+" :")); | |
} catch(NumberFormatException e){ | |
System.out.println("Error. Pls input numbers only."); | |
} | |
} | |
print(array); | |
System.out.println("Max:" + max(array)); | |
System.out.println("Min:" + min(array)); | |
System.out.println("Avg:" + average(array)); | |
System.out.println("Total:" + total(array)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment