43.2k views
2 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.

Output each floating-point value with two digits after the decimal point, which can be achieved as follows:
System.out.printf("%.2f", yourValue);

Ex: If the input is:

440.0
(which is the A key near the middle of a piano keyboard), the output is:

440.00 466.16 493.88 523.25 554.37
Note: Use one statement to compute r = 2(1.0/12.0) using the pow function. Then use that r in subsequent statements that use the formula fn = f0 * rn with n being 1, 2, 3, and finally 4.

import java.util.Scanner;

public class LabProgram {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);

/* Type your code here. */

}
}

2 Answers

2 votes

The completed Java code for the problem is shown below

java

import java.util.Scanner;

public class LabProgram {

public static void main(String[] args) {

Scanner scnr = new Scanner(System.in);

double f0 = scnr.nextDouble();

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

System.out.printf("%.2f ", f0);

for (int i = 1; i <= 4; i++) {

double fn = f0 * Math.pow(r, i);

System.out.printf("%.2f ", fn);

}

System.out.println(); // to move to the next line after printing the frequencies

}

}

User Jeremy Dentel
by
3.1k points
4 votes

Answer:

The code is below and the sample output is attached.

Step-by-step explanation:

import math

#Implementation of RaiseToPower method

def RaiseToPower():

#r = 2^(1/12)

r = math.pow(2,1/12)

#Get the input from user

#a key has a frequency ,sy f0

f0 = float(input('Enter f0 : '))

#Display statement

print(f0,end=' ')

#Iterate the loop up to

#next 4 higher key frequencies

for n in range(1,5):

#calculate the fn value

fn = f0 * math.pow(r,n)

#Display statement

print(fn,end=' ')

#call RaiseToPower method

RaiseToPower()

On a piano, a key has a frequency, say f0. Each higher key (black or white) has a-example-1
User Timedt
by
3.6k points