There is an application to create leaderboards of e-sports competitions. It uses the Score class to represent a score of each player. This class has two fields: player for the player's name and totalScore for that player's total score. To build a leaderboard, the Score objects need to be compared. A Score object is considered bigger than another Score if it's totalScore value is bigger. If totalScore values of two Score objects are the same, such objects must be compared by their player values. See the example below.
Computer scienceProgramming languagesJavaCode organizationObject-oriented programmingClass hierarchiesInterfaces and abstract classes
Comparable
Leaderboard
Report a typo
Sample Input 1:
Ann 162
Zipper 121
Flash 121
CoolDoge 200Sample Output 1:
[Flash=121, Zipper=121, Ann=162, CoolDoge=200]Write a program in Java 17
import java.util.*;
class Score implements Comparable<Score> {
private final String player;
private final int totalScore;
public Score(String player, int totalScore) {
this.player = player;
this.totalScore = totalScore;
}
public String getPlayer() {
return player;
}
public int getTotalScore() {
return totalScore;
}
@Override
public String toString() {
return player + '=' + totalScore;
}
@Override
public int compareTo(Score score) {
// your code here
}
}
// do not change the code below
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<Score> scores = new ArrayList<>();
___
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.
Create a free account to access the full topic
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.