Implement comparing by additional fields

Report a typo

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).

Sample Input 1:

How to bake an awesome cake?-300
Alice likes pancakes...But who doesn't?-800
Germany wants to win EURO 2020!-500

Sample Output 1:

How to bake an awesome cake? 300
Germany wants to win EURO 2020! 500
Alice likes pancakes...But who doesn't? 800
Write 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!
}
}
___

Create a free account to access the full topic