120k views
15 votes
On a piano, a key has a frequency, say f0. Each higher key (black or white) has a frequency of f0 * rn, where n is the distance (number of keys) from that key, and r is 2(1/12). Given an initial key frequency, output that frequency and the next 4 higher key frequencies. Ex: If the input is 440 (the A key near the middle of a piano keyboard), the output is: 440 466.1637615180899 493.8833012561241 523.2511306011974 554.3652619537443 Note: Use one statement to compute r = 2(1/12). Then use that r in subsequent statements that use the formula fn = f0 * rn with n being 1, 2, 3, and finally 4. Note: Import the math module as part of your code to help you compute r = 2(1/12)

1 Answer

3 votes

Answer:

Program in Java:

import java.util.Scanner;

import java.lang.Math;

public class MyClass {

public static void main(String args[]) {

Scanner input = new Scanner(System.in);

float f0; double r=0.0;

System.out.print("Initial Key Frequency: ");

f0 = input.nextFloat();

int numkey = 1;

System.out.print(f0+" ");

r= Math.pow(2,(1.0/12.0));

while(numkey<=4){

f0*=r;

System.out.print(f0+ " ");

numkey++;

}

}

}

Explanation:

This line declares f0 as float and r as double. f0 represents the initial key frequency

float f0; double r=0.0;

This line prompts the user for initial key frequency

System.out.print("Initial Key Frequency: ");

This line gets the initial key frequency from the user

f0 = input.nextFloat();

This declares numkey as integer and also initializes it to 1

int numkey = 1;

This prints the initial key

System.out.print(f0+" ");

This calculates r

r= Math.pow(2,(1.0/12.0));

The following iteration calculates and prints the next 4 key frequencies

while(numkey<=4){

f0*=r;

System.out.print(f0+ " ");

numkey++;

}

}

}

User GuD
by
4.6k points