Create a package called SwanSong. It will contain the two files described below.
ID: 663135 • Letter: C
Question
Create a package called SwanSong. It will contain the two files described below.
Create a reference class called Chord. It will contain two instance variables: duration, a double value indicating in seconds how long to play a chord, and an array of double values that are the frequencies combining to make the chord.
The API will contain:
public Chord(double duration, double... frequencies), a constructor taking as parameters the duration and a varargs parameter of frequencies;
public void play(): this method plays the chord using the method StdAudio.play.
public double duration(): this method returns the value of the duration instance variable;
public double[] frequencies(): this method returns a copy of the array of frequencies; look at the Java Arrays library of static methods for one that performs this copy;
public String toString(): this method returns a string representing the chord, which (the string) should contain the duration followed by the list of frequencies;
public boolean equals(Chord that): this method returns true if and only if the two chords have identical frequencies; the durations do not have to be equal;
You will also write a program called PlayChordsFromFile. It reads in a text file containing pairs of lines. Each pair consists of a line with a single double value, which is the duration, followed by a line with a list of space-separated double values that are frequencies. The program should repeatedly read in two lines, create a Chord object from the values, and then place that Chord object on a queue.
To read one line of an arbitrary number of double values and return an array of those values, you may use the code:
String[] inputValues = StdIn.readLine().split("\s+"); double[] frequencies = new double[inputValues.length]; for (int i = 0; i < frequencies.length; i++) { frequencies[i] = Double.parseDouble(inputValues[i]); }
The code assumes you have directed StdIn to read the data from the input file.
After the end of the input has been reached the program should repeatedly dequeue chords from the queue, playing each one, doing so until the queue is empty. Please use the test files in this zip file.
Explanation / Answer
Complete Program:
NOTE: Please refer the text book for the files StdAudio.java, In.java, and Out.java.
// File: Chord.java
package SwanSong;
public class Chord
{
private double duration;
private double[] frequencies;
public Chord(double duration, double... frequencies)
{
this.duration = duration;
this.frequencies = frequencies;
}
public void play()
{
StdAudio.play(frequencies);
}
public double duration()
{
return duration;
}
public double[] frequencies()
{
return frequencies;
}
public String toString()
{
String result = "";
result += "Duration: " + duration + " seconds ";
result += "Frequencies: ";
for(int i = 0; i < frequencies.length; i++)
result += frequencies[i] + " ";
result += " ";
return result;
}
public boolean equals(Chord that)
{
if(frequencies.length != that.frequencies.length)
return false;
for(int i = 0; i < frequencies.length; i++)
{
if(frequencies[i] != that.frequencies[i])
return false;
}
return true;
}
}
--------------------------------------------------------------------
// File: PlayChordsFromFile.java
package SwanSong;
import java.util.*;
public class PlayChordsFromFile
{
public static void main(String[] args)
{
Queue<Chord> queue = new LinkedList<Chord>();
Chord crd;
/* need to use In (reads data from file) instead of StdIn (reads data from console) */
In infile = new In("inData.txt");
/* need to use Out (writes data to file) instead of StdIn (writes data to console) */
Out outfile = new Out("outData.txt");
while(infile.hasNextLine())
{
double chord = Double.parseDouble(infile.readLine());
String[] inputValues = infile.readLine().split("\s+");
double[] frequencies = new double[inputValues.length];
for (int i = 0; i < frequencies.length; i++)
{
frequencies[i] = Double.parseDouble(inputValues[i]);
}
crd = new Chord(chord, frequencies);
queue.offer(crd);
}
while(!queue.isEmpty())
{
crd = queue.poll();
crd.play();
outfile.println(crd);
}
}
}
-------------------------------------------------------------------
Sample Output:
Input file: inData.txt
233.1339
52.0044 11.3598 59.6452 54.1857 55.9388
577.5171
38.6337 79.8969 53.4614 54.8444 42.3273 37.7266 93.2716 63.5461 16.8151 96.9938
296.9058
39.2181 98.5628 35.6388 32.4978 90.9882
230.6745
97.7170 3.0342 67.8219 62.1015
356.6500
72.9656 2.4256 82.1256 46.5611 69.7695 82.8026
246.0736
22.9967 33.1115 87.5413 79.4250 22.9990
255.9294
71.3819 86.4819 98.0656
503.1501
6.8464 64.0920 62.0680 97.0782 12.2702 20.4829 90.0407 17.0441 5.6165 37.8417 89.7694
359.7410
97.1708 57.6514 6.2165 44.1282 95.9336 58.6406
483.3005
92.3671 51.4752 36.3435 55.4621 29.7993 31.4169 96.6194 89.8171
-------------------------------------------------------------------
Output file: outData.txt
Duration: 233.1339 seconds
Frequencies: 52.0044 11.3598 59.6452 54.1857 55.9388
Duration: 577.5171 seconds
Frequencies: 38.6337 79.8969 53.4614 54.8444 42.3273 37.7266 93.2716 63.5461 16.8151 96.9938
Duration: 296.9058 seconds
Frequencies: 39.2181 98.5628 35.6388 32.4978 90.9882
Duration: 230.6745 seconds
Frequencies: 97.717 3.0342 67.8219 62.1015
Duration: 356.65 seconds
Frequencies: 72.9656 2.4256 82.1256 46.5611 69.7695 82.8026
Duration: 246.0736 seconds
Frequencies: 22.9967 33.1115 87.5413 79.425 22.999
Duration: 255.9294 seconds
Frequencies: 71.3819 86.4819 98.0656
Duration: 503.1501 seconds
Frequencies: 6.8464 64.092 62.068 97.0782 12.2702 20.4829 90.0407 17.0441 5.6165 37.8417 89.7694
Duration: 359.741 seconds
Frequencies: 97.1708 57.6514 6.2165 44.1282 95.9336 58.6406
Duration: 483.3005 seconds
Frequencies: 92.3671 51.4752 36.3435 55.4621 29.7993 31.4169 96.6194 89.8171
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.