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
Asked Nov 11, 2019
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

Expert Answer

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++;

   }

   //adding all elements of arra...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

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

Engineering

Computer Science

Java

Related Computer Science Q&A

Find answers to questions asked by student like you
Show more Q&A
add
question_answer

Q: Translate the following pseudocode for finding the minimum value from a set of inputs into a Java pr...

A: Java program to find the minimum valueimport java.util.Scanner; public class Program{     Public sta...

question_answer

Q: Python 3.7.4: Write a loop that reads positive integers from standard input and that terminates when...

A: Program Instructions:The sum is used to calculate the sum of the list.The check variable is used to ...

question_answer

Q: How can I fix that Traceback (most recent call last):  File "C:\Users\Richard Seegerer\Documents\Pyt...

A: Here you are trying to open a file name module.txt in reading only format. You are utilizing a relat...

question_answer

Q: Using DeMorgan's Law, write an expression for the complement of F ifF(x,y,z) = xz' (xy + xz)+ xy'(wz...

A: DeMorgan’s law:The theorems of DeMorgan are basically two sets of rules or laws developed for AND, O...

question_answer

Q: Suppose that a maximum flow network contains a node, other than the source node, without incoming ar...

A: Maximum flow problem:“Yes”, it is possible if the maximum flow network does not contain source node ...

question_answer

Q: For the TCP/IP model, describe 2 types of vulnerabilities commonly attacked for each layer.

A: Two types of vulnerabilities in TCP/ IP model:TCP SYN flood attach (DDoS)DNS spoofing

question_answer

Q: Find the truth table that describes the following circuit.

A: The diagram shown below is taken from the given question- 

question_answer

Q: PLEASE EXPLAIN ALL STEPSIn the following code, determine the value of the symbole this. Predict and ...

A: Explanation of given code:“this” is a variable.DECO this, d; The instruction DECO means Decimal outp...

question_answer

Q: Hi, I am required to: Create an array of 100 integers using a random generator. ( 1 for loop) Provi...

A: The given java program will follow the following rubrics:Importing essential header files.Creating a...