In: Computer Science
Please enter a seed:
1
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:
12
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: 6 1 1 6 8 4 5 1 1 1 7 7
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:
Examples: Please see the MinilabReviewExample1.pdf and MinilabReviewExample2.pdf that you are given for rather long examples of running the program. Please note:
There are tabs before and after each option number when the menu is printed.
The required Java code is
package sample;
import java.util.Random;
import java.util.Scanner;
public class MinilabReview {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a seed:");
int seed_val = sc.nextInt();
System.out.println("Please enter the size of the array:");
int size;
while(true){
size = sc.nextInt();
if (size>1){
break;
}
System.out.println("Array size must be greater than 1. Please reenter:");
}
int[] arr = new int[size];
Random random = new Random();
random.setSeed(new Long(seed_val));
for (int i =0;i<size;i++){
arr[i]=random.nextInt(10);
}
while(true){
System.out.println("\nPlease 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"
);
int option = sc.nextInt();
if(option==8){
break;
}
if (option==1){
System.out.print("Array: ");
for(int i =0;i<size;i++){
System.out.print(arr[i]+" ");
}
}else if (option==2){
double aver = average(arr,size);
System.out.println("Average: "+aver);
}else if(option==3){
int max = largest(arr,size);
System.out.println("Largest: "+max);
}else if(option==4){
int count = Occurence(arr,size);
System.out.println("Number of times 3 occurred: "+ count);
}else if (option==5){
int count = lessthfir(arr,size);
System.out.println("Number of elements that are less than half of the first element: "+ count);
}else if(option==6){
repeconse(arr,size);
}else if (option==7){
int x = arr[0];
arr[0]=arr[size-1];
arr[size-1]=x;
}
}
}
// Function that return average of an array.
static double average(int a[], int n)
{
// Find sum of array element
float sum = 0f;
for (int i = 0; i < n; i++)
sum += a[i];
return sum/n;
}
static int largest(int a[],int size){
//initializing first value as max
int max = a[0];
for(int i =0;i<size;i++){
if (a[i] > max)
max = a[i];
}
return max;
}
static int Occurence(int a[],int size){
int count=0;
for (int i =0 ;i<size;i++){
if(a[i]==3){
count++;
}
}
return count;
}
static int lessthfir(int a[],int size){
int count =0;
int val = new Integer(a[0]/2);
for (int i =1 ;i<size;i++){
if(a[i]<val){
count++;
}
}
return count;
}
static void repeconse(int a[],int size){
int start = a[0];
int i =1;
int count =0;
while (i<size){
if(start==a[i]){
count++;
}else{
if(count>0){
System.out.println(start+" number is repeating"+(count+1)+" time consecutively");
}
count=0;
start=a[i];
}
i++;
}
}
}
The output which I got while running it.
Please enter a seed:
1
Please enter the size of the array:
9
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: 5 8 7 3 4 4 4 6 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
2
Average: 5.44444465637207
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: 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
4
Number of times 3 occurred: 1
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
Number of times 3 occurred: 1
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
Number of elements that are less than half of the first element:
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
6
4 number is repeating3 time consecutively
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: 8 8 7 3 4 4 4 6 5
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
I hope you got the answer and understand it.
If you have any doubts you can ask in comments. I will help with it.
Thank you:):)