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 list):Prints all the elements in the list.

subList(ArrayList list, int fromIndex, int toIndex): Creates a subList with the fromIndex and toIndex. Also print the subList

findIndex(ArrayList list, String book): Check if the given book exists in the list . If exists, find and print the index of the book. Else, print The given book doesn't exist in the list.

removeItem(ArrayList list, String book) : Check if the given book exists in the list. If yes, remove the given book and print the updated list. Else print The given book doesn't exist in the list.

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 bookNames = new ArrayList(); //Prints all the elements in the list. private static void displayList(ArrayList list) { //Implement } //Creates a subList with the fromIndex and toIndex. Also print the subList private static void subList(ArrayList list, int fromIndex, int toIndex) { //Implement } //Check if the given book exists in the list . If exists, find and print the index of the book. Else, print The given book doesn't exist in the list. private static void findIndex(ArrayList list, String book) { //Implement } //Check if the given book exists in the list. If yes, remove the given book and print the updated list. Else print The given book doesn't exist in the list. private static void removeItem(ArrayList list, String book) { //Implement } private static void prepareBookList() { bookNames.add("Java Programming"); bookNames.add("Big Data Programming"); bookNames.add("Algorithms"); bookNames.add("Data Structures"); } public static void main(String[] args) { BookList.prepareBookList(); BookList.displayList(bookNames); BookList.subList(bookNames,2,4); BookList.findIndex(bookNames, "Java Programming"); BookList.removeItem(bookNames, "Big Data Programming"); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!