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...
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....
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).
28. Add the following methods to the ArrayBoundedStack class, and create a test driver for each...
28. Add the following methods to the ArrayBoundedStack 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 ArrayBoundedStack, not by calling the previously defined public methods of the class. a. String toString()—creates and returns a string that correctly represents the current stack. Such a method could prove useful for testing and debugging the class...
Purpose Purpose is to implement some single linked list methods. Add methods to the List class...
Purpose Purpose is to implement some single linked list methods. Add methods to the List class In the ‘Implementation of linked lists’ lecture, review the ‘Dynamic implementation of single linked list’ section. You will be adding new methods to the List class. Eight new methods are required: new constructor – creates a new single linked list from an array of integers e.g. int a[] = {1, 2, 3, 4}; List list = new List(a); toString() – returns a string representing...
Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private...
Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private String name;    private double price;    private int bulkQuantity;    private double bulkPrice;    /***    *    * @param name    * @param price    * @param bulkQuantity    * @param bulkPrice    */    public Item(String name, double price, int bulkQuantity, double bulkPrice) {        this.name = name;        this.price = price;        this.bulkQuantity = bulkQuantity;        this.bulkPrice = bulkPrice;   ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT