Question

The ExceptionLab class provided: – Creates an array of 100 elements and fills it with random numbers from 1 to 100. – It asks the user for an index value between 0 and 99. – Prints the element at that position. – If a number > 99 is entered by the user, the class will abort with an ArrayIndexOutOfBoundsException • Modify the ExceptionLab: – Add a try-catch clause which intercepts the ArrayIndexOutOfBounds and prints the message: Index value cannot be greater than or equal to 100. The array can only hold 100 elements – Add a finally clause which prints the entries in the Array. – Print 20 elements on each line of output. See the sample output.

Computer Organization and Programming 43/47 in class 42 Sample runs The Array has been filled with random numbers Enter an index value from 0 to 99 The element at index 8 is 67 The Array 77 20 96 50 46 84 57 56 67 52 12 63 47 22 42 58 34 37 5 15 84 61 74 71 85 5 78 12 72 3 72 39 83 35 55 11 47 72 44 33 12 60 55 58 92 48 48 52 60 69 90 90 26 75 92 100 10 65 49 93 74 92 95 93 47 72 52 60 20 23 76 88 22 9 13 66 25 14 36 30 32 60 29 49 35 13 55 68 98 19 83 89 42 15 22 72 66 88 39 3 The Array has been filled with random numbers Enter an index value from 0 to 99 100 Index value cannot be 100 Array contains 100 elements The Array 45 44 54 66 46 42 60 99 44 18 90 55 71 12 80 71 57 35 62 2 51 37 1 15 35 67 71 97 34 86 15 7 36 57 24 16 17 51 56 60 54 55 21 65 57 69 33 64 40 26 73 47 80 14 10 81 53 41 17 18 94 76 28 16 12 4 45 97 39 11 77 32 65 1 61 61 42 31 71 51 76 63 42 39 59 74 94 55 99 72 56 70 19 64 95 4 97 67 77 17

import java.util.*;

