I was just doing practice on Hackerrank since I'm still pretty new to Java (I'm only experienced with C and C++, with minimal Python/Matlab/C#). Basically we only had to write the "Checker class" below from scratch. However, I noticed that when I add public to the Checker class it results in runtime error. Does anyone know why? I couldn't find any answers on this online.
Also, yes, I know access modifiers restrictions on how much they can have access to the scope of classes, but it does not make sense to me on how a default class cannot access a public class's method. I'm assuming it is perhaps I'm implementing a parent class that's causing the problem? Here is the RE message I receive on Hackerrank:
Error: Main method not found in class Checker, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
If interested, link to the practice problem for reference: https://www.hackerrank.com/challenges/java-comparator/problem
import java.util.*;
// Write your Checker class here
class Checker implements Comparator<Player>{ //If I add "public" in front I get RE
@Override
public int compare(Player A, Player B){
if(A.score == B.score)
return A.name.compareTo(B.name);
else
return B.score - A.score;
// return A.compareTo(B);
}
}
class Player{
String name;
int score;
Player(String name, int score){
this.name = name;
this.score = score;
}
}
class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
Player[] player = new Player[n];
Checker checker = new Checker();
for(int i = 0; i < n; i++){
player[i] = new Player(scan.next(), scan.nextInt());
}
scan.close();
Arrays.sort(player, checker);
for(int i = 0; i < player.length; i++){
System.out.printf("%s %s\n", player[i].name, player[i].score);
}
}
}