/** * The keyboard class allows storage of and access to facts about * a particular keyboard. * */ public class Keyboard { int noofKeys, currentKey; char zeroLetter; int zeroOctave; //Where did we start out? public static void main(String args[]) { //Unit test: Keyboard k = new Keyboard('C', 4); // System.out.println(k.getDistanceTo('C', 2) ); // System.out.println(k.getDistanceTo('A', 4) ); // System.out.println(k.getDistanceTo('G', 3) ); } public Keyboard(char topLetter, int topOctave) { noofKeys = 1; //Where we start out. currentKey = 0; // as it turns out, the keyboard starts at key 0 and // goes down from there... This is a bizarre design decision, but // it works. this.zeroLetter = topLetter; this.zeroOctave = topOctave; } public int getNoofKeys() { return noofKeys; } public void moved(int noofSteps) { currentKey += noofSteps; //KEYS RUN FROM -(N-1) TO 0 if (currentKey < (1 - noofKeys)) noofKeys = (1 - currentKey) ; } public int getCurrentKey() { return currentKey; } public void setCurrentKey(int ck) { currentKey = ck; } public int getKeyNum(char letter, int octave) { int smalldist = letter - zeroLetter; int bigdist = (octave - zeroOctave)*7; return bigdist + smalldist; } public int getDistanceTo(char letter, int octave) { int keynum = getKeyNum(letter, octave); return (keynum - currentKey); } /** * * Oops, we're back at the edge of the keyboard. * * */ public void atEdge(boolean forward) { if (forward) currentKey = 0; else { currentKey = -1 * (noofKeys - 1); } } }