CS 261 Lab E - Bead Necklaces

Due Wed Feb 18 at 9:00am

Overview

In this assignment, you will practice using the "node and reference" structure of a LinkedList to model a necklace made up of beads of different sizes and colors. You will write a Necklace class to model this necklace as a kind of linked list of beads. Your necklace will need to keep Beads organized in order (based on a provided palette's rainbow, with larger beads coming first). You should be able to add and remove Beads from the necklace while maintaining the order. An example screenshot is below:

This lab will be completed in pairs. Review the pair programming guidelines if you don't remember them!

Objectives

Necessary Files

You will need to download the copy of the zipped source files. and import them into Eclipse. These files include:

Details

  1. Read through the provided classes and make sure you're familiar with them. Note that they won't compile without the Necklace class you will need to create. You should make a skeleton of that class (with empty methods) so that everything can compile.
  2. Since we want to be able to put Beads in order, it would be a good idea to add a way to compare them to see who comes before whom. Modify the Bead class so that it implements the Comparable<E> interface.
    • Generics are tricky! Since you're actually implementing the generic interface, does it still include a generic type, or do you get to specify what Beads can be compared to?
    • You will need to write an appropriate compareTo() method (see the documentation for further details). Beads should be sorted by color (you can use the Palette's getIndex(Color) method to get the "rank" of a color), followed by size (with larger beads being first).
    • Test this code thoroughly! A problem here can introduce difficult bugs later on.
  3. The bulk of the assignment will be implementing the Necklace class. A necklace is basically a linked-list of Beads. Consider: will this be a generic class?
    • Beads will act as the "nodes" of your necklace--each bead has a "hook" that another bead can be hung on.
    • Just like with a linked-List, you'll want a reference to the first bead (the head), as well as a size variable.
    • Your necklace must implement the following public interface:
      • void addBead(Bead) adds a bead to the necklace, placing the bead in the necklace following the appropriate order.
      • Bead removeBead(Color) removes a bead of the given color from the necklace. This can just be the first bead of that color (or the last bead of that color if you're feeling clever).
        • This method is now extra credit
      • Bead getBead(int) which returns the bead at the appropriate index (which is used for iterating and drawing the list).
      • int size() which deserves no further explanation.
    • You are welcome to (and should!) use the code in the textbook (pp. 88-97) as a reference for this lab, though since we're not making a generic list some of the methods will differ. (Also, keeping the beads in order will require further modification).
  4. Advice on how to proceed on the necklace:
    1. Write all your method signatures, then fill in the size() method. It's easy.
    2. I recommend you start by implementing two pieces of the necklace: write the get() method (which will be nearly identical to the version described in the text), and write the add() method so it just adds a bead to the front of the necklace (similar to what we did in class yesterday). This will let you run the frame and the tester and be able to see that the necklace shows up.
      • Remember to update your size variable!
    3. Once that is working, finish up the add method so that it places beads in order. Make sure this works!
    4. When writing remove, start by simply finding and printing out which bead you want to remove--check that you can do that. Then you can add in the change in pointers to actually remove the bead.
      • Remember to update your size variable!
  5. Try and keep algorithmic efficiency in mind; for example, don't iterate through your beads more than you have to. That said: always make the code work first, then optimize later.
  6. Include plenty of inline comments explaining how your methods work. This will be very handy for tracing problems that may arise!
  7. Check and double-check that your classes work flawlessly before submitting them to the submission folder. Also be sure that your names are on the top of your files.
  8. Fill out the lab partner evaluation survey after you turn in your work!

Submitting

Make sure both your names are on all the classes you've modified (Bead and Necklace), and upload the entire project directory to the LabE submission folder on hedwig. Only one partner should upload the code. Make sure you upload your work to the correct folder! The lab is due at the start of class the morning after lab.

After you have submitted your solution, log onto Moodle and submit the Lab E Partner Evaluation. Both partners need to submit evaluations.

Extensions

Try to make the bead prettier. Can you draw specular highlights on them? Give them a "shape" so we can have square beads, circular beads, polygon beads? Or even load an image to represent the bead?

Grading

This assignment will be graded based on approximately the following criteria:

  • Your Bead class implements comparable, ordering the beads by color and size [20%]
  • You have created the Necklace class that acts as a singularly linked list of beads [5%]
  • You can get a bead at a particular index [15%]
  • Your necklace correctly tracks its size [5%]
  • You can add a bead to your necklace, in order [25%]
  • You can remove a bead from your necklace, in order [25%]
  • You completed your lab partner evaluation [5%]