<< Chapter < Page
  Accessible objected-oriented     Page 11 / 17
Chapter >> Page >
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

Questions & Answers

what is variations in raman spectra for nanomaterials
Jyoti Reply
I only see partial conversation and what's the question here!
Crow Reply
what about nanotechnology for water purification
RAW Reply
please someone correct me if I'm wrong but I think one can use nanoparticles, specially silver nanoparticles for water treatment.
yes that's correct
I think
what is the stm
Brian Reply
is there industrial application of fullrenes. What is the method to prepare fullrene on large scale.?
industrial application...? mmm I think on the medical side as drug carrier, but you should go deeper on your research, I may be wrong
How we are making nano material?
what is a peer
What is meant by 'nano scale'?
What is STMs full form?
scanning tunneling microscope
how nano science is used for hydrophobicity
Do u think that Graphene and Fullrene fiber can be used to make Air Plane body structure the lightest and strongest. Rafiq
what is differents between GO and RGO?
what is simplest way to understand the applications of nano robots used to detect the cancer affected cell of human body.? How this robot is carried to required site of body cell.? what will be the carrier material and how can be detected that correct delivery of drug is done Rafiq
what is Nano technology ?
Bob Reply
write examples of Nano molecule?
The nanotechnology is as new science, to scale nanometric
nanotechnology is the study, desing, synthesis, manipulation and application of materials and functional systems through control of matter at nanoscale
Is there any normative that regulates the use of silver nanoparticles?
Damian Reply
what king of growth are you checking .?
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
Stoney Reply
why we need to study biomolecules, molecular biology in nanotechnology?
Adin Reply
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
what school?
biomolecules are e building blocks of every organics and inorganic materials.
anyone know any internet site where one can find nanotechnology papers?
Damian Reply
sciencedirect big data base
Introduction about quantum dots in nanotechnology
Praveena Reply
what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
Damian Reply
absolutely yes
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!

Source:  OpenStax, Accessible objected-oriented programming concepts for blind students using java. OpenStax CNX. Sep 01, 2014 Download for free at https://legacy.cnx.org/content/col11349/1.17
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Accessible objected-oriented programming concepts for blind students using java' conversation and receive update notifications?