# 0.10 Jbs2060-a player piano simulator  (Page 11/17)

 Page 11 / 17
Listing 16 . Beginning of the getPiano method.
```Hashtable getPiano(){Hashtable piano = new Hashtable();double factor = 1.05946309436;//12th root of 2//Define the name and frequency of the first note. double freq = 110;//Frequency of A2 at 110 Hz. Start with this.String note = "A2";//Name of note at 110 Hz. Start with this.//Used to parse a note into 3 single-character substrings such // as C, 5, and #.String sub1 = null; String sub2 = null;String sub3 = null;```

The first half of the code in Listing 16 is completely straightforward.

The number in the center of the note names (such as A3#) changes at C and not at A. Also, the # character appears on some notes andnot on all notes. As a result, it is difficult to use a loop structure to deal with the names of the notes.

I will decompose each note into two or three substrings for processing. In other words, the note name "A2" will be decomposed into an"A" and a "2". The note name "C5#" will be decomposed into a "C", a "5", and a "#". The last three variables that are declared in Listing 16 will be used to save those strings.

## Store the current note and compute the frequency of the next note

The process of populating the Hashtable object with note names and frequencies is controlled by a for loop that begins in Listing 17 .

Listing 17 stores the name and frequency for the current note and computes the frequency of the next note. ( The initial name and frequency for the current note were defined in Listing 16 .) The frequency of the next note is computed by multiplying the frequency of the current note bythe twelfth root of two, which is stored in the variable named factor.

Listing 17 . Store the current note and compute the frequency of the next note.
```for(int cnt = 0;cnt<61;cnt++){ //This loop counts from A1 through A7 at 3520 Hz. //Store the name and the frequency of the note in the next element in// the hashtable. piano.put(note,freq);//Compute the frequency of the next notefreq *= factor;```

## Construct the name of the next note

While it may be possible to write a loop structure to deal with note names, it was not obvious to me how to do that. Therefore, I took a brute forceapproach and wrote a long and tedious block of logic code to do the job.

Listing 18 shows the logic used to decompose the name of the current note into two or three single-character strings and to use those strings to constructthe name of the next note. That note will be the current note at the top of the next iteration and will be saved, along with the frequency in the Hashtable object. I will leave it as an exercise for the student to understand and possibly improve on this logic.

Listing 18 . Construct the name of the next note.
```//Use logic to construct the name of the next note in a sequence such // as A2,A2#,B2,C3,C3#,D3,D3#,E3,F3,F3#,G3,G3#,A3, etc.//Begin by parsing the current note name into three single-character // substrings, the third of which may be null.if(note.length() == 3){ sub1 = note.substring(0,1);sub2 = note.substring(1,2); sub3 = note.substring(2,3);}else{ sub1 = note.substring(0,1);sub2 = note.substring(1,2); sub3 = null;}//end if//Use the three substrings of the current note name to determine the // name of the next note. This is long and tedious but it works.if((sub1.equals("A"))&&(sub3 == null)){ sub1 = "A";sub3 = "#"; }else if((sub1.equals("A"))&&(sub3.equals("#"))){ sub1 = "B";sub3 = null; }else if((sub1.equals("B"))){sub1 = "C"; sub3 = null;//Increment the number sub2 = "" + (1 + Integer.parseInt(sub2));}else if((sub1.equals("C"))&&(sub3 == null)){ sub1 = "C";sub3 = "#"; }else if((sub1.equals("C"))&&(sub3.equals("#"))){ sub1 = "D";sub3 = null; }else if((sub1.equals("D"))&&(sub3 == null)){ sub1 = "D";sub3 = "#"; }else if((sub1.equals("D"))&&(sub3.equals("#"))){ sub1 = "E";sub3 = null; }else if((sub1.equals("E"))){sub1 = "F"; sub3 = null;}else if((sub1.equals("F"))&&(sub3 == null)){ sub1 = "F";sub3 = "#"; }else if((sub1.equals("F"))&&(sub3.equals("#"))){ sub1 = "G";sub3 = null; }else if((sub1.equals("G"))&&(sub3 == null)){ sub1 = "G";sub3 = "#"; }else if((sub1.equals("G"))&&(sub3.equals("#"))){ sub1 = "A";sub3 = null; }else{System.out.println("Can't reach this point."); }//end else//Construct the next note from the updated substrings. if(sub3 == null){note = sub1 + sub2; }else{note = sub1 + sub2 + sub3; }//end if}//end for loop return piano;}//end getPiano }//end class PlayerPiano01```

what is an electronic
what is solubility?
what is covalent bonding
a covalent bond is a chemical bond that involves the sharing of electron pairs between atoms.
Yvette
what is isotope
Why the moves at a constant velocity is all packages the force of balanced
what is covalent bond
how do u draw a vector
What is a vector
vector is anything that has both a direction and a magnitude .they are usually drawn as pointed arrows ,the length of which represents a vector's magnitude
Tetteh
what is electronics?
Edward
how to calculate the reading on voltmeter or ammeter
why is HCl considered a strong acid
it dissociate almost completely
Tetteh
what is metal displacement
what is an electric field?
is the charge of an electron always 1,6 ×10^-19? and the mass is always 9,1×10^-13?
how to calculate a distance between charges
juss apply the formula of the Electrostatic force
Noluthando
please what's the simplest way to derived Schrodinger's wavelength equation
Maimo
how to show polarity
Got questions? Join the online conversation and get instant answers!

#### Get Jobilize Job Search Mobile App in your pocket Now! By Jazzycazz Jackson By OpenStax By John Gabrieli By Lakeima Roberts By Angela January By Mary Matera By Jonathan Long By OpenStax By OpenStax By JavaChamp Team