I am creating a reference class called Chord. It consist of two instance variabl
ID: 3552330 • Letter: I
Question
I am creating a reference class called Chord. It consist of two instance variables-a Duration, which is a double value, indicating in seconds how long I should play my chord and the frequencies which consist of an array of double values that are used in combination to play the chord.
I am having difficulty trying to create the String toString instance method because I am dealing with an array of double values for my reference class as well as a boolean equals instance method. I am unsure how to represent them. Here is what I have so far. I am not sure If I am on the right path. Any HINTS OR SUGGESTIONS ON HOW TO CREATE THESE would be helpful THANKYOU!!
import stdlib.StdAudio;
public class Chords {
public final Double duration;
public final Double[] frequencies;
public Chords(double duration, double...frequencies){
}
public void play(){
final double sliceCount = (int) (StdAudio.SAMPLE_RATE * duration);
final double[] slices = new double[(int) (sliceCount + 1)];
for(int i = 0; i < sliceCount; i++ ){
double result = 0;
for(int j = 0; i < frequencies.length; j++){
result += Math.sin((2 * Math.PI * i * frequencies[j])/StdAudio.SAMPLE_RATE);
}
slices[i] = result/(frequencies.length);
result = 0;
}
StdAudio.play(slices);
}
public double duration(){
return duration;
}
public double[] frequencies(){
final int N = frequencies.length;
final double[] copy = new double[N];
for(int i = 0; i < N; i++)
copy[i] = frequencies[i];
return copy;
}
public String toString(){
String frequencyConversion = Double.toString(frequencies);
}
public boolean equals(Chords that){
if(this.frequencies==that.frequencies) {return true;}
{return false;}
}
}
Explanation / Answer
public class Chords {
public final Double duration;
public final Double[] frequencies;
public Chords(double duration, double... frequencies) {
}
public void play() {
final double sliceCount = (int) (StdAudio.SAMPLE_RATE * duration);
final double[] slices = new double[(int) (sliceCount + 1)];
for (int i = 0; i < sliceCount; i++) {
double result = 0;
for (int j = 0; i < frequencies.length; j++) {
result += Math.sin((2 * Math.PI * i * frequencies[j])
/ StdAudio.SAMPLE_RATE);
}
slices[i] = result / (frequencies.length);
result = 0;
}
StdAudio.play(slices);
}
public double duration() {
return duration;
}
public double[] frequencies() {
final int N = frequencies.length;
final double[] copy = new double[N];
for (int i = 0; i < N; i++)
copy[i] = frequencies[i];
return copy;
}
public String toString() {
String frequencyConversion= "";
int i;
for(i=0;i<frequencies.length;i++){
frequencyConversion += frequencies[i];
frequencyConversion+= ", ";
}
return frequencyConversion;
}
public boolean equals(Chords that) {
if (this.frequencies == that.frequencies) {
return true;
}
{
return false;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.