Java compareTo method beginner level
Asked Answered
C

5

6

I have an assignment where I need to create a Student class where you store the student's neptun code (String nep_c) and the number of points you have achieved in the exam (int point_num). Prepare a public int getMark () method that returns the ticket obtained to the exam as a function of the score according to the following table:

  • between 100 and 90- > 5;
  • between 90 and 80 -> 4;
  • between 80 and 70 -> 3
  • between 70 and 60 -> 2
  • < 60 -> 1

The class implements the Comparable interface and the compareTo () method sort the students based on the neptun code. Within it, by the number of mark you get. Unfortunately I don't understand comperTo method. Can you help me how can I write the correct code?

public class Student implements Comparable{

private String nep_c;
private int point_num;
private int Mark;

public Student(int point_num, String nep_c) {
    this.point_num = 65;
    this.nep_c= "AAA1BB1";
}

public int getMark(){
    if (point_num <= 100 && point_num > 90)
    Mark = 5;
    else if (point_num <= 90 && point_num > 80)
    Mark = 4;
    else if (point_num <= 80 && point_num > 70)
    Mark = 3;
    else if (point_num <= 70 && point_num > 60)
    Mark = 2;
    else if (point_num <= 60)
    Mark = 1;
    else{
        return 0;
    }
    return Mark;
}

public String getNep_c(){
    return nep_c;
}

public int getPoint_num(){
    return point_num;
}

@Override
public int compareTo (Object o){
    return ???;
}

}

Courageous answered 3/1, 2019 at 12:57 Comment(2)
It helps to read the documentation of Comparable, if you haven't already. This question may also help: stackoverflow.com/questions/21626439Carafe
Note that your Mark field is redundant, and it's dangerous because unless you call getMark(), it's value is wrong.Uniformity
S
7

sort the students based on the neptun code

Two parts. Part one, change

implements Comparable

to

implements Comparable<Student>

And then

@Override
public int compareTo(Student o) {
    return this.nep_c.compareTo(o.nep_c);
}

However, you then say Within it, by the number of mark you get. so perhaps you really want

@Override
public int compareTo(Student o) {
    return Integer.compare(getMark(), o.getMark());
}

If you mean to sort by neptun code, and use mark(s) as a tie-breaker then you could do something like

int c = this.nep_c.compareTo(o.nep_c);
if (c != 0) {
    return c;
}
return Integer.compare(getMark(), o.getMark());

Or, in Java 8+, using Comparator.comparing like

return Comparator.comparing(Student::getNep_c)
        .thenComparingInt(Student::getMark).compare(this, o);
Stet answered 3/1, 2019 at 13:3 Comment(3)
By Within it, by the number of mark you get, I think that Lajos means that if the nep_c are equal, the marks are then compared.Backtrack
@BillyBrown maybe. more added to the answer to try and cover the possibilities.Stet
I did not know about Comparator.comparing; it definitely helps fight the boilerplate. Upvoted your answer, as it's a nice step-by-step guide.Backtrack
M
2

compareTo gets Object because you implement Comparable, rather than generic Comparable<Student>. That is why it is hard to see what needs to be done.

Change your code as follows:

public class Student implements Comparable<Student> {
    ...
    @Override
    public int compareTo(Student other) {
        ...
    }
}

Now inside the implementation compare nep_c of this student to other.nep_c. If these two are not equal, return the result of comparison; otherwise return the result of comparing the marks.

Note: There is an issue with your getMark method: it returns 1 for students with 60 points when it should return 2, and it also assigns private Mark field which could be converted to a local variable.

Metanephros answered 3/1, 2019 at 13:7 Comment(0)
B
2

The compareTo method on a Comparable takes a value to which it compares the current object, and should return:

  • -1 if the current object comes before the other object (any negative integer can be used),
  • 0 if the two objects are equal,
  • 1 if the current object comes after the other object (any positive integer can be used).

If you want to compare two objects by two different fields, you would do the following (make sure to implement Comparable<Student>):

@Override
public int compareTo(Student other) {
    final int comparedNepCode = nep_c.compareTo(other.nep_c);
    if (comparedNepCode == 0) {
        return Integer.compare(getMark(), other.getMark());
    }
    return comparedNepCode;
}

When comparing numbers, subtracting the other from the current one gives an ascending order, so:

  • x < y <=> x - y < 0
  • x = y <=> x - y = 0
  • x > y <=> x - y > 0
Backtrack answered 3/1, 2019 at 13:7 Comment(0)
M
0

See the Java compaterTo withibn Comparable for info

compareTo is a function which should return an integer. The idea is that (for example) any external sorting algorithm uses this to compare one object (of type Student, in your case) with another. Within the sort routine (which you probably won't have written: it's built-in to the colleciton), it needs a way of working out which is 'highest' compared to another Student, and that's what compareTo provides.

So in your case, within compareTo, it's something like ...

return -1 if Object o.point_num < this.point_num
return 1 if Object o.point_num > this.point_num
return 0 if Object o.point_num = this.point_num

(that's not java code by the way, just notes)

Note that compareTo doesn't actually do the sorting. For that you'd need some Collection (a type of array) of Student objects. The Collection object should have a '.sort' method built-in.

As for which kind of collection: Depends on your needs. Google is your friend here :-)

Masefield answered 3/1, 2019 at 13:8 Comment(0)
D
0

The compareTo method is used to compare two objects by specifying what parametres you want to compare in the method body. But I guess you already understood that part.

You will have to return a integer value based on the comparison you are attempting to make. There are 3 cases of return:

  • 0 - If the parametres you are comparing are equal.
  • any positive integer - If the argument is greater than the object you are calling the method from.
  • any negative number - If the argument is less than the object you are calling the method from.

By example, I use -1 if this > argument, 0 if this = argument, 1 if this < argument. The sorting method will then take care of sorting the items based on the return value of the method. Here's an example

Dobbs answered 3/1, 2019 at 13:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.