Create a java program using the following requirements The numbers below are fro
ID: 3828017 • Letter: C
Question
Create a java program using the following requirements
The numbers below are from the march.text
Write a reference class called Song. It represents a simple song consisting of two sequences, one of durations and one of frequencies. Here is its API: public Song (): this constructor creates a Song object with empty duration and frequency sequences. public void addFrequency (double frequency): this method adds a frequency to the sequence of frequencies. public void addDuration (double duration): this method adds a duration to the sequence of durations. public void play (): this plays the sequence of durations and frequencies. It does this by indexing through both sequences, calling the playTone method on each individual duration and frequency. In addition to those public methods above, place in your class a private version of the playTone method from an earlier assignment. Its first line will be: private void playTone(double frequency, double duration) {Test your class by placing my PlaySong.java program into your song package. My program creates a Song object, reads a series of durations and frequencies from a file into it, and then calls the play () method. You will also need to copy my march. txt file into your Eclipse data folder. import stdlib.StdIn; public class PlaySong {public static void main(String[] args) {Song song = new Song(); StdIn.fromFile("data/march.txt"); while (!StdIn.isEmpty()) {double duration = StdIn.readDouble(); double frequency = StdIn.readDouble(); song.addDuration(duration); song.addFr|equency(frequency);} song.play();} 0.25 195.998 0.10 0 0.25 195.998 0.10 0 0.25 195.998 0.10 0 0.1875 164.814 0.10 0 0.1875 233.082 0.10 0 0.25 195.998 0.10 0 0.1875 164.814 0.10 0 0.09 233.082 0.10 0 0.50 195.998 0.10 0 0.25 293.665 0.10 0 0.25 293.665 0.10 0 0.25 293.665 0.10 0 0.1875 329.628 0.10 0 0.1875 233.082 0.10 0 0.25 184.997 0.10 0 0.1875 164.814 0.10 0 0.1875 233.082 0.10 0 0.50 207.652 0.10 0Explanation / Answer
import java.util.ArrayList;
import java.util.List;
public class Song {
//Sequence of frequencies
private List<Double> frequencyList = new ArrayList<>();
//Sequence of durations
private List<Double> durationList = new ArrayList<>();
public void addFrequency(double frequency){
frequencyList.add(frequency);
}
public void addDuration(double duration){
durationList.add(duration);
}
public void play(){
for(int i=0; i < frequencyList.size() && i < durationList.size(); i++){
playTone(frequencyList.get(i), durationList.get(i));
}
}
//TODO please add playTone method from your earlier assignment for which no description is being provided in the question
private void playTone(double frequency, double duration){
System.out.println("Frequency = "+ frequency + ", duration = "+duration);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.