Search code examples
javacomparablecompareto

I need help to correctly implement the Comparable interface


I have a builded a class called Melody that reads the following lines from a file and stores them into an array called notes of type Note. The columns are: Time in 1/100 second ticks, Note Number, Velocity, Length.

0 60 100 24
25 72 100 24
100 60 100 24
50 60 100 24
75 72 100 24

For my course work I need to implement the Comparable interface. I need to make my Note class Comparable, so that notes are ordered based on time,and if two notes occur at the same time, then I need to put the note with the lower note number first. I need some help as I have got stuck trying to correctly implement the comparaTo method and to print out the result. I would appreciate any help, thanks in advance.

This is my Note class

public class Note implements Comparable <Note> {  

    private int time;
    private int noteNumber;
    private int velocity;
    private int length;

    public Note (int time,int noteNumber,int velocity,int lenght){

        this.time = time;
        this.noteNumber = noteNumber;
        this.velocity = velocity;
        this.length = lenght;  
    }

    public String toString()
    {

        String s =  time +" "+ noteNumber + " " + velocity + " " + length;

        return s;   
    } 

    @Override
    public int compareTo(Note o) {

        return Integer.compare(time, o.time);
    }
} 

This is my Melody class

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;

public class Melody  {

    Note [] notes = new Note[5];

    public Melody() throws IOException{

        FileReader fr = new FileReader ("/Users/enricomomo/Desktop/Text/file2.txt");
        BufferedReader br = new BufferedReader (fr); //Info stored into a buffer

        String line = br.readLine(); //Lines are read from the buffer and stored into a variable

        int lineCounter = 0;

        while  ( line != null )//When reached the end of the file the while loop stops 

        { 

            StringTokenizer st = new StringTokenizer(line, " ");

            int time = Integer.parseInt(st.nextToken());
            int noteNumber = Integer.parseInt(st.nextToken());
            int velocity = Integer.parseInt(st.nextToken());
            int length = Integer.parseInt(st.nextToken());

            System.out.println(line + " " + lineCounter);

            Note n = new Note(time,noteNumber,velocity,length);

            notes[lineCounter] = n;

            line = br.readLine();

            lineCounter ++;
        }

        br.close(); 
    }

    public void contet(){

        for ( int i = 0; i < notes.length; i ++)
        {

            System.out.println(notes[i]);                    
        }
    }

    public String toString()
    {
         String rtn = "";

         //Code to create a String version of the object 

         for ( int i = 0; i < notes.length; i ++)
            {

                rtn += "\n"; 
            }

         return rtn; 
    }
}    

And this is my Test class

import java.io.IOException;

public class Test {

    public static void main(String[] args) throws IOException {

        Melody m = new Melody();

        System.out.print(m);

        m.contet();
    } 
}

Solution

  • Since time and noteNumber are int:

    @Override
    public int compareTo(Note o) {
        if (time == o.time)
            return Integer.compare(noteNumber, o.noteNumber);
        else
            return Integer.compare(time, o.time);
    }
    

    compareTo needs to return a negative, a zero or a positive number,
    depending whether the 1st item is less, equal or greater than the 2nd.
    After you fill the array of Note objects call:

    Arrays.sort(notes);