public class ExceptionLab {

public static void main(String[] args) {

Scanner reader = new Scanner (System.in);

int [] numbers = new int [100];

for (int i = 0; i < 100; i++)

{

numbers [i] = (int)(Math.random() * 100 + 1);

}

System.out.println("Enter an index value from 0 to 99");

int index = reader.nextInt();

//the statement following should be in the try clause

System.out.println( numbers [index]);

//add catch and finally

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;

public class ExceptionLab {

public static void main(String[] args) {

Scanner reader = new Scanner (System.in);

int [] numbers = new int [100];

for (int i = 0; i < 100; i++)

{

numbers [i] = (int)(Math.random() * 100 + 1);

}

System.out.println("The Array has been filled with random numbers");

System.out.println("Enter an index value from 0 to 99");

int index = reader.nextInt();

//the statement following should be in the try clause

try{

System.out.println( "The element at the index "+index+" is "+numbers [index]);

}catch(ArrayIndexOutOfBoundsException e){

System.out.println("Index value cannot be greater than or equal to 100\nArray contains 100 elements");

}

finally{

System.out.println("The Array");

for(int i=0;i<numbers.length;i++){

if(i%20==0 && i!=0) System.out.print("\n");

System.out.print(numbers[i]+" ");

}

}

}

}

The Array has been filled with random numbers Enter an index value from 0 to 99 he element at the index 8 is 70 The Array 24 70 8 10 96 33 12 64 70 77 44 63 28 75 11 17 34 86 25 13 40 60 63 29 44 18 27 41 59 9 16 89 59 66 51 95 48 54 34 78 84 71 90 64 59 19 17 93 93 10 80 91 65 36 37 6 37 9 26 21 42 82 21 11 91 61 4 54 82 66 84 94 81 19 82 26 80 10 81 3 72 77 16 51 9 90 20 38 32 78 18 99 68 38 81 57 86 88 93 91

Add a comment
Know the answer?
Add Answer to:
The ExceptionLab class provided: – Creates an array of 100 elements and fills it with random...
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
  • Consider the below matrixA, which you can copy and paste directly into Matlab.

    Problem #1: Consider the below matrix A, which you can copy and paste directly into Matlab. The matrix contains 3 columns. The first column consists of Test #1 marks, the second column is Test # 2 marks, and the third column is final exam marks for a large linear algebra course. Each row represents a particular student.A = [36 45 75 81 59 73 77 73 73 65 72 78 65 55 83 73 57 78 84 31 60 83...

  • Write a python nested for loop that prints out the following pattern 100 99 98 97...

    Write a python nested for loop that prints out the following pattern 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33...

  • 1. Forecast demand for Year 4. a. Explain what technique you utilized to forecast your demand....

    1. Forecast demand for Year 4. a. Explain what technique you utilized to forecast your demand. b. Explain why you chose this technique over others. Year 3 Year 1 Year 2 Actual Actual Actual Forecast Forecast Forecast Demand Demand Demand Week 1 52 57 63 55 66 77 Week 2 49 58 68 69 75 65 Week 3 47 50 58 65 80 74 Week 4 60 53 58 55 78 67 57 Week 5 49 57 64 76 77...

  • Use C++ (2D Array) Write a program which: 1. Assigns data given below into the 2D...

    Use C++ (2D Array) Write a program which: 1. Assigns data given below into the 2D array of integers which is 10x10. 2. Prints out the contents of the 2D array after assigning the data to make sure correct data was assigned. 3. Figures out and prints out the square root of the sum of ALL the elements in the 2D array. 4. Figures out and prints out the average of ALL THE ELEMENTS in the 2D array. 5. Figures...

  • 2.29 The following is the C codes and the translated MIPS codes. Assume that the C-level...

    2.29 The following is the C codes and the translated MIPS codes. Assume that the C-level integers t1 and a0 are held in register $t1 and $a0, and $s0 holds the base address of the integer MemArray. void foo () { int MemArray[100] = { 96, 98, 63, 69, 42, 27, 16, 6, 47, 74, 44, 33, 76, 7, 88, 33, 80, 86, 86, 64, 17, 67, 60, 51, 2, 61, 93, 87, 49, 98, 24, 98, 30, 65, 2,...

  • Use the accompanying data set on the pulse rates​ (in beats per​ minute) of males to...

    Use the accompanying data set on the pulse rates​ (in beats per​ minute) of males to complete parts​ (a) and​ (b) below. LOADING... Click the icon to view the pulse rates of males. a. Find the mean and standard​ deviation, and verify that the pulse rates have a distribution that is roughly normal. The mean of the pulse rates is 71.871.8 beats per minute. ​(Round to one decimal place as​ needed.) The standard deviation of the pulse rates is 12.212.2...

  • I need it in JAVA Write a program that randomly populates an array of size 100,...

    I need it in JAVA Write a program that randomly populates an array of size 100, sorts it, and then finds the median. The random numbers must be from 0-99 and all integer values. Also since there is an even set of numbers the median is found by taking the mean of the two middle numbers (In other words the 50th and 51st). You have to code your own sorting method. You may not use a built in java sorter....

  • I need to develop the 7 functions below into the main program that's given on top...

    I need to develop the 7 functions below into the main program that's given on top Write a C program to create array, with random numbers and perform operations show below. Il Project 3 (53-20). 1 Projects CS5,00 This file contains the function Programcution Dagine and one include <timo // Defining some constants definer_SIZE 20 define LOVE LIMIT 1 eine UPR UNIT define TALSE eine Tut 1 wold randomizery (int[], int, Int, int) wold pinay in. St, Int); En find...

  • Question 1 15 pts Test scores for a class of 40 students are listed below: 25...

    Question 1 15 pts Test scores for a class of 40 students are listed below: 25 35 43 44 47 48 54 55 56 57 59 62 63 65 66 68 69 69 71 72 72 73 74 76 77 77 78 79 80 81 81 82 83 85 89 92 93 94 97 98 a) The mean of the sample data is b) The median of the sample data is c) The standard deviation of the sample data is...

  • Use the pulse rates in beats per minute​ (bpm) of a random sample of adult females...

    Use the pulse rates in beats per minute​ (bpm) of a random sample of adult females listed in the data set available below to test the claim that the mean is less than 74 bpm. Use a 0.01 significance level. Pulse Rate​ (bpm) 60 87 93 92 95 62 92 93 71 67 35 84 54 68 74 36 72 67 100 61 52 42 58 104 67 102 83 95 73 76 38 42 55 85 38 99 84...

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
ADVERTISEMENT