Question
Using Java

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. o 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. o This will allow you to see the elements of your array, with their index Compute and print the total of the array items. o Will calculate and output the summation of all elements of the array Compute and print the average value in the array. o Will calculate and output the average of all the elements of the array Compute and print the smallest value in the array. o Find the smallest element in the array Count and display the number of items in the array that are negative. o Count how many negative numbers are there in the array, output each number with its index Sort the array. o 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 1. Displaying the main menu 2. Getting the selection value from the user 3. Validating your entry (for the size, and for the menu) 4. Randomly generating your number. 5. Printing the elements of the array 6. Finding the total of the elements 7. Calculating the average of the array elements 8. Finding the smallest element in the array 9. Counting the negative numbers of the array 10. Sorting the array
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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.");
          }
       }
    }
}

Add a comment
Know the answer?
Add Answer to:
Using Java In this assignment we are working with arrays. You have to ask the user...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA - the program should output as follows: Please enter a seed: 2345 Please enter the...

    JAVA - the program should output as follows: Please enter a seed: 2345 Please enter the size of the array: 1 Array size must be greater than 1. Please reenter: 0 Array size must be greater than 1. Please reenter: -1 Array size must be greater than 1. Please reenter: 8 Please choose an option: 1 Print the array 2 Find the average 3 Find the largest element 4 Count how many times 3 occurred 5 Count how many elements...

  • Using Matlab Write a program which will: a. Accept two numbers from the user m and...

    Using Matlab Write a program which will: a. Accept two numbers from the user m and n b. Define an array with the dimensions a(n,m) and fill it with random numbers in the range of -100 to 100 inclusive. c. Provide the user the following menu from which he can select: 1. Sum of all elements in the array. Print the result. 2. Sum of the element in a row that he’ll specify. Print the result. 3. Sum of the...

  • Q1. Write a program in Java         a. Create 2 separate arrays, of user defined values,...

    Q1. Write a program in Java         a. Create 2 separate arrays, of user defined values, of same size         b. Add these 2 arrays. ........................................................................................................................... Q2. Write a program in java (using loop) input any10 numbers from user and store in an array. Check whether each of array element is positive, negative, zero, odd or even number. ............................................................................................................................ Q3. Write a program in Java to display first 10 natural numbers. Find the sum of only odd numbers. .............................................................................................................................. Q4....

  • Playing With Arrays The following problems are to give you practice with programming with arrays. Write...

    Playing With Arrays The following problems are to give you practice with programming with arrays. Write a program to: 1.Ask the user for numbers and place them in an array sequencially. 2.Next, print out the values in the array in the order they are stored. 3.Search the array for the postion containing the smallest value. 4.Print out the position where the smallest value was found. 5.Swap the smallest value with the value in the first postition of the array. 6.Print...

  • please help fill out the class menu create a prototype that will display information about housing...

    please help fill out the class menu create a prototype that will display information about housing data1. In this prototype you will use dynamic array data structures to store the data and compute metrics. For the requirements of the prototype, your client has given you a list of metrics and operations. The user interface will be a menu that displays this list and prompts the user to choose which option to execute.You will create two classes. First you will create...

  • Write a C++ program that prompts the user with the following menu options: Erase-ArrayContent Count-Words Quit...

    Write a C++ program that prompts the user with the following menu options: Erase-ArrayContent Count-Words Quit 1. For Erase-ArrayContent option, write complete code for the C++ function Erase described below. The prototype for Erase function is as follow: void Erase( int a[ ], int * N, int * Search-Element) The function Erase should remove all occurrences of Search-Element from the array al). Note that array a[ ] is loaded with integer numbers entered by the user through the keyboard. N...

  • Make a program using Java that asks the user to input an integer "size". That integer...

    Make a program using Java that asks the user to input an integer "size". That integer makes and prints out an evenly spaced, size by size 2D array (ex: 7 should make an index of 0-6 for col and rows). The array must be filled with random positive integers less than 100. Then, using recursion, find a "peak" and print out its number and location. (A peak is basically a number that is bigger than all of its "neighbors" (above,...

  • 14.5 Prog 5: People's weights (arrays) JAVA (1) Prompt the user to enter five numbers, being...

    14.5 Prog 5: People's weights (arrays) JAVA (1) Prompt the user to enter five numbers, being five people's weights. Store the numbers in an array of doubles. Output the array's numbers on one line, each number followed by one space. (2 pts) Ex: Enter weight 1: 236.0 Enter weight 2: 89.5 Enter weight 3: 142.0 Enter weight 4: 166.3 Enter weight 5: 93.0 You entered: 236.0 89.5 142.0 166.3 93.0 (2) Also output the total weight, by summing the array's...

  • Number of Numbers (10 pts) For this assignment you will be working with arrays. Open a...

    Number of Numbers (10 pts) For this assignment you will be working with arrays. Open a new Java project called Nums Prompt a user to enter in the length of the array (the number of numbers). Enter the number of numbers: Use this information to declare an array of the user-specified length. Next, using a for loop, prompt the user to enter that many numbers, and store each one in your array until your array is at full capacity. Using...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT