Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design and implement a class named SafeArray which solves the array index out of

ID: 3768755 • Letter: D

Question

Design and implement a class named SafeArray which solves the array index out of bounds problem and also allows the user to begin the array index starting at any integer, positive or negative. Every object of type SafeArray is an array of type int. During execution, when accessing an array component, if the index is out of bounds, the program must throw exception to handle the error. You will define your own exception class which will display the index out of bound error message. Consider the following statements:

SafeArray list(5) // declares list to be an array of 5 integers, list[0], list[1]……list[4]

SafeArray posList(2, 10) //declares posList to be an array of 8 integers, posList[2], posList[3}……. posList[9]

SafeArray negList(-2,10) //declares negList to be an array of 12 integers, negList[-2], negList[-1], negList[0].. ……negList[9]

A SafeArray header file is given and your just need to implement the member functions and test your class with each scenario in your main program.

Explanation / Answer

I have written the following code in java and executed under netbeans IDE and handling the Array index out of bounds exception is done through myExceptionHandler method.

package javaapplication1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class SafeArray {

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));//to read inout from the keyboard
public SafeArray(){//constructor
System.out.println("Implementing Safe Array");
}
public void list(int size) throws IOException{
int j,index;
int[] list=new int[size];
  
for(j=0;j<size;j++){//initializing the array
list[j]=j;
}
  
System.out.println("Enter the index to be access");
index=Integer.parseInt(br.readLine());
if(index<0 || index>size){//checking the bounds
myExceptionHandler();
}
else{
System.out.println(list[index]);//if with in bouds print the value
}
  
  
}
public void posList(int i,int j) throws IOException{
int start=i,end=j,k,index;
int[] list=new int[end];
  
for(k=start;k<end;k++){//initializing the array
list[k]=k;
}
System.out.println("Enter the index to be access");
index=Integer.parseInt(br.readLine());
if(index<start || index>end){
myExceptionHandler();
}
else{
System.out.println(list[index]);
}
}
public void negList(int i,int j) throws IOException{
int start=i,end=j,k,index;
int[] list=new int[end];
  
for(k=0;k<start;k++){
list[k]=k;
}
System.out.println("Enter the index to be access");
index=Integer.parseInt(br.readLine());
if(index<start || index>end){
myExceptionHandler();
}
else{
System.out.println(list[index]);
}
}
public void myExceptionHandler()
{
System.out.println("Array Index Out Of Bounds Exception");
}
public static void main(String[] args) throws IOException {
SafeArray sa=new SafeArray();
  
int arrayStartIndex,arrayStartNegIndex,arrayEndIndex,arraySize,choice;

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter the size of the array");
arraySize=Integer.parseInt(br.readLine());

System.out.println("Please enter the starting index of the array");
arrayStartIndex=Integer.parseInt(br.readLine());
  
System.out.println("Please enter the negative starting index of the array");
arrayStartNegIndex=Integer.parseInt(br.readLine());
  
System.out.println("Please enter the ending index of the array");
arrayEndIndex=Integer.parseInt(br.readLine());

System.out.println("Enter");
System.out.println("1.read from list()");
System.out.println("2.read from posList()");
System.out.println("3.read from negList()");
choice=Integer.parseInt(br.readLine());
switch(choice)
{
case 1:sa.list(arraySize);
break;
case 2:sa.posList(arrayStartIndex, arrayEndIndex);
break;
case 3:sa.negList(arrayStartIndex, arrayEndIndex);
default:System.out.println("Please choose Correct Option");
}   

}
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote