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 OUTPUTThe lowest element is 59The highest element is 96Here is the array59 64 76 77 80 88 91 96 Evens: 5, odds: 3Total: 631 Textbook: Introduction to Java Programming and Data Structures, Comprehensive Version, Edition: 11thAuthor: Y. Daniel LiangPublisher: PearsonISBN: 9780134670942

Question
162 views

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

Textbook: Introduction to Java Programming and Data Structures, Comprehensive Version, Edition: 11th

Author: Y. Daniel Liang

Publisher: Pearson

ISBN: 9780134670942

check_circle

star
star
star
star
star
1 Rating
Step 1

Following is the java program that defines an array named array of 8 elements with each element initialized as random variable using Random() method. Sort method is defined to sort all these elements of array in ascending order using bubble sort algorithm. After sorting, element at index array[0] will give the smallest element of array and element at index array[array.length-1] will give the largest element. Finally sorted array is printed using for each loop and simultaneously check for even, odd number is performed using if else condition and sum of all elements of array is done.

Step 2

Code:

import java.util.Random;

//main class

public class Main {

//defining random variable

private static Random r = new Random();

public static void main(String args[]) {

//creating array to store 8 random numbers

int[] array = new int[8];

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

//to generate a random number from 50 to 100

array[i] = r.nextInt(51) + 50;

}

//calling sort method to sort elements of array and storing the return type in maxMin array

int[] maxMin = sort(array);

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

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

//declaring variables to store the number of even counts, odd count and sum of all numbers

int sum = 0;

int oCount = 0;

int eCount = 0;

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

//for each loop to print elements of sorted array

for (int i : array ) {

System.out.print(i + " ");

//condition to check if number is even then increment the even counter

if (i % 2 == 0) {

eCount++;

} else {

//if number is not divisible by 2 increment odd counter

oCount++;

}

Want to see the full answer?

See Solution

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in