In the example below, you can see our class Article. It has two fields: title and size. You should implement comparing articles by their size, and if their sizes are equal, compare them by title (according to the lexicographical order).
Computer scienceProgramming languagesJavaCode organizationObject-oriented programmingClass hierarchiesInterfaces and abstract classes
Comparable
Implement comparing by additional fields
Report a typo
Sample Input 1:
How to bake an awesome cake?-300
Alice likes pancakes...But who doesn't?-800
Germany wants to win EURO 2020!-500Sample Output 1:
How to bake an awesome cake? 300
Germany wants to win EURO 2020! 500
Alice likes pancakes...But who doesn't? 800Write a program in Java 17
class Article implements Comparable<Article> {
private String title;
private int size;
public Article(String title, int size) {
this.title = title;
this.size = size;
}
public String getTitle() {
return this.title;
}
public int getSize() {
return this.size;
}
@Override
public int compareTo(Article otherArticle) {
// add your code here!
}
}
___
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.