The purpose of this assignment is to practice:
Consider a software system that models a SpiceRack. A SpiceRack consists of n numbered spaces. Each space can hold at most one Spice. The spaces are indexed starting from 0; the index of the last space is n-1. No two Spices in the rack will have the same name. The class Spice represents your basic Spice. You should write the Spice class first. A Spice has a name and a weight. The Spice class will need the following methods:
The incomplete declaration for the SpiceRack is shown below. You will complete the declaration and write two unrelated methods for the SpiceRack class.
public class SpiceRack { /** * the spaces in the SpiceRack. Each array element holds a reference to * the Spice that is currently occupying the space. A null reference * indicates an empty space. */ private Spice[] spaces; /** * returns the index of the space that contains the * Spice with the specified name. * PRECONDITION: no 2 Spices have the same name * returns -1 if there is no Spice with the specified name */ public int findSpiceSpace(String name){}
/** * consolidates the SpiceRack by moving Spices so that they * are in adjacent spaces, starting at index 0, with no empty space * between any two Spices. * POSTCONDITION: the order of the Spices is the same as before * the consolidation */ public void consolidate(){} }
What you have to do for this assignment:
0 1 2 3 4 5
(Cloves, 0.45) (Cinnamon, 1.24) (Nutmeg, 2.25) (Peppercorn, 1.72)
0 1 2 3 4 5
(Cloves, 0.45) (Cinnamon, 1.24) (Nutmeg, 2.25) (Peppercorn, 1.72)
0 1 2 3 4 5
(Cloves, 0.45) (Cinnamon, 1.24) (Nutmeg, 2.25) (Peppercorn, 1.72)