The purpose of this assignment is to practice:

  • using arrays
  • writing methods given a specification
  • writing a class that defines an object

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:

  • an accessor for each field
  • a mutator for each field
  • a toString method that returns a String representation of the Spice as an ordered pair consisting of the name followed by the weight.

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:

  • Write the Spice class
  • Write the SpiceRack method findSpiceSpace. If the SpiceRack object grandmasKitchen contains the following Spices:
0 1 2 3 4 5
(Cloves, 0.45) (Cinnamon, 1.24) (Nutmeg, 2.25) (Peppercorn, 1.72)
  • then the call grandmasKitchen.findSpiceSpace(“Nutmeg”)would return a 3 and the call grandmasKitchen.findSpiceSpace(“Sumac”); would return a -1.
  • Write the SpiceRack method consolidate. If the SpiceRack object grandmasKitchen contains the following Spices:
0 1 2 3 4 5
(Cloves, 0.45) (Cinnamon, 1.24) (Nutmeg, 2.25) (Peppercorn, 1.72)
  • then the call grandmasKitchen.consolidate() would arrange the Spices as shown below:
0 1 2 3 4 5
(Cloves, 0.45) (Cinnamon, 1.24) (Nutmeg, 2.25) (Peppercorn, 1.72)
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.