programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# is there an easier way to write this?

niraj pateI
Greenhorn
Posts: 5
public void Calculate()
{
frequency[0] = middleC;
frequency[1] = middleC * (Math.pow(2,(0.0833)));
frequency[2] = frequency[1] * (Math.pow(2,(0.0833)));
frequency[3] = frequency[2] * (Math.pow(2,(0.0833)));
frequency[4] = frequency[3] * (Math.pow(2,(0.0833)));
frequency[5] = frequency[4] * (Math.pow(2,(0.0833)));
frequency[6] = frequency[5] * (Math.pow(2,(0.0833)));
frequency[7] = frequency[6] * (Math.pow(2,(0.0833)));
frequency[8] = frequency[7] * (Math.pow(2,(0.0833)));
frequency[9] = frequency[8] * (Math.pow(2,(0.0833)));
frequency[10] = frequency[9] * (Math.pow(2,(0.0833)));
frequency[11] = frequency[10] * (Math.pow(2,(0.0833)));
frequency[12] = frequency[11] * (Math.pow(2,(0.0833)));
frequency[13] = frequency[12] * (Math.pow(2,(0.0833)));

}

fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Do you know how to write loops yet?

niraj pateI
Greenhorn
Posts: 5
fred rosenberger wrote:Do you know how to write loops yet?

yes

fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
That was really a hint, more than an actual question.

Any time you see a hunk of code doing the same thing over and over and over, with the only difference being the index of an array, you should think "How can I write this using a loop of some kind".

Note that this line:

could have been written like this:

Campbell Ritchie
Marshal
Posts: 56536
172
It isn't 0.0833. Use 1.0 / 12.0. Don't call pow twelve times. Call it once and get a double value for the twelfth root of 2.
I thought 0.0833 would be dreadfully imprecise, but using that twelve times comes to 1.9995... I was mistaken.
Where do you get frequency[13] from?