Use JAVA Develop a program which allows the user to enter nu
Use JAVA
Develop a program which allows the user to enter numbers into an array. Input will be as follows: The user will enter the total number of integers to be entered into the array. The user will then enter that number of unique integers (negative or positive). Do not allow the number of values entered to exceed the array size.
Develop methods to: ‘main’ method.
Print the array Sort the array ( YOU MUST DEVELOP YOUR OWN SORT METHOD – don’t use someone else’s)
Determine the highest value Determine the lowest value
Calculate the average value (double)
Solution
import java.util.Scanner;
public class ArraysSort {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int n;
Scanner scanner = new Scanner(System.in);
System.out.print(\"Enter the number of integers:\");
n = scanner.nextInt();
int[] arr = new int[n];
System.out.println(\"Enter all the elements:\");
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
arr = sort(arr);
System.out.print(\"Ascending Order:\");
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + \",\");
}
System.out.println(\"\ Highest Value:\" + arr[n - 1]);
System.out.println(\"Lowest Value:\" + arr[0]);
System.out.println(\"Average Value:\" + getAverage(arr));
}
/**
* method to sort the array in ascending order
*
* @param a
* @return
*/
public static int[] sort(int[] a) {
int temp;
for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[i] > a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
return a;
}
/**
* method to find the average from array elements
*
* @param arr
* @return
*/
public static double getAverage(int[] arr) {
int total = 0;
for (int i = 0; i < arr.length; i++) {
total += arr[i];
}
return (double) total / arr.length;
}
}
OUTPUT:
Enter the number of integers:5
Enter all the elements:
6
4
7
3
8
Ascending Order:3,4,6,7,8,
Highest Value:8
Lowest Value:3
Average Value:5.6

