Question: Assignment 15 Problem 1 : An ArrayExceptionTest class has been defined (the code can be found in the given java file ArrayExceptionTest.java.) This class uses
Assignment 15
Problem 1 :
An ArrayExceptionTest class has been defined (the code can be found in the given java file ArrayExceptionTest.java.) This class uses Scanner to obtain a number between 0 and 6 (inclusive) from the user and save the number in variable. The code has two limitations:
It does not handle the situation when the user input is not a number. If the input is not a number it throws NumerFormatException exception and stops. Modify the code to fix the above problem: when the user input is not a number, the program prints a not a number message and asks the user to enter another number. Use try- catch.
It does not handle a situation when the input is out of array size. It will throw ArrayIndexOutOfBoundsException for invalid array inputs. Fix the problem so when the number is out of range the program prints entered index is out of bounds and lets user input another number. Use try- catch. Note: Program successfully terminates only when the user inputs ###.
Modify ArrayExceptionTest.java and turn it in. Your output should look exactly like the sample output below.
Enter the index number to print the week day name. Enter ### to exit:
fewfacdsa
The user input is not a number
Enter the index number to print the week day name. Enter ### to exit:
0
The week day name is:Monday
Enter the index number to print the week day name. Enter ### to exit:
5
The week day name is:Saturday
Enter the index number to print the week day name. Enter ### to exit:
-1
The entered index is out of bounds
Enter the index number to print the week day name. Enter ### to exit:
98
The entered index is out of bounds
Enter the index number to print the week day name. Enter ### to exit:
###
The program is finished!
Problem 2:
Given BookList.java, that creates an ArrayList with 5 elements as below.
"Java Programming"
"Big Data Programming"
"Algorithms"
"Data Structures"
Implement the following 4 static methods.
displayList(ArrayList
subList(ArrayList
findIndex(ArrayList
removeItem(ArrayList
Sample Output:
Java Programming
Big Data Programming
Algorithms
Data Structures
SubList: [Algorithms, Data Structures]
The index of Java Programming is 0
The Updated List is:[Java Programming, Algorithms, Data Structures]
Codes Given #1:
import java.util.Scanner;
public class ArrayExceptionTest { public static String[] weekdays = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
public static void main(String[] args) { Scanner userInput = new Scanner(System.in); System.out.println("Enter the index number to print the week day name. Enter ### to exit: "); int index=0; while(true){ String input = userInput.nextLine(); if(input.equals("###")){ System.out.println("The program is finished!"); break; } index = Integer.parseInt(input);
String dayName = weekdays[index];
System.out.println("The week day name is:"+dayName); System.out.println("Enter the index number to print the week day name. Enter ### to exit: "); } }
}
Codes Given #2:
import java.util.ArrayList;
public class BookList {
private static ArrayList
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
