Question

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 are less than half of the first element
6 Find how many times numbers repeat consecutively
7 Swap the first and last elements
8 Exit
1

Array: 3 7 2 1 3 0 6 3

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 are less than half of the first element
6 Find how many times numbers repeat consecutively
7 Swap the first and last elements
8 Exit
2

Average: 3.125

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 are less than half of the first element
6 Find how many times numbers repeat consecutively
7 Swap the first and last elements
8 Exit
3

Largest: 7

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 are less than half of the first element
6 Find how many times numbers repeat consecutively
7 Swap the first and last elements
8 Exit
4

3Count: 3

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 are less than half of the first element
6 Find how many times numbers repeat consecutively
7 Swap the first and last elements
8 Exit
5

Less than half of first: 2

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 are less than half of the first element
6 Find how many times numbers repeat consecutively
7 Swap the first and last elements
8 Exit
6

Repeats: 0

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 are less than half of the first element
6 Find how many times numbers repeat consecutively
7 Swap the first and last elements
8 Exit
7

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 are less than half of the first element
6 Find how many times numbers repeat consecutively
7 Swap the first and last elements
8 Exit
1

Array: 3 7 2 1 3 0 6 3


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 are less than half of the first element
6 Find how many times numbers repeat consecutively
7 Swap the first and last elements
8 Exit
9

Illegal option, try again

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 are less than half of the first element
6 Find how many times numbers repeat consecutively
7 Swap the first and last elements
8 Exit
8

======================================================

Your program: should be named MinilabReview.java and will create an array of (pseudo)random ints and present a menu to the user to choose what array manipulations to do.   Specifically, the program should:

  • Declare constants to specify the maximum integer that the array can contain (set to 8) and the integer whose occurrances will be counted (set to 3, to be used in one of the menu options).
  • Ask the user to enter a “seed” for the generation of random numbers (this is so everyone’s results will be the same, even though random).
  • Ask the user what the size of the array should be. Read in the size; it should be greater than 1. Keep making the user re-enter the value as long as it is out of bounds.
  • Create a new random number generator using the seed.
  • Create the array and fill it in with random numbers from your random number generator.   (Everyone’s random numbers therefore array elements should be in the range 0 to and everyone’s random numbers should match).
  • Show the user a menu of options (see examples that are given). Implement each option.   The output should be in the exact same format as the example. Finally, the menu should repeat until the user chooses the exit option.

Examples: Please see the MinilabReviewExample1.pdf and MinilabReviewExample2.pdf that you are given for rather long examples of running the program.   Please note:

  • If you use the same seed as in an example and use the Random number generator correctly, your results should be the same as the example.
  • Please be sure that the formatting is EXACT, including words, blank lines, spaces, and tabs.
  • Not all of the options nor all of the error checking may have been done in a given example, so you may have to add some test cases.
  • There is 1 space after each of the outputs (Array: ) or (Length: ) or (prompts)
  • There are 2 spaces between each element when the array is listed

There are tabs before and after each option number when the menu is printed.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Random;
import java.util.Scanner;

public class ArrayOperations {

