Question

In: Computer Science

Add code (see below for details) to the methods "set" and "get" in the following class,...

Add code (see below for details) to the methods "set" and "get" in the following class, ArrayTen.java, so that these two methods catch the exception java.lang.ArrayIndexOutOfBoundsException if an illegal index is used, and in turn throw java.lang.IndexOutOfBoundsException instead.

Modify the "main" method to catch java.lang.IndexOutOfBoundsException and, when such an exception is caught, print the exception as well as the stack trace.

public class ArrayTen {

private String myData[] = new String[10];

public void set(int index, String value) {
myData[index] = value;
}

public String get(int index) {
return myData[index];
}

public static void main(String[] args) {
ArrayTen a = new ArrayTen();
a.set(10, "hello world");
System.out.println (a.get(10));
}

}

The expected output from your successful modification will be:

exception java.lang.IndexOutOfBoundsException
java.lang.IndexOutOfBoundsException
at ArrayTen.set(ArrayTen.java:10)
at ArrayTen.main(ArrayTen.java:25)

**Adding code means you are not allowed to modify or delete any of the existing code statements. You must add "try" and "catch" and any additional code needed to fulfill the requirements, without modifying the existing statements.

Solutions

Expert Solution

Please find your solution below and if doubt comment and do upvote.

public class ArrayTen {

private String myData[] = new String[10];

public void set(int index, String value) {
    try{
        myData[index] = value;
    }
    catch(Exception e){
        System.out.println(e);
        e.printStackTrace();
        
    }
}

public String get(int index) {
    String data="";
    try{
        data= myData[index];
    }
    catch(Exception e){
        System.out.println(e);
        e.printStackTrace();
        
    }
    return data;
}

public static void main(String[] args) {
    ArrayTen a = new ArrayTen();
    a.set(10, "hello world");
    System.out.println (a.get(10));
    
}

}


Related Solutions

A. Add code (see below for details) to the methods "set" and "get" in the following...
A. Add code (see below for details) to the methods "set" and "get" in the following class, ArrayTen.java, so that these two methods catch the exception java.lang.ArrayIndexOutOfBoundsException if an illegal index is used, and in turn throw java.lang.IndexOutOfBoundsException instead. Modify the "main" method to catch java.lang.IndexOutOfBoundsException and, when such an exception is caught, print the exception as well as the stack trace. public class ArrayTen { private String myData[] = new String[10]; public void set(int index, String value) { myData[index]...
A. Add code (see below for details) to the methods "set" and "get" in the following...
A. Add code (see below for details) to the methods "set" and "get" in the following class, ArrayTen.java, so that these two methods catch the exception java.lang.ArrayIndexOutOfBoundsException if an illegal index is used, and in turn throw java.lang.IndexOutOfBoundsException instead. Modify the "main" method to catch java.lang.IndexOutOfBoundsException and, when such an exception is caught, print the exception as well as the stack trace. public class ArrayTen { private String myData[] = new String[10]; public void set(int index, String value) { myData[index]...
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String...
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String new_word): Adds a linkedlist item at the end of the linkedlist print(): Prints all the words inside of the linkedlist length(): Returns an int with the length of items in the linkedlist remove(int index): removes item at specified index itemAt(int index): returns LinkedList item at the index in the linkedlist public class MyLinkedList { private String name; private MyLinkedList next; public MyLinkedList(String n) {...
java code Add the following methods to the LinkedQueue class, and create a test driver for...
java code Add the following methods to the LinkedQueue class, and create a test driver for each to show that they work correctly. In order to practice your linked list cod- ing skills, code each of these methods by accessing the internal variables of the LinkedQueue, not by calling the previously de?ined public methods of the class. String toString() creates and returns a string that correctly represents the current queue. Such a method could prove useful for testing and debugging...
Please explain this C++ below on how to get the following outputs (see code) #include <iostream>...
Please explain this C++ below on how to get the following outputs (see code) #include <iostream> using namespace std; // Tests for algorithm correctness // ------------------------------- // 1 gives output "1 3   7 15 31 63 127 255 511 1023 2047" // 2 gives output "2 5 11 23 47 95 191 383 767 1535 3071" // 3 gives output "3 7 15 31 63 127 255 511 1023 2047 4095" // 5 gives output "5 11 23 47 95...
IN JAVA: Repeat Exercise 28, but add the methods to the LinkedStack class. Add the following...
IN JAVA: Repeat Exercise 28, but add the methods to the LinkedStack class. Add the following methods to the LinkedStacked class, and create a test driver for each to show that they work correctly. In order to practice your array related coding skills, code each of these methods by accessing the internal variables of the LinkedStacked, not by calling the previously defined public methods of the class. - String toString()—creates and returns a string that correctly represents the current stack....
Below is some code for a rectangle class that needs to be completed. Add member function...
Below is some code for a rectangle class that needs to be completed. Add member function declarations to the class declaration and member function definitions below the declaration. For the accessor functions, you can add the definitions directly into the class declaration. The goal is to code the class so that it works wthout changing the main program #include <iostream> using namespace std; // rectangle has a vertical height and horizontal width // The class below is a rectangle. It...
Add The following methods to the LinkedQueue class and create a test driver for each to...
Add The following methods to the LinkedQueue class and create a test driver for each to show they work correctly. In order to practice your linked list coding skills, code each of these methods by accessing the internal variables of the LinkedQueue, not by calling the previously defined public methods of the class. a. String toString () creates and returns a string that correctly represents the current queue. Such a method could prove useful for testing and debugging the class...
How do i get the code below to add an element before another without making the...
How do i get the code below to add an element before another without making the array off one element? (javascript) public void addBefore(double element) { itemCount++; double data[] = new double[this.data.length]; if(currentIndex <= itemCount) { if(currentIndex != 0) { for(int index = currentIndex; index >= itemCount; index ++) { data[index] = this.data[index]; } currentIndex--; data[currentIndex] = element; } if(currentIndex == 0) { data[0] = element; currentIndex = 0; } } }
code the following methods: one returning the average weekly hours and a method to add a...
code the following methods: one returning the average weekly hours and a method to add a weeks hours to the array of hours (this means creating a larger array).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT