Why is linked lists (java computer programming) so hard?

Updated 6 days ago:

Part 1

In the first part of the assignment, you will implement a class called GuitarString that models a vibrating guitar string of a given frequency. The GuitarString object will need to keep track of a ring buffer. You are to implement the ring buffer as a queue using the Queue<E> interface and the LinkedList<E> implementation. 

Your class should have the following public methods.

GuitarString(double frequency)

GuitarString(double[] init)

void pluck()

void tic()

double sample()

2 Answers

  • 5 days ago
    Favorite Answer

    That's a cool problem.  Thanks for sharing.

    If you're thinking in terms of linked lists, though, I think you're the one making the problem harder than it needs to be.  

    You're supposed to use a queue, not a list.  A queue only supports three actions:

        - add an element to the end

        - remove an element from the front

        - (optional) "peek" at the front element without removing it

    The Queue<T> interface provides two different versions of each of those.  The LinkedList<T> class is simply one of the standard classes that implements Queue<T>.  ArrayList<T> is another, but it sound like your instructor really doesn't want you to be thinking of this problem in terms of  arrays, either.

    So, because the instructions say so, and because it's just a good idea, create your queue with something like:

        Queue<Double> ringBuffer = new LinkedList<Double>();

    The actual object is a LinkedList, but the variable type is Queue so it only supports Queue method calls.  I suggest using ringBuffer.add(), ringBuffer.remove() and ringBuffer.element() to add, remove and peek.  Those methods will throw an exception if anything goes wrong.

    The ring buffer update cycle is pretty easy using queue operations.  On each cycle, you'll get two numbers from the queue (only removing the first one), compute a new value, then add that new value to the end.  Since Java guarantees left-to-right evaluation, that could even be a single statement:

        ringBuffer.add(DECAY * 0.5 * (ringBuffer.remove() + ringBuffer.element());

    I'd make that four statements to make debugging easier. You'll need a "static final double DECAY = 0.996;" statement somewhere in your class (not in a method or constructor).   That will let you experiment with different decay factors without finding and updating every one.  A pre-war Martin D-28 guitar probably has a different decay factor than, say, a modern Epiphone PR-150.

    • Commenter avatarLogin to reply the answers
  • EddieJ
    Lv 7
    6 days ago

    Linked lists are hard because everything in life is hard.

    It's similar to crossing the street.  There are so many different kinds of vehicles, and even if you focus on one, like a Toyota Camry, there are different model years and different colors, and the drivers can be different ages, different genders, different heights, different weights.  Some of the drivers are married and some of them aren't.

    When you are crossing the street, you need to keep all of that in mind, along with various weather conditions.

    • Commenter avatarLogin to reply the answers
Still have questions? Get your answers by asking now.