    public static void printMenu() {
        System.out.println("Please choose an option:\n" +
                "1 Print the array\n" +
                "2 Find the average\n" +
                "3 Find the largest element\n" +
                "4 Count how many times 3 occurred\n" +
                "5 Count how many elements are less than half of the first element\n" +
                "6 Find how many times numbers repeat consecutively\n" +
                "7 Swap the first and last elements\n" +
                "8 Exit");
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Please enter a seed:");
        int seed = in.nextInt();
        Random random = new Random(seed);
        System.out.println("\nPlease enter the size of the array:");
        int size = in.nextInt();
        while (size <= 1) {
            System.out.println("Array size must be greater than 1. Please reenter:");
            size = in.nextInt();
        }
        int[] arr = new int[size];
        for (int i = 0; i < size; i++) {
            arr[i] = random.nextInt(10);
        }
        int choice;
        while (true) {
            printMenu();
            choice = in.nextInt();
            if (choice == 1) {
                System.out.print("Array:");
                for (int i = 0; i < size; i++) {
                    System.out.print(" " + arr[i]);
                }
                System.out.println();
            } else if (choice == 2) {
                double avg = 0;
                for (int i = 0; i < size; i++) {
                    avg += arr[i];
                }
                avg /= arr.length;
                System.out.println("Average: " + avg);
            } else if (choice == 3) {
                int max = arr[0];
                for (int i = 0; i < size; i++) {
                    if (arr[i] > max)
                        max = arr[i];
                }
                System.out.println("Largest: " + max);
            } else if (choice == 4) {
                int count = 0;
                for (int i = 0; i < size; i++) {
                    if (arr[i] == 3)
                        ++count;
                }
                System.out.println("3Count: " + count);
            } else if (choice == 5) {
                int count = 0;
                for (int i = 0; i < size; i++) {
                    if (arr[i] < arr[0]/2)
                        ++count;
                }
                System.out.println("Less than half of first: " + count);
            } else if (choice == 6) {
                int count = 0;
                for (int i = 0; i < size-1; i++) {
                    if (arr[i] == arr[i+1])
                        ++count;
                }
                System.out.println("Repeats: " + count);
            } else if (choice == 7) {
                int temp = arr[0];
                arr[0] = arr[arr.length-1];
                arr[arr.length-1] = temp;
            } else if (choice == 8){
                return;
            } else {
                System.out.println("Illegal option, try again");
            }
        }
    }
}
Add a comment
Know the answer?
Add Answer to:
JAVA - the program should output as follows: Please enter a seed: 2345 Please enter the...
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
  • Using Java In this assignment we are working with arrays. You have to ask the user...

    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. 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...

  • Write MIPS code, please. Following: Write a program to be used by the Wisconsin DNR to...

    Write MIPS code, please. Following: Write a program to be used by the Wisconsin DNR to track the number of Deer Hunting Licenses issued in a county and the state. It will display a menu with 5 choices numbered 1-5. They are (1) update the count, (2) display the number of licenses already issued to a county whose number is input, (3) display the county number and the number of licenses already issued to a range of counties whose starting...

  • Programming language: Java Home Work No.2 due 09.11.2019 either ascending or descending SORTING: An array is...

    Programming language: Java Home Work No.2 due 09.11.2019 either ascending or descending SORTING: An array is said to be ordered if its values order. In an ascending ordered array, the value of each element is less than or equal to the value of the next element. That is, [each element] <= [next element]. A sort is an algorithm for ordering an array. Of the many different techniques for sorting an array we discuss the bubble sort It requires the swapping...

  • 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,...

  • Create a program that counts how many times a number is included in an array. To...

    Create a program that counts how many times a number is included in an array. To create the array ask for 10 values (as integers) and then ask for the number that should be checked for. Then print the number of times the requested number appears in the array (see below). Please comment your code as you go along. Hint: You may need 2 for loops for this, one to create your array and one to check the values. Enter...

  • In C++: Write a program that asks the user for an array of X numbers. Your...

    In C++: Write a program that asks the user for an array of X numbers. Your program moves the largest number all the way to the right, and then shows it to the user. Hint: Use a loop to swap X times, but only swap if the number on the left is bigger than the number on the right. Sample Run: How many numbers would you like to enter? 6 Please enter number 1: 10 Please enter number 2: 8...

  • 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...

  • using matlab In Class Exercises 8-Arrays For the following exercises, assume an array is already populated, your job is to write a program that traverses the array. DO NOT HARD CODE the last in...

    using matlab In Class Exercises 8-Arrays For the following exercises, assume an array is already populated, your job is to write a program that traverses the array. DO NOT HARD CODE the last index. In other words, you code for 1-4&6 should be able to handle the following 2 arrays: amp2 10 array1 [52, 63, 99, 71, 3.1] array2 - [99:-3: 45); For 5: wordArray1 wordArray2 ('number, 'arrays', 'indices', 'hello', finish' [number, 'different, 'finish? 1. Determine the sum of all...

  • Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers...

    Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...

  • ***Please give comments and show it running*** Create a MIPS program that gets a set of...

    ***Please give comments and show it running*** Create a MIPS program that gets a set of numbers from the user, sorts them using selection sort, and displays the sorted numbers on the screen. You should create a subprogram to do the selection sort, sending it the length and address of the array. This is the pseudo code for the structure the selection: for (int i = 0; i < aLength-1; i++) {     /* find the min element in the...

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