Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

======= ======== ArrayQueue.java successfully compiled. [ArrayQueue.java] [Missing] Method public T[] getBackingArray() is missing/modified. ======= ======= ====== 1 import java.util. NoSuchElement Exception; * Your implementation

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

======= ======== ArrayQueue.java successfully compiled. [ArrayQueue.java] [Missing] Method public T[] getBackingArray() is missing/modified. ======= ======= ====== 1 import java.util. NoSuchElement Exception; * Your implementation of an array deque. + 10 +/ 11 public static final int INITIAL_CAPACITY = 9; 12 // Do not add new instance variables. 13 private T[] backingArray; 14 private int front: ZOVEETERS~822258225282885883=997495998; 15 public class ArrayQueue { The initial capacity of the ArrayDeque. 16 private int size: 17/** Constructs a new ArrayDeque with an initial capacity of *the {@code INITIAL_CAPACITY} constant above. 20 +/ 21 public ArrayQueue () { 31 48 49 } + + * * backingArray = (TI]) new Object [INITIAL_CAPACITY]; front = 0; size = 0; Adds the data to the front of the deque. If sufficient space is not available in the backing array, you should regrow it to double the current capacity. If a regrow is necessary, you should copy elements to the front of the new array and reset front to the beginning of the array (and move (@code back} appropriately). After the regrow, the new data should be at index 0 of * + the array. + * This method must run in amortized 0(1) time. + * @param data the data to add to the deque *@throws java.lang. Illegal Argument Exception if data is null 42 private void addFirst (T data) { if (data == null) { throw new Illegal Argument Exception ("The data is null and can not" be added."); + } if (size== backingArray. length) { T tempArr = (T[]) new Object [(size + 2)]; tempArr[0] = data; for (int i = 0; i < backingArray.length; i++) { 51 T num = backingArray[(front + i) % backingArray. length]; 52 tempArr[i+1] = num; 53 } 54 backingArray = tempArr; 55 front = 0; 56 57 58 } else { 59 60 if (front == 0) { 61 front backingArray.length; 62 } backingArray[(front - 1) % backingArray. length] = data: 63 64 front- 65 } 66 size++; 67 } 68 /** * + * If sufficient space is not available in the backing array, you should * regrow it to double the current capacity. If a regrow is necessary, * you should copy elements to the front of the new array and reset front to the beginning of the array (and move (@code back} appropriately). This method must run in amortized 0(1) time. @param data the data to add to the deque + @throws java.lang. Illegal Argument Exception if data is null 81 +/ 82 83 if (data == null) { 84 throw new Illegal Argument Exception("The data is null and can not" 85 + be added."); 86 } 70 71 72 73 74 75 * 76 * 77 * 78 + 79 80 * Adds the data to the back of the deque. private void addLast (T data) { 87 if (size == backingArray.length) { 88 T 89 for (int i = 0; i < backingArray.length; i++) { 90 T num = backingArray[(front + i) % backingArray, length]; 91 tempArr[i] = num; 92 } tempArr = (T[]) new Object [(size + 2)]; 93 tempArr[backingArray. length] = data; 94 backingArray = tempArr; 95 front = 0; 96 97 } else { 98 101 size++; 102 } 103 /** 104 *Removes the data at the front of the deque. 105 + 106 + Do not shrink the backing array. 107 + 108 * If the deque becomes empty as a result of this call, you should 109 * explicitly reset front and back to the beginning of the array. 110 + 111 * You should replace any spots that you remove from with null. Failure to 112 + do so will result in a major loss of points. 113 + 114 * This method must run in 0(1) time. 115 + * @return the data formerly at the front of the deque 116 117 + @throws java.util. NoSuchElement Exception if the deque is empty 118 +/ 119 private T removeFirst() { 120 if (size == 0) { 121 throw new java.util.NoSuchElement Exception ("The array is empty" 122 123so nothing can be removed."); 124 } 125 T ans backingArray [front % backingArray, length]; 126 backingArray[front % backingArray. length] = null; 127 size-- 128 if (size == 0) { 129 front = 0; 130 131 return ans; 132 } else { 133 front++; 134 return ans; 135 } 136} 137 /** 138 +Removes the data at the back of the deque. 139 140 + + Do not shrink the backing array. + 141 142 *If the deque becomes empty as a result of this call, you should 143 * explicitly reset front and back to the beginning of the array. 144 + 145 * You should replace any spots that you remove from with null. Failure to 146 * do so will result in a major loss of points. 147 + 148 * This method must run in 0(1) time. 149 * 150 * @return the data formerly at the back of the deque 151 @throws java.util.NoSuchElement Exception if the deque is empty. 152 +/ 153 private T removeLast() { 154 155 if (size == 0) { 156 throw new java.util. NoSuch Element Exception ("The array is empty" 157 + so nothing can be removed."); 158 } 159 if (front == 0) { 160 front backingArray.length; = 161 } 162 ans = backingArray[(front 1) % backingArray, length]; 163 backingArray[(front - 1) % backingArray. length] = null; 164 size-- 165 if (size == 0) { 166 front = 0; 167 168 return ans; 169 } else { 170 front- 171 return ans; 172 } 173} 174 /+* 175 176 177 178 + Returns the smallest non-negative remainder when dividing (@code index} *by (@code modulo). So, for example, if modulo is 5, then this method will return either 0, 1, 2, 3, or 4, depending on what the remainder is. + 179 This differs from using the % operator in that the % operator returns 180 + the smallest answer with the same sign as the dividend. So, for example, 181 + (-5) % 6 => -5, but with this method, mod(-5, 6) = 1. 182 183 + Examples: 184 +mod (-3, 5) => 2 185 mod(11, 6) => 5 186 This study source was downloaded by 100000847367614 from CourseHero.com on 07-21-2022 21:40:13 GMT -05:00 187 https://www.coursehero.com/file/32202956/ArrayDequejava/ 188 mod(-7, 7) => 0 189 190 * DO NOT MODIFY THIS METHOD. This helper method is here to make the math 191 192 + part of the circular behavior easier to work with. 193 * @param index the number to take the remainder of 194 @param modulo the divisor to divide by 195 * @return the remainder in its smallest non-negative form 196 @throws java.lang. Illegal Argument Exception if the modulo is non-positive 197 +/ 198 private static int mod(int index, int modulo) { 199 // DO NOT MODIFY! 200 if (modulo 201 throw new Illegal Argument Exception ("The modulo must be positive."); 202 } else { 203 int newIndex = index % modulo; 204 return newIndex >= 0 ? newIndex : newIndex + modulo; 205 } 206} 207 /** 208 209 + 210 + Runs in 0(1) for all cases. 211 + 212 DO NOT USE THIS METHOD IN YOUR CODE. 213 + 214 * @return the size of the list Returns the number of elements in the list. 215 +/ 216 public int size() { 217 // DO NOT MODIFY! 218 return size; 219 } 220 /** 221 222 * 223 + 224 + DO NOT USE THIS METHOD IN YOUR CODE. 225 + 226 @return the backing array 227 +/ 228 public T[] get BackingArray() { 229 // DO NOT MODIFY THIS METHOD! Returns the backing array of this deque. Normally, you would not do this, but we need it for grading your work. 230 return backingArray; } 231 232} 233 Array-Backed Queue For this assignment, you will be implementing a Queue back by an array. Recall that a Queue is a first-in, first-out (FIFO) data structure; the first item inserted is the first item to be removed. Your array-backed Queue should follow the requirements stated in the javadocs of each method you are to implement.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Java How To Program Late Objects Version

Authors: Paul Deitel, Deitel & Associates

8th Edition

0136123716, 9780136123712

More Books

Students also viewed these Programming questions

Question

What is the balance in the balanced scorecard?

Answered: 1 week ago