Question: IN JAVA PLEASE. I need to write the code inside the method public boolean addItem(ShelfItem item, int position). This addItem method works a little differently

IN JAVA PLEASE. I need to write the code inside the method public boolean addItem(ShelfItem item, int position).

This addItem method works a little differently than the previous one, as it shifts the contents of the array to make room for the new entry and then adds the item at the specific position. If the item was added, the method will return true and false otherwise. This is an O(n) method.

1. If the position is valid based on the array length - [0, length)

2. If there is not room for the new item

3. Expand the array capacity

4. If there is room to add the new item

5. Shift the array contents over, making a hole for the new item

6. If the hole was created successfully

7. Add the new item at the specific position

8. Increment numberOfShelfItems

9. Return true or false appropriately

Note: In this case the expansion or shift could both fail. You will need to setup Boolean variables appropriately, which is omitted from this algorithm.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!