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

Modify the java programs provided in pAssignment07_template.zip and add necessar

ID: 3740129 • Letter: M

Question

Modify the java programs provided in pAssignment07_template.zip and add necessary code components so as to allow a user to input integer values into a 6-element array and search the array. The program should allow the user to retrieve values from the array by index and by specifying a value to locate (see the examples below and explanations in the class by the instructor). The program should handle any exceptions that might arise when inputting values or accessing array elements. The program should throw a NumberNotFoundException (refer to the template code given above) if a particular value cannot be found in the array during a search. If an attempt is made to access an element outside the array bounds, catch the ArrayIndexOutOfBoundsException and display an appropriate error message. Also, the program should throw an ArrayIndexOutOfBoundsException if an attempt is made to access an element for which the user has not yet input a value (the inputs and outputs should be though basic Java GUIs as shown below).

Explanation / Answer

Please find my answer:

Hi friend, you have not posted : pAssignment07_template.zip


Place the following code in ArrayAccess.java in package lab7.
Also place your other .java files (ArrayAccessIf, ArrayAccessTest, NumberNotFoundException) in package lab7.

To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i

package lab7;
public class ArrayAccess implements ArrayAccessIf {
private int[] array;
@Override
public void initArray(String[] s) {
try
{
if(s.length > 10)
throw new ArrayIndexOutOfBoundsException("Length greater than 10");
array = new int[s.length];
for(int i = 0; i < s.length; i++)
array[i] = Integer.parseInt(s[i]);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Array out of bounds");
}
catch(NumberFormatException e)
{
System.out.println("Invalid number");
}

}
@Override
public void findIndexFor(String s) {
int index = 0;
boolean found = false;
try
{
int num = Integer.parseInt(s);
for(index = 0; index < array.length; index++)
{
if(array[index] == num)
{
found = true;
break;
}
}
if(!found)
throw new NumberNotFoundException("Number not found");
else
System.out.println(num + ", index: " + index);
}
catch(NumberFormatException e)
{
System.out.println("Invalid number");
}
catch(NumberNotFoundException e)
{
System.out.println(e.getMessage());
}
}
@Override
public void findValueFor(String s) {
try
{
int index = Integer.parseInt(s);
if(index < 0 || index >= array.length)
throw new ArrayIndexOutOfBoundsException("Valid index is 0-" + array.length);
System.out.println("index " + index + ":" + array[index]);
}
catch(NumberFormatException e)
{
System.out.println("Invalid number");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Array out of bounds");
}
}
}


output of ArrayAccessTest.java
==============================
==>sending array test1
Invalid number
==>sending array test2
Array out of bounds
==>sending array test3
==>index for: aaa
Invalid number
==>index for: 12
Number not found
==>index for: 4
4, index: 3
==>value for: aaa
Invalid number
==>value for: -1
Array out of bounds
==>value for: 5
index 5:6

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