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

Programming Assignment # 2 Write a program that generates an array fAlled up wlt

ID: 3714750 • Letter: P

Question


Programming Assignment # 2 Write a program that generates an array fAlled up wlth random positive Integer number ranging froem 15 to 20, and display it on the screen. After the creation and displaying of the array, the program displays the following Plasition [RJeverse, [Ajverage, (Slearch. fQluit Please select an aptians Then, If the user selects Prsplaysthe aray lements pasition and value pairs for all member elements of the array -R (lowercase or uppercase): the program displays the reversed version of the A (lowercase or uppercase]: the program calculates and displays the average of the array elements S Cowercase or uppercase ): the program asks the user to insert an integer number and look for it In the array, returning the message wheher the number is found and its position [ including nultiple occurences), or is nat found Q (lowercase oe uppercase]: the program quits NOTE: Until the last option ('q'or) is selected, the main program comes back at the beginning, asking the user to insert a nevr integer number. Important notes, please read carefully: . On the due date please submit ONLY the Assignment2. cyour last namex cPp Eile through blackbaard. 2. Late programs will not be evalusted and o points wiall be given 6 8 9

Explanation / Answer

//CODE

import java.util.*;

public class RandomNumbers {

public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int c, Res = 0;
ArrayList<Integer> al = new ArrayList<Integer>();
for (int i = 0; i < 20; i++) {
Random r = new Random();

Res = r.nextInt(21);
al.add(Res);

}
Iterator itr = al.iterator();
while (itr.hasNext()) {
System.out.println("The random numbers are : " + itr.next());
}

Map<Integer, Integer> hm = new HashMap<Integer, Integer>();
System.out.println("#########################");
System.out.println("Select the option from Below List : Enter the First Charachter of the Choice");
System.out.println("[P]osition");
System.out.println("[A]verage");
System.out.println("[R]everse");
System.out.println("[S]earch");
System.out.println("[Q]uit");
System.out.println("##########################");

String str = s.nextLine();
while (!str.equalsIgnoreCase("Q")) {

if (str.equalsIgnoreCase("P")) {
System.out.println("Array element position and value pairs are :");
int index = 0;
for (int j = 0; j < al.size(); j++) {
int value = al.get(j);
System.out.println(String.valueOf(index++) + " : " + value);
hm.put(index, value);
}
} else if (str.equalsIgnoreCase("R")) { // calculating the reverse stream
System.out.println("The reversed version of the array is : ");
Collections.reverse(al);
System.out.println(al);
} else if (str.equalsIgnoreCase("A")) { //calculating the average
long sum = 0;
int n = al.size();
for (int i = 0; i < n; i++) {
sum += al.get(i);
}
double res = (double) sum / n;
System.out.println("The average of the array is : " + res);
} else if (str.equalsIgnoreCase("S")) { // search the elements
System.out.println("To search a value, Please enter the desired value :");
int des = s.nextInt();
if (al.contains(des)) {
int occurrences = Collections.frequency(al, des);
System.out.println("The Desired number " + des + " is found " + occurrences + " times.");
int m = 0;
for (int k : al) {
if (k == des) {
int p = al.indexOf(k);
System.out.println("The values are found at index "+m);
}// end of if
m++;
}
} else {
System.out.println("Number not found");
}
} else {
System.out.println("Invalid Choice..!!!");
}
System.out.println("Need to check more..?? (Y or N) :"); // prompt for other choice
String s = s.nextLine();
if (s.equalsIgnoreCase("Y")) {
System.out.println("Enter the Choice");
String str2 = s.nextLine();
str = str2;
} else if(s.equalsIgnoreCase("N")) {
break;
}
}
System.out.println("Quitting the Program..!"); // quitting the code
}

}

OUTPUT :

The random numbers are : 9
The random numbers are : 9
The random numbers are : 15
The random numbers are : 7
The random numbers are : 1
The random numbers are : 14
The random numbers are : 20
The random numbers are : 8
The random numbers are : 14
The random numbers are : 6
The random numbers are : 6
The random numbers are : 13
The random numbers are : 6
The random numbers are : 10
The random numbers are : 17
The random numbers are : 19
The random numbers are : 12
The random numbers are : 13
The random numbers are : 17
The random numbers are : 9
#########################
Select the option from Below List : Enter the First Charachter of the Choice
[P]osition
[A]verage
[R]everse
[S]earch
[Q]uit
#########################
p
The array element position and value pairs are :
0 : 9
1 : 9
2 : 15
3 : 7
4 : 1
5 : 14
6 : 20
7 : 8
8 : 14
9 : 6
10 : 6
11 : 13
12 : 6
13 : 10
14 : 17
15 : 19
16 : 12
17 : 13
18 : 17
19 : 9
Need to check more..?? (Y or N) :
y
Enter the Choice
r
The reversed version of the array is :
[9, 17, 13, 12, 19, 17, 10, 6, 13, 6, 6, 14, 8, 20, 14, 1, 7, 15, 9, 9]
Need to check more..?? (Y or N) :
y
Enter the Choice
a
The average of the array is : 11.25
Need to check more..?? (Y or N) :
y
Enter the Choice :
s
To search a value, Please enter the desired value :
10
The Desired number 10 is found 1 times.
The values are found at index 13
Need to check more..?? (Y or N) :

N
Quitting the Program..!