This question involves reasoning about a 2D array of integers representing inventory in a large warehouse. The warehouse is organized into a grid of shelving sections.
Alright, let's tackle a problem you see in the real world every day, even if you don't notice it. Think about a massive online retailer's warehouse in a city like Dallas or Seattle. They have thousands of shelves, and they need to keep track of everything. We're going to model this with a 2D array. Each row represents a long shelf, and each column represents a vertical section of that shelf. The number in each cell is the count of items in that specific spot.
A Warehouse class manages this inventory, which is stored in a 2D array inventory.
public class Warehouse
{
/** The number of items in each section of the warehouse, where
* inventory[r][c] represents the number of items on shelf r, section c.
*/
private int[][] inventory;
/** Constructor and other methods are not shown. **/
// ... other methods
}
Write the Warehouse method countItemsInAisle. This method is called with an integer aisleNum representing a column index in the inventory grid.
First up, let's write a method to help a worker figure out how many items are in a specific aisle. An aisle is a vertical column in our grid. The countItemsInAisle method should calculate and return the total number of items in the specified aisle. You can assume that aisleNum will be a valid column index for the inventory grid.
A key thing to remember here: you're moving down a column, not across a row. Pay close attention to your loop and how you access the array indices.
For example, if inventory is the array shown below, the call countItemsInAisle(2) should return 18 (since 10 + 0 + 8 = 18).
// inventory array
{
{15, 20, 10, 5},
{ 0, 30, 0, 2},
{ 5, 12, 8, 1}
}
/** @param aisleNum a valid column index in the inventory grid
* @return the total number of items in the specified aisle (column)
*/
public int countItemsInAisle(int aisleNum)
Write the Warehouse method findShelfToRestock.
Now, for efficiency, the warehouse wants to find empty shelves to store new shipments. Your task is to write the method findShelfToRestock. This method will scan the grid and find the first shelf (row) that is completely empty, meaning every section in that row has 0 items. It should return the row index of that empty shelf. If it scans the whole warehouse and every shelf has at least one item, it should return -1.
This is a classic search problem. The trick is knowing when you've confirmed a whole row is empty and how to stop searching once you've found your answer.
/** Scans the inventory to find the first shelf (row) that is completely empty.
* @return the index of the first empty row, or -1 if no rows are empty.
*/
public int findShelfToRestock()
Write the Warehouse method consolidateInventory.
This last part is the most challenging, but you can absolutely do it. Over time, shelves get messy, with items spread out and empty spots in between. To make things tidy, we need to consolidateInventory. This method will go through every single row of the inventory grid and shift all the item counts to the left. All the empty '0' spots should end up on the right. For example, a row like { 5, 0, 10, 0, 3 } should become { 5, 10, 3, 0, 0 }. Notice the order of 5, 10, and 3 is preserved.
This is where most students slip up: you are modifying the inventory array in place. You are not creating a new array. Think about how you can process a row with a single pass. This method does not return a value.
For example, if inventory starts as:
{
{10, 0, 5, 0},
{ 0, 0, 0, 0},
{ 8, 2, 0, 4}
}
After calling consolidateInventory(), the inventory array should be modified to:
{
{10, 5, 0, 0},
{ 0, 0, 0, 0},
{ 8, 2, 4, 0}
}
/** Modifies the inventory grid by consolidating items in each row.
* In each row, all non-zero elements are moved to the beginning of the row,
* preserving their relative order. The remaining elements are set to 0.
* Postcondition: The inventory grid is modified as described.
*/
public void consolidateInventory()