Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write complete Java programs to complete the following task. Q1: Fix out-of-bound exception with simple test Ask the user to enter a length n for

Write complete Java programs to complete the following task.

Q1: Fix out-of-bound exception with simple test

Ask the user to enter a length n for an integer array

n must be larger than 0. Use a do while loop to keep asking the user enter an positive integer until a positive integer is entered

Generate random integers in the range of [1,n] to fill the array

In a do while loop, ask the user to enter an index of the array,

If the specified index is out of bounds, i.e. index<0 or index>=n display the message "Out of Bounds", then quit

otherwise, display the corresponding element value and ask the user to enter a next index

Test cases

# case 1 Please enter the length of an array: -7 Please enter the length of an array: 0 Please enter the length of an array: 10 Please enter an index: 3 The array element at index 3 is 3 Please enter an index: 8 The array element at index 8 is 3 Please enter an index: 6 The array element at index 6 is 5 Please enter an index: -2 Index -2 is out of bound [0,9] # case 2 Please enter the length of an array: 5 Please enter an index: 6 Index 6 is out of bound [0,4] # case 3 Please enter the length of an array: 7 Please enter an index: 7 Index 7 is out of bound [0,6]

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

What is gallows humor? Use Catch 22 to cite examples.

Answered: 1 week ago

Question

Question 1 of 4

Answered: 1 week ago

Question

Create a Fishbone diagram with the problem being coal "mine safety

Answered: 1 week ago

Question

How is communication defi ned?

Answered: 1 week ago

Question

What are the benefi ts of studying communication?

Answered: 1 week ago