Question

(ArrayIndexOutOfBoundsException) Write a program that meets the following requirements: ■ Creates an array with 100 randomly...

(ArrayIndexOutOfBoundsException) Write a program that meets the following requirements: ■ Creates an array with 100 randomly chosen integers. ■ Prompts the user to enter the index of the array, then displays the corresponding element value. If the specified index is out of bounds, display the message Out of Bounds.

Following modifications:

* Use either a Scanner object or JOptionPane.showInputDialog() for input.

* Use either System.out.println() or JOptionPane.showMessageDialog() for output.

* In addition to ArrayIndexOutOfBoundsException, also catch InputMismatchException or NumberFormatException, and Exception.

This assignment is completed by writing a single application (.java file) with a main() method that includes all the elements

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

//random1.java

import java.util.InputMismatchException;
import java.util.Scanner;
public class Random1
{
public static void main(String []args)
{
int[] array = new int[100];
for (int k = 0; k < array.length; k++)
{
array[k] = (int) (Math.random() * 100);
}
Scanner inputval = new Scanner(System.in);
System.out.print("Enter an index here: ");
try
{
int index = inputval.nextInt();
System.out.println("array[" + index + "] = " + array[index]);
}
catch (ArrayIndexOutOfBoundsException ex)
{
System.out.println("INDEX " + ex.getLocalizedMessage() + " is out of bounds");
}
catch(InputMismatchException ime)
{
System.out.println("Wrong input");  
}
catch (Exception exc)
{
System.out.println("YOU ENTERED AN INVALID INPUT");
}
}
}

Randomi.java 3 1e import java.util.InputMismatchException; 2 import java.util.Scanner; 3 public class Randoml 4. 5e public st

Add a comment
Know the answer?
Add Answer to:
(ArrayIndexOutOfBoundsException) Write a program that meets the following requirements: ■ Creates an array with 100 randomly...
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
  • I need help wrting a java program that creates an array with 20 randomly chosen integers....

    I need help wrting a java program that creates an array with 20 randomly chosen integers. The program should prompt the user for an index of the array and display the corresponding element value. If the specified index is out of bounds,display the message "Out of Bounds".

  • CM245 Lab 4 (Chapter 12) Submit online through D2L Implement solutions using the appropriate concepts from...

    CM245 Lab 4 (Chapter 12) Submit online through D2L Implement solutions using the appropriate concepts from Chapter 12. There is no need for any UML for this lab. Provide your implementation (java source code) and the output for each of the tests. There should be one class for exercises 12.2 and 12.3 and two classes for 12.4. I prefer one output file per programming exercise. For each problem you must handle the exception. 12.2 (InputMismatchException) Write a program that prompts...

  • For Java - Write a program that creates an array of 10 integers. Ask the user...

    For Java - Write a program that creates an array of 10 integers. Ask the user to enter the subscript (index) of an element, then displays the element located at that subscript. If the subscript entered is out of bounds (less than 0 or greater than length - 1), display the message "Out of Bounds".

  • JAVA: (15 marks) Write a program that creates an integer array with 50 random values, prompts...

    JAVA: (15 marks) Write a 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...

  • Assignment 11 – Exceptions, Text Input/Output - Random Numbers Revised 9/2019 Chapter 12 discusses Exception Handling...

    Assignment 11 – Exceptions, Text Input/Output - Random Numbers Revised 9/2019 Chapter 12 discusses Exception Handling and Input/Output. Using try/catch/finally statement to handle exceptions, or declare/throw an exception as needed, create the following program: Create an array of 25 random numbers between 0 & 250 formatted to a field width of 10 & 4 decimal places (%10.4f). Use the formula Math.random() * 250. Display the array of random numbers on the console and also write to a file. Prompt the...

  • The ExceptionLab class provided: – Creates an array of 100 elements and fills it with random...

    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...

  • I have the following java-program, that creates a random walk starting at (0,0) and continuing until...

    I have the following java-program, that creates a random walk starting at (0,0) and continuing until x or y is greater than abs(n). First: I get an error, when I try closing the Scanner by inserting "reader.close(); " in line 28. It says "Unreachable code". How can I fix that? Second: Is it possible to create a different colour for the StdDraw.point when it reaches a point on one of the edges "n+1" or "n-1" ? 1 import java.util.*; 3...

  • Please write a Java program that does the following: Create an array of 100 integers. Store...

    Please write a Java program that does the following: Create an array of 100 integers. Store 100 random integers (between 1 and 100) in the array. Print out the elements of the array. Sort the array in ascending order. Print out the sorted array. Prompt the user to enter a number between 1 and 100. Search the array for that number and then display "Found" or "Not Found" message. Display each number from 1 to 100 and the number of...

  • Taking this program how can the requirements under it be added in java? import java.util.Scanner; public...

    Taking this program how can the requirements under it be added in java? import java.util.Scanner; public class RetirementSavings {    private static final int MAX_SALARY = 300000;    private static final double MAX_SAVING_RATE = 0.3;    private static final double MAX_INTEREST_RATE = 0.2;    private static final int MAX_YEAR_EMPLOYED = 40;    public static void main(String[] args) {        double savings_rate, interest_rate;        int salary, years_employed;        String name = ""; // name is initialized with an...

  • ( Object array + input) Write a Java program to meet the following requirements: 1. Define...

    ( Object array + input) Write a Java program to meet the following requirements: 1. Define a class called Student which contains: 1.1 data fields: a. An integer data field contains student id b. Two String data fields named firstname and lastname c. A String data field contains student’s email address 1.2 methods: a. A no-arg constructor that will create a default student object. b. A constructor that creates a student with the specified student id, firstname, lastname and email_address...

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