Programming Assignment Checklist: Plucking a Guitar String

Frequently Asked Questions

What are the goals of this assignment? To learn how to create user-defined data types in Java and to learn about digital audio.

Do I need to follow the prescribed API? Yes, we will be testing the methods in the API directly. If your method has a different signature or does not behave as specified, you will lose a substantial number of points. You may not add public methods to the API; however, you may add private instance variables or methods (which are only accessible in the class in which they are declared).

Where do I enter keystrokes in GuitarHeroLite and GuitarHero? Be sure that the standard draw window has focus by clicking in it. Then, type the keystrokes.

How do I determine the length of an array? If you're asking this, you probably need to review Section 1.4.

Is the size of a RingBuffer equal to the number of nonzeros? No. Some of the elements in the buffer can be zero. To get an accurate count, increment the instance variable size each time you call enqueue() and decrement it each time you call dequeue().

How do I throw a RuntimeException? Read the Exceptions paragraph on page 446 and see the examples in (but you'll need to pick your own short description string).

When generating random values between -0.5 and +0.5 in should I include the endpoints? You are free to choose whatever convention you find most convenient. The inclusion or exclusion of the endpoints makes no humanly discernable difference in the sound, but the inclusion of exactly one endpoint makes the implementation of the method very simple.

How do I round a double up to the nearest int? Why must we use this approach? Use the ceiling function static double Math.ceil(double x) and then cast the result to an integer. The reason for using this is that it causes the natural resonance frequency of your guitar string's ring buffer to be as close as possible to the desired one.

What happens if I call where x is greater than 1 or less than -1? The value is clipped—it is replaced by the value 1.0 or -1.0, respectively.

I get an ArrayOutOfBounds or NullPointerException error in RingBuffer. What could cause this? Does your constructor correctly initialize all of the instance variables? Did you allocate memory for your array? Did you inadvertently redeclare an instance variable in a method or constructor, thereby shadowing the instance variable with the same name?

I get a RuntimeException or an out-of-bounds error in GuitarHeroLite before I type any keystrokes. Why? Did you forget to initialize the ring buffer to contain N zeros in your GuitarString constructor?

When I run GuitarHeroLite for the first time, I hear no sound. What am I doing wrong? Make sure you have tested with the main() provided for GuitarString. If that works, it is likely something wrong with pluck() since the main() provided for GuitarString does not test that method. To diagnose the problem, print out the values of sample() and check that they become nonzero after you type lower case characters 'a' and 'c'.

When I run GuitarHeroLite, I hear static (either just one click, and then silence or continual static). What am I doing wrong? It's likely that pluck() is working, but tic() is not. The best test is to run the main() provided for GuitarString.

How do I use keyboard.indexOf(key)? If keyboard is a String and key is a character, then keyboard.indexOf(key) returns the integer index of the first occurrence of the character key in the string keyboard, or –1 if it does not occur. You can read about it in the String Javadoc page.

Can I hardwire the constants 44,100, 110.0, 440.0, 880.0, and 37 in my program? As usual, we will deduct style points if you use an unnamed constant (such as 37) in your program more than once. We recommend using the name SAMPLING_RATE for 44,100 and CONCERT_A for 440. But you need not name all of the constants in the formula 2(i - 24) / 12.

When I run from DrJava, even though I have clicked in the StdDraw window, the program seems to ignore my keystrokes. What is wrong? Try clicking on DrJava, then clicking in the StdDraw window, again. If that does not work, close the StdDraw window and run from Terminal or Command Prompt.


Be sure to thoroughly test each piece of your code as you write it. We offer some suggestions below.

Possible Progress Steps

These are purely suggestions for how you might make progress. You do not have to follow these steps.