Reference no: EM132430688
Here are the requirements:
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 (SIX) to: 'main' method, Print the array, Sort the array ( YOU MUST DEVELOP YOUR OWN SORT METHOD - don't use the API classes), Determine the highest value, Determine the lowest value, Calculate the average value (double).
My professor suggested this:
Program Logic (algorithm):
- Prompt and get the size of the array
- Create the array of that size
- Loop through the initialization of the array
o Prompt the user for each value
o Save the value in the corresponding element
- Print the array
- Sort the array
- Print the array again
- Output the highest value
- Output the lowest value
- Output the average
This is the code I already created:
import java.util.Scanner;
public class UserArray {
public static void main(String[] args){
int numElements, temp;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter number of elements you want in your array.");
numElements = keyboard.nextInt();
int num[] = new int[numElements];
System.out.println("Enter your elements.");
for (int i = 0; i < numElements; i++)
{
num[i] = keyboard.nextInt();
}
System.out.println("Your array before sorting: ");
int i;
for (i = 0; i < num.length; i++)
System.out.print(num[i] + ", ");
System.out.println();
for (i = 0; i < numElements; i++)
{
for (int j = i + 1; j < numElements; j++) {
if (num[i] > num[j])
{
temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
double[] arr = new double[numElements];
double total = 0;
for(i=0; i<arr.length; i++){
arr[i] = num[i];
}
for(i=0; i<arr.length; i++){
total = total + arr[i];
}
double average = total / arr.length;
System.out.print("Your array after sorting: ");
for (i = 0; i < numElements - 1; i++)
{
System.out.print(num[i] + ", ");
}
System.out.print(num[numElements - 1]);
System.out.println("nHighest value: " + num[i]);
System.out.println("Lowest value: " + num[0]);
System.out.format("The average is: %.2f", average);
}
}