// A class representing a NewsSource object public class NewsSource implements Comparable { private String name; private int subscribers; private double trustRating; // pre: given trust rating must be between 0 and 10 inclusive // and the number of subscribers must be >= 0 // post: Constructs a new NewsSource with the given name, // number of subscribers and trust rating public NewsSource(String name, int subscribers, double trustRating) { this.name = name; this.subscribers = subscribers; this.trustRating = trustRating; } // Returns the String representation of this NewsSource public String toString() { return name + "{" + subscribers + ", " + trustRating + "}"; } // Returns a number < 0 if this NewsSource comes before other // Returns a number > 0 if this NewsSource comes after other // Returns 0 if this NewsSource and the other are the same // NewsSources are ordered first by subscribers (with fewer subscribers coming first), // then trustRating (with more trustworthy sources coming first), // then name (in alphabetical order) public int compareTo(NewsSource other) { // Hunter: Changed the logic slightly to be less nested, but does the same thing // as discussed in class if (this.subscribers != other.subscribers) { return this.subscribers - other.subscribers; // only works for ints } else if (this.trustRating != other.trustRating) { if (this.trustRating < other.trustRating) { return 1; } else { // trustRating > other.trustRating return -1; } } else { // subscribers and trustRating are equal return this.name.compareTo(other.name); } } }