Write a reference class called Song. It represents a simple song consisting of t
ID: 3828430 • Letter: W
Question
Write a reference class called Song. It represents a simple song consisting of two sequences, one of durations and one offrequencies. 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 play Tone 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 ava 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 mymarch.txt file into your Eclipse data folder.Explanation / Answer
class Song
{
//Creating a ArrayList of frequencies and durations
private ArrayList<double> frequency;
private ArrayList<double> duration;
public Song()
{
frequency=new ArrayList<double>(100);
duration=new ArrayList<double>(100);
}
private void playTone(double frequency,double duration)
{
//Just paste the code for playTone method from your earlier assignment
//Then execute the code.
}
//Add frequency
public void addFrequency(double frequency)
{
this.frequency.add(frequency);
//this.frequency=frequency;
}
//Add the duration
public void addDuration(double duration)
{
this.duration.add(duration);
}
//Returns the frequency
public double getFrequency(int index)
{
return frequency.get(index);
}
//Returns the duration
public double getDuration(int index)
{
return duration.get(index);
}
public void play()
{
//Calling the playTone private method on each indivisual frequencies and durations
for(int i=0;i<frequency.size();i++)
playTone(frequency.get(i),duration(i));
//playTone(getFrequency(),getDuration());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.