Write a java program that creates an integer array with 50 random values, prompts the user to enter the index of an element in the array between 0 and 49, then displays the corresponding element value. If the specified index is out of bounds, display an error message (e.g. “Out of Bounds”) and ask the user to enter another index. Use a while loop that will keep prompting the user until a valid input is received. To handle invalid inputs, write two versions of your program: one that uses exception handling, and one that uses defensive programming. Assume a user will always enter numbers. Verify that the displayed element number is indeed from the index number entered. Add comments.
Sample run:
list[50] of random int generated
14 37 19 98 31 1 68 11 18 90
36 14 37 63 68 54 64 51 80 75
66 85 30 65 97 66 87 47 52 63
9 22 68 70 11 32 23 69 37 12
62 62 76 89 33 0 43 87 30 27
Enter an index: 55
Out of Bound. Try again:50
Out of Bound. Try again:5
The element is 1
Dear student your solution is below:
====================================================================
Note: Follow the screenshot of program to get the idea about indentation in program.
Comment are written in front of code for better understability of the program.
First program is based on defensive approac.
Second Program uses Exception Handling
------------------------------------------------------------------------------------------------------------------
1. Program in Java: (Defensive Approach)
-----------------------------------
//importing the classes
import java.util.Random;
import java.util.Scanner;
//class
public class Main
{
//main function
public static void main(String[] args) {
int list[] = new int[50];
//declearing a list of size 50
Random r = new
Random(); //for generating random number
Scanner sc = new
Scanner(System.in);
for(int i = 0; i < 50;
i++){
list[i] = r.nextInt(100);
//generating and storing random number in list
}
int index = -1;
System.out.print("Enter an Index:
"); //input index from user
index =
sc.nextInt();
//checking and taking input again
from user
while(index < 0 || index
> 49){
System.out.print("Out of Bound. Try
Again: ");
index = sc.nextInt();
}
sc.close();
//printing the element at provided
index
System.out.println("The
element is " + list[index]);
}
}// End of program
OUTPUT:
------------------------------------------------------------------------------------------------------------------------------------
2. Java Program (Exception Handling):
-------------------------------------------------------------
//importing the classes
import java.util.Random;
import java.util.Scanner;
//class
public class Main
{
//main function
public static void main(String[] args) {
int list[] = new int[50];
//declearing a list of size 50
Random r = new
Random(); //for generating random number
Scanner sc = new
Scanner(System.in);
for(int i = 0; i < 50;
i++){
list[i] = r.nextInt(100);
//generating and storing random number in list
}
int index = -1;
System.out.print("Enter an Index:
"); //input index from user
index =
sc.nextInt();
//checking and taking input again
from user with try-catch
while(true){
try{
//printing the element at provided
index
System.out.println("The
element is " + list[index]);
//if no exception occour in above
statement
// then break out of the while
loop
break;
}catch(ArrayIndexOutOfBoundsException e){
//If exception occour in try
block
System.out.print("Out of
Bound. Try Again: ");
index = sc.nextInt();
}
}
}
}
OUTPUT:
==============================================================================
Hope it has helped you.
==============================================================================
Get Answers For Free
Most questions answered within 1 hours.