0

I am having trouble with part of this java project. So part of this project is setting notes equal to a frequency. I made a string array of notes and a double array of frequencies but i don't know how to set the string array equal to the double so that when a note is played, it plays to the specific frequency of the double array. If there is a better method of doing this?

double[] freq = {
  16.35, 17.32, 17.32, 18.35, 19.45, 19.45, 20.60, 21.83, 23.12,
  23.12, 24.50, 25.96, 25.96, 27.50, 29.14, 29.14, 30.87
};
String[] notes = {
  "C0", "C#0", "Db0", "D0", "D#0", "Eb0", "E0", "F0", "F#0",
  "Gb0", "G0", "G#0", "Ab0", "A0", "A#0", "Bb0", "B0"
};

here are the two arrays.

jontro
  • 10,241
  • 6
  • 46
  • 71
  • 6
    You should probably use a `map` instead – Grant Foster Feb 25 '19 at 22:49
  • simple: by the index(if done correctly). – kai Feb 25 '19 at 22:50
  • 3
    Definitely read up on how to use a `Map` - https://docs.oracle.com/javase/tutorial/collections/interfaces/map.html - and try out a `HashMap`. – Dawood ibn Kareem Feb 25 '19 at 22:51
  • 1
    @gfos suggestion is the best option for you. – d3n13d1 Feb 25 '19 at 22:52
  • a map is not necessaryly the best idea as it doesn't keep an order which may be important.... especially if you go for sound: e.g. an oktave doubles the frequency. – kai Feb 25 '19 at 22:54
  • 1
    @kai - It depends on the implementation of `Map`. A `TreeMap` holds keys in order according to natural key order (`Comparable`) or a key order defined by a `Comparator`. A `LinkedHashMap` preserves insertion ordering. – Stephen C Feb 25 '19 at 23:02
  • you need a fn like: 0->C 1->C# ... or you calculate the freq. map is good if you want to index the other way round C->0 ... – kai Feb 25 '19 at 23:04
  • He appears to be mapping from String to double. But the question is very unclear. (Joshua ... you should do something about that! The ability to communicate ideas clearly is a job requirement for most IT jobs ....) – Stephen C Feb 25 '19 at 23:11
  • 1
    @kai How could the order possibly matter if all he is doing is lookups? – Dawood ibn Kareem Feb 26 '19 at 00:07
  • see also: https://en.wikipedia.org/wiki/Scale_(music)#Note_names – kai Feb 26 '19 at 06:09

3 Answers3

0

You should create one Java Map object with

key:value

pairs instead of two different Arrays. Here is Java documentation of Map class and it's types: https://docs.oracle.com/javase/8/docs/api/java/util/Map.html

Here are some examples of implementation: https://examples.javacodegeeks.com/java-basics/java-map-example/

But first check what type of Map do you need. Here is well explained difference: Difference between HashMap, LinkedHashMap and TreeMap

Example:

HashMap<String, Double> notesMap = new HashMap<>();
notesMap.put("C0", 16.35); ...

Add all notes and then you can get frequencies by note name, and iterate through all properties of a HashMap.

If it's one-to-many relationship, you should solve this with creating the Note class with name and frequency variables.

Tadija Malić
  • 445
  • 7
  • 26
0

In case the order of the arrays matters, or if your assignment is limited to arrays, this is the solution I would use (although definitely not as efficient as a Map)

noteToTest = scanner.nextLine();

for(x = 0; x < notes.length; x++)
{
     if(noteToTest.equalsIgnoreCase(notes[x])
     {

          System.out.println("The frequency is :" + freq[x]);

     }          
}
geekTechnique
  • 850
  • 1
  • 11
  • 38
0

Besides using a map, the "more type safe" answer: create a Note class, like:

 public class Note {
   private final String name;
   private final double frequency;

   public Note(String name, double frequency) {
     this.name = name; ...

You could then go on and put the play method into class, too.

The next other options would be to use an enum instead of a class, as obviously, the note Note("C0", 16.35) doesn't need to be created more than once ...

Please note: in any case, you want your data to somehow represent that relationship between a name and the corresponding frequency. Your current code is creating that mapping implicitly, based on the same index in two different arrays. And as shown, you can now decide how you can make things more explicit.

GhostCat
  • 137,827
  • 25
  • 176
  • 248