Since I have studied programming on my own I thought I should start creating proper classes and variables but I dont seem to quite get it.
I have read that first I need and Interface so I made mine look like this:
public interface PitchInterface {
void setOccuranceTime(float occuranceTime);
float getOccuranceTime();
void setPitch(float pitch);
float getPitch();
}
Secondly I made a class that implements
the Interface
:
public class Pitch implements PitchInterface{
float occuranceTime;
float pitch;
@Override
public void setOccuranceTime(float occuranceTime) {
this.occuranceTime = occuranceTime;
}
@Override
public float getOccuranceTime() {
return occuranceTime;
}
@Override
public void setPitch(float pitch) {
this.pitch = pitch;
}
@Override
public float getPitch() {
return pitch;
}
}
Now I need to give values to Pitch it and this is where I am stuck at the moment.
public class Foo {
Pitch pitch = new Pitch();
String[] pitches;
List<Pitch> listOfPitches = new ArrayList<Pitch>();
List<String> wordsList = new ArrayList<String>();
public List<Pitch> getListOfPitches(){
getPitches();
for (String pitchString: pitches) {
makeListOfPitches(pitch, pitchString);
}
return listOfPitches;
}
public void makeListOfPitches(Pitch pitch, String pitchString){
pitch.setPitch(getPitchesInfo(pitchString, 0));
pitch.setOccuranceTime(getPitchesInfo(pitchString, 1));
listOfPitches.add(pitch);
}
public List<String> getWordsList(){
//To-do make words out of Pitches and add them to list
return wordsList;
}
public String[] getPitches() {
pitches = pitchesRaw.split("\\r?\\n");
return pitches;
}
private float getPitchesInfo(String pitch, int position){
String[] frequencyAndTime = pitch.split("\\:");
if(position == 0){
return Float.parseFloat(frequencyAndTime[0].replace(',', '.'));
}
if(position == 1){
return Float.parseFloat(frequencyAndTime[1].replace(',', '.'));
}
else return 0;
}
String pitchesRaw = "0,14:23281,61\n" +
"0,23:53,65\n" +
"0,37:72,53\n" +
"0,56:86,09\n" +
"0,60:88,58\n" +
"0,65:87,45\n" +
"0,70:87,11\n" +
"0,74:89,56\n" +
"0,79:96,22\n" +
"0,84:23288,24\n" +
"0,88:103,92\n" +
"0,93:107,46\n" +
"0,98:108,02\n" +
"1,02:107,51\n" +
"1,07:104,92\n" +
"1,11:105,94\n" +
"1,16:106,40\n";
}
And finally to initialize it from some main class:
public static void main(String[] args) {
Foo listOfWordsAndPithes = new Foo();
List<Pitch> list = listOfWordsAndPithes.getListOfPitches();
for (Pitch pitch: list) {
System.out.println(pitch.getPitch());
}
}
I am not sure if I am asking for too much but I would like to understand how the classes
work properly in Java and if I understood the classes and interfaces the correct way. Also how to make new Pitches
, get them
and iterate
over them
EDIT: Edited the Foo
class as was suggested in comments and it gives no errors now. There are still two things I dont seem to understand.
First
if I print out all the pitches that I should have it only gives me the pitch of the last String added but they all should be different.
Secondly
if I need to make a words list
out of pitches by adding multiple Pitches
into one list (for example add 6 different Pitches into 1 list) and then add those Pitches
into another list that would be the first element of a new list. Then do I need to make another class
called Word
and define there what word is and how to set/get it?
Thirdly
I read from the comments that using Interface here is not needed. When do I know if it is OK to use an Interface and when not?