CST8132 Lab8 Access Array Exercise solved


Category: You will receive a download link of the .ZIP file upon Payment


5/5 - (1 vote)

Warmup Exercises
A number of practice questions to help you prepare for doing this lab are provided in the file

Due Date and Demonstration
This Lab Exercise is worth 10 marks, and must be demonstrated to your lab instructor. See Blackboard for the
due date. Do not forget to do the Follow-Up Question and Activity!
The problem of this Lab Exercise is divided into six parts:
1. Lab Objectives
2. Description of the Problem
3. Required Output
4. Provided source code
5. Instructions
6. Problem-Solving Tips
7. Follow-Up Question and Activity
The program template represents a complete working Java program with one or more key lines of code
replaced with comments. Read the problem description and examine the sample output, then study the
template code. Using the problem-solving tips as a guide, replace the /* */ comments with Java code.
Compile and execute the program. Compare your output with the sample output provided. Then answer
the follow-up question. The source code for the template is provided for you.
Lab Objectives
This lab was designed to reinforce programming concepts from Chapter 11 (Exception Handling: A
Deeper Look) of Java How to Program: 10/e. In this lab, you will practice:
• Using exception handling to determine valid inputs.
• Using exception handling to write more robust and more fault-tolerant programs.
The follow-up activity also will give you practice:
• Creating your own exception type and throwing exceptions of that type.
Description of the Problem
Write a program that allows a user to input integer values into a 10-element array and search the array.
The program should allow the user to retrieve values from the array by index or by specifying a value to
locate. The program should handle any exceptions that might arise when inputting values or accessing
array elements. The program should throw a NumberNotFoundException 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.
Required Output
==>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

Provided source Code
The following java code is provided in lab7.zip
– ArrayAccessIf.java
– ArrayAccessTest.java
– NumberNotFoundException.java
The following is the recommended steps for this assignment:
– Create a package called “lab7”, and copy all the files from lab7.zip.
– Create ArrayAccess class that implements ArrayAccessIf.
– Implement the methods in ArrayAccess as described in ArrayAccessIf.
– Implement printing messages as per “Required Output”.
– If you are printing in try block, use “System.out” stream.
– If you are printing in catch block, ensure you use “System.err” stream.
– Run the provided ArrayAccessTest.java – and compare with required result.
Problem-Solving Tips
1. When you search the array for a value, you should define a boolean value at the beginning of the try
block and initialize it to false. If the value is found in the array, set the boolean value to true. This will
help you determine whether you need to throw an exception due to a search key that is not found.
4. If you have any questions as you proceed, ask your lab instructor for assistance.
Follow-Up Activity
1. Create a copy of your project (highlight the project in Eclipse and ctrl-C ctrl-V), and in the copy, create
another exception class called DuplicateValueException that will be thrown if the user inputs a value
that already resides in the array. Modify the copy your lab exercise solution to use this new exception
class to indicate when a duplicate value is input, in which case an appropriate error message should be
printed. During your demonstration, your lab instructor may ask to see either the original solution, or
the modified copy.