Declare an array to hold eight integers. Use a for loop to add eight random integers, all in the range from 50 to 100, inclusive, to this array. Duplicates are okay. Next, pass the array to a method that sorts the array and returns another array containing only the largest and smallest elements in the original array. Print these two values in main. Then use a foreach loop to display all elements of the sorted array on one line separated by a single space. This latter loop should also count the odd and even numbers in the array and determine the sum of all elements in the array. SAMPLE OUTPUT The lowest element is 59 The highest element is 96 Here is the array 59 64 76 77 80 88 91 96 Evens: 5, odds: 3 Total: 631

my code:

I am stuck on the problem to return the highest value and lowest value. Also on sorting the randomly generated integers. All in Java programming.

private static Random rand = new Random();

public static void main(String[] args){
int[] randomEight = numbers();
// printArray(randomEight);
System.out.println("The lowest element is");
System.out.println("The highest element is");
System.out.println("Here is the array");
// System.out.println(Arrays.toString(randomEight));
System.out.println();
for(int i = 0; i System.out.println(randomEight[i] + " ");
}
}
}

Respuesta :

Answer:

Explanation:

import java.util.Random;

public class RandomGen {

  private static Random rand = new Random();

  public static void main(String args[]) {

      int[] randomEight = new int[8];

      for(int i=0; i<8; i++) {

          /*

          * Get a random number between 50 and 100

          * */

          randomEight[i] = rand.nextInt(51) + 50;

      }

     

      int[] extremeValues = sortArray(randomEight);

      System.out.println("The lowest element is " + extremeValues[0]);

      System.out.println("The highest element is " + extremeValues[1]);

     

      int sum = 0;

      int oddCount = 0;

      int evenCount = 0;

     

      System.out.println("\nThe contents of the sorted array are : ");

      for (int i = 0; i < randomEight.length; i++) {

          System.out.print(randomEight[i] + " ");

          if (randomEight[i]%2==0) {

              evenCount++;

          }

          else {

              oddCount++;

          }

          sum += randomEight[i];

      }

      System.out.println("\nEven numbers count : " + evenCount);

      System.out.println("\nOdd numbers count   : " + oddCount);

      System.out.println("\nSum of all elements : " + sum);      

     

  }

  public static int[] sortArray(int[] input) {

      int extremeVal[] = new int[2];

      extremeVal[0] = input[0];   /* Minimum */

      extremeVal[1] = input[1];   /* Maximum */

      /*

      * Bubble Sort Algorithm

      * */

      int n = input.length;

      for (int i=0; i<n-1; i++) {

          for (int j=0; j<n-i-1; j++) {

              if (input[j] > input[j+1]) {

                  int temp = input[j];

                  input[j] = input[j+1];

                  input[j+1] = temp;

              }

          }

         

          /* Finding minimum */

          if (extremeVal[0] > input[i]) {

              extremeVal[0] = input[i];

          }

          /* Finding maximum */

          if (extremeVal[1] < input[i]) {

              extremeVal[1] = input[i];

          }

         

      }

      return extremeVal;

  }

}

 The lowest element is 58

The highest element is 71

The content of the sorted array are:

51 52 58 59 59 63 71 100

Even numbers count  :3

Odd numbers count   :5

Sum of all element      :513