0

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 an 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, 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 the 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());
    }
}
  • First if I print out all the pitches in a loop, as shown above, I get the values shown below as an output. But what I want is for each pitch to have a different value that it gets from the pitchesRaw

    106.4 106.4 106.4 106.4 106.4 106.4 106.4 106.4 106.4 106.4 106.4 106.4 106.4 106.4 106.4 106.4 106.4

  • 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?

  • ThirdlyI 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?

Robo Mop
  • 3,485
  • 1
  • 10
  • 23
Richard
  • 1,087
  • 18
  • 52
  • 1
    Each time you call `makeListOfPitches(pitch, pitchString);`, you are passing *the same Pitch object.* So you are adding multiple references to the *same object* to your List, over and over. You need to add a brand new Pitch object with each iteration of your loop. – VGR Mar 20 '18 at 13:52
  • For such simple code, no, an interface is not needed. You typically don't define an interface for a single class, but when you want common behavior across many – OneCricketeer Mar 20 '18 at 13:54
  • @VGR oh.. true, once I added it inside of the loop in function `getListOfPitches()`, it made different pitches. Thank you – Richard Mar 20 '18 at 13:56
  • Remove `Pitch pitch = new Pitch();` from the fields of `Foo` – OneCricketeer Mar 20 '18 at 13:56
  • A decision on when to use interface should be made on whether you have different behaviors for classes that implement it. If you don't, I see no reason for an interface. – duffymo Mar 20 '18 at 13:56
  • Also, actually use the return variables of your methods. Don't use methods other than setters to assign fields – OneCricketeer Mar 20 '18 at 13:57
  • You'll know once you understand exactly what an interface is – Stultuske Mar 20 '18 at 13:57
  • @cricket_007 as I do not plan on adding more functions to the interface then I will just remove it, thank you for answering. – Richard Mar 20 '18 at 13:58

1 Answers1

1

To answer the last part of your questions, using interfaces here is neither good or bad, you are essentially creating an interface for your getters and setters, whether that's necessary depends on how you want to use the interface. It looks like it's not necessary in your case. Take a look at Java Interface Usage Guidelines -- Are getters and setters in an interface bad?

Regarding your second question, you probably also don't need a separate class for instantiating lists of pitches, does word actually have meaning and will be instantiated elsewhere as objects? If you just want a list, simply do what you have done above List<Pitch> word = new ArrayList<Pitch>();, and simply add the pitches to the list.

hysoftwareeng
  • 497
  • 4
  • 15
  • Alright thank you for your answer, I will remove it then since I have no further plans of adding more functions. – Richard Mar 20 '18 at 13:57
  • `word` does not have a meaning itself so yes I will do as you said. Thank you a lot – Richard Mar 20 '18 at 14:14