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

QUESTION 20 4 points Save Answer The following is an implementation of a \"prote

ID: 3708190 • Letter: Q

Question

QUESTION 20 4 points Save Answer The following is an implementation of a "protected" array. The purpose is to provide a more secure array where specific indices cannot be accessed. This class includes get and set methods that correlate to use of array brackets, however, these two methods check the index being visited again a variable called hiddenRegion to determine if it has data that the class should allow access to. If a set happens to an index that is in protected region, then nothing happens. If a get happens to an index that is in the protected region then an out of bounds exception is triggered. Complete the implementation of an Iterator class for Prottedrray. The iterator class should make sure that the elements in the protected region of the array are skipped. public class ProtectedArraycT> iplenents Iterable«T> ¡ private int hiddenRegton private Ti data public ProteccedArray (int size data II1) (new Coject[size]) hiddenReg on 0; public void secFrotectedRegion (int n) t this.hiddenRegion- public void set(int , 7 element) > hiddenRegion) return if(¡ data-element: public T get (int 1) throws Exception [ if (? > hiddenRegion) return datal return new ProtectedArray: throw new ArrayIndexout0FBoundsException("Index out of bounds." public IteratorE iterator private class Procectedarray implenents IteratorKE> /TODO: isplement this olass. T TT ? Paragraph ' Arial HTHL ESS

Explanation / Answer

Hello, I have completed the required Iterator class for ProtectedArray, also defined a Test class to test the working of this thoroughly. Note that I have changed the name of the iterator class as it can not be the same as that of the main class. In your question, name of the class and iterator class were the same. Drop a comment if you have any doubts. Thanks.

// ProtectedArray.java

import java.util.Iterator;

public class ProtectedArray<T> implements Iterable<T> {

                private int hiddenRegion;

                private T[] data;

                public ProtectedArray(int size) {

                                data = (T[]) new Object[size];

                                hiddenRegion = 0;

                }

                public void setProtectedRegion(int n) {

                                this.hiddenRegion = n;

                }

                public void set(int i, T element) {

                                if (i > hiddenRegion) {

                                                return;

                                }

                                data[i] = element;

                }

                public T get(int i) throws Exception {

                                if (i > hiddenRegion) {

                                                throw new ArrayIndexOutOfBoundsException("Index out of bounds.");

                                }

                                return data[i];

                }

                public Iterator<T> iterator() {

                                /**

                                * returning an iterator

                                */

                                return new ProtectedArrayIterator<T>();

                }

                /**

                * below is the definition for the iterator of ProtectedArray class, I had to

                * change the class name, because in the question it was same as that of the

                * main class, which is not allowed in java.

                */

                private class ProtectedArrayIterator<T> implements Iterator<T> {

                                private int index;// determines the index of current element

                                public ProtectedArrayIterator() {

                                                // setting index to 0

                                                index = 0;

                                }

                                public boolean hasNext() {

                                                /**

                                                * checking if there is any element left to be iterated, if the

                                                * element is in protected region, or if the index go out of bounds,

                                                * this method will return false

                                                */

                                                if (index > hiddenRegion || index >= data.length) {

                                                                return false;

                                                } else {

                                                                return true;

                                                }

                                }

                                public T next() {

                                                /**

                                                * returning the element at current index and updating the index

                                                * value

                                                */

                                                return (T) data[index++];

                                }

                                public void remove() {

                                                // setting the current element as null

                                                data[index] = null;

                                }

                }

}

// Test.java

import java.util.Iterator;

public class Test {

                public static void main(String[] args) {

                                /**

                                * creating a protected array of 10 String elements

                                */

                                ProtectedArray<String> array = new ProtectedArray<String>(10);

                                /**

                                * setting initial protected region to maximum size, so that we can add

                                * elements to it initially

                                */

                                array.setProtectedRegion(10);

                                /**

                                * Adding 10 elements in proper index values

                                */

                                array.set(0, "First");

                                array.set(1, "Second");

                                array.set(2, "Third");

                                array.set(3, "Fourth");

                                array.set(4, "Fifth");

                                array.set(5, "Sixth");

                                array.set(6, "Seventh");

                                array.set(7, "Eighth");

                                array.set(8, "Ninth");

                                array.set(9, "Tenth");

                                /**

                                * Demonstrating the use of iterator

                                */

                                Iterator<String> iterator = array.iterator();

                                System.out.println("All elements: ");

                                while (iterator.hasNext()) {

                                                System.out.println(iterator.next());

                                }

                                /**

                                * Changing protected region and displaying elements again using

                                * iterator

                                */

                                System.out.println(" Setting protected region to index 5 ");

                                array.setProtectedRegion(5);

                                iterator = array.iterator();

                                while (iterator.hasNext()) {

                                                System.out.println(iterator.next());

                                }

                                /**

                                * trying to access a protected index

                                */

                                try {

                                                System.out.println(" Trying to access element at index 6 ");

                                                System.out.println(array.get(6));

                                } catch (Exception e) {

                                                System.out.println(e.getMessage());

                                }

                }

}

/*OUTPUT*/

All elements:

First

Second

Third

Fourth

Fifth

Sixth

Seventh

Eighth

Ninth

Tenth

Setting protected region to index 5

First

Second

Third

Fourth

Fifth

Sixth

Trying to access element at index 6

Index out of bounds.