Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this assignment we are working with arrays. You have to ask the user to enter

ID: 3835467 • Letter: I

Question

In this assignment we are working with arrays. You have to ask the user to enter the size of the integer array to be declared and then initialize this array randomly. Then use a menu to select from the following Modify the elements of the array. At any point in the program, if you select this option you are modifying the elements of the array randomly. Print the items in the array, one to a line with their index. This will allow you to see the elements of your array, with their index Compute and print the total of the array items. Will calculate and output the summation of all elements of the array Compute and print the average value in the array. Will calculate and output the average of all the elements of the array Compute and print the smallest value in the array. Find the smallest element in the array Count and display the number of items in the array that are negative. Count how many negative numbers are there in the array, output each number with its index Sort the array. This option should take you to another menu to choose the order of sorting the array ascending or descending order. You should call methods for Displaying the main menu Getting the selection value from the user Validating your entry (for the size, and for the menu) Randomly generating your number. Printing the elements of the array Finding the total of the elements Calculating the average of the array elements Finding the smallest element in the array Counting the negative numbers of the array Sorting the array

Explanation / Answer

import java.util.*;
class ArrayOperations
{
    static void displayMenu()
    {
       System.out.println("1. Modify the elements of the array.");
       System.out.println("2. Print the items in the array.");
       System.out.println("3. Compute and print the total of the array items.");
       System.out.println("4. Compute and print the average value in the array.");
       System.out.println("5. Compute and print the smallest value in the array.");
       System.out.println("6. Count and display the number of items in the array that are negative.");
       System.out.println("7. Sort the array.");
       System.out.println("8. Quit");
    }
    static int selectChoice()
    {
       Scanner sc = new Scanner(System.in);
       System.out.print("Enter your choice: ");
       int choice = sc.nextInt();
       while(choice < 1 || choice > 8)
       {
          System.out.println("Choice should be in the range 1 - 8.");
          System.out.print("Enter your choice: ");
          choice = sc.nextInt();
       }
       return choice;
    }
    static void randomNumber(int[] array)
    {
       Random rand = new Random();
       for(int i = 0; i < array.length; i++)
           array[i] = rand.nextInt();
    }
    static void printValues(int[] array)
    {
       for(int i = 0; i < array.length; i++)
           System.out.print(array[i] + " ");
       System.out.println();  
    }
    static int totalOfElements(int[] array)
    {
       int sum = 0;
       for(int i = 0; i < array.length; i++)
           sum += array[i];
       return sum;            
    }
    static double averageOfElements(int[] array)
    {
       return (double)totalOfElements(array) / array.length;
    }
    static int smallestElement(int[] array)
    {
       int small = array[0];
       for(int i = 0; i < array.length; i++)
           if(array[i] < small)
               small = array[i];
       return small;      
    }
    static int countNegatives(int[] array)
    {
       int count = 0;
       for(int i = 0; i < array.length; i++)
           if(array[i] < 0)
               count++;
       return count;      
    }
    static void bubbleSort(int[] array)
    {
       for(int i = 0; i < array.length - 1; i++)
           for(int j = 0; j < array.length - i - 1; j++)
               if(array[j] > array[j+1])
               {
                  int temp = array[j];
                  array[j] = array[j+1];
                  array[j+1] = temp;
               }
    }
    public static void main(String[] args)
    {
       int choice;
       Scanner sc = new Scanner(System.in);
       System.out.print("Enter the number of elements to generate in the array: ");
       int numOfElements = sc.nextInt();
       int[] array = new int[numOfElements];
       randomNumber(array);
       while(true)
       {
          displayMenu();
          choice = selectChoice();
          switch(choice)
          {
             case 1:   randomNumber(array); break;
             case 2:   printValues(array); break;
             case 3:   System.out.println("Total: " + totalOfElements(array)); break;
             case 4: System.out.println("Average: " + averageOfElements(array)); break;
             case 5: System.out.println("Smallest: " + smallestElement(array)); break;
             case 6:   System.out.println("Negatives count: " + countNegatives(array)); break;
             case 7:   bubbleSort(array);
             case 8:   return;
             default: System.out.println("Invalid selection.");
          }
       }
    }
}