Coursera - Computer Science: Programming With A Purpose

Week 10: Programming Languages - Bar Data Type

A bar aggregates related information (name, value, and category) for use in a bar chart. For example, the first bar drawn in the bar chart represents name = Beijing, value = 672, and category = East Asia. In addition to methods for accessing the individual fields, you will need to sort bars in order of value. Implement the following API:

public class Bar implements Comparable<Bar> {

    // Creates a new bar.
    public Bar(String name, int value, String category)

    // Returns the name of this bar.
    public String getName()

    // Returns the value of this bar.
    public int getValue()

    // Returns the category of this bar.
    public String getCategory()

    // Compare two bars by value.
    public int compareTo(Bar that)

    // Sample client (see below).
    public static void main(String[] args)
}

Sorting arrays of objects. To sort an array of objects, use Java’s Arrays.sort() method. For example, the following code fragment creates an array of 10 bars and sorts them in ascending order of value.

// create an array of 10 bars
Bar[] bars = new Bar[10];
bars[0] = new Bar("Beijing",     22674, "East Asia");
bars[1] = new Bar("Cairo",       19850, "Middle East");
bars[2] = new Bar("Delhi",       27890, "South Asia");
bars[3] = new Bar("Dhaka",       19633, "South Asia");
bars[4] = new Bar("Mexico City", 21520, "Latin America");
bars[5] = new Bar("Mumbai",      22120, "South Asia");
bars[6] = new Bar("Osaka",       20409, "East Asia");
bars[7] = new Bar("São Paulo",   21698, "Latin America");
bars[8] = new Bar("Shanghai",    25779, "East Asia");
bars[9] = new Bar("Tokyo",       38194, "East Asia");

// sort in ascending order by weight
Arrays.sort(bars);

Comparable interface. In order to use a data type with Arrays.sort(), that data type must be comparable. This is Java’s mechanism for specifying a total order among objects of a given type. To make a data type comparable, you must implement the Comparable interface, which involves doing two things:

Corner cases. Handle invalid argument in the following manner:

Note: the above description is copied from Coursera and converted to markdown for convenience

Solution:

public class Bar implements Comparable<Bar> {

    private final String name;
    private final int value;
    private final String category;

    // Creates a new bar.
    public Bar(String name, int value, String category) {
        if ((name == null) || (value < 0) || (category == null)) {
            throw new IllegalArgumentException();
        }
        this.name = name;
        this.value = value;
        this.category = category;
    }

    // Returns the name of this bar.
    public String getName() {
        return name;
    }

    // Returns the value of this bar.
    public int getValue() {
        return value;
    }

    // Returns the category of this bar.
    public String getCategory() {
        return category;
    }

    // Compare two bars by value.
    public int compareTo(Bar that) {
        if (that == null) {
            throw new NullPointerException();
        }
        // sort value in ascending order
        return Integer.compare(value, that.value);
    }

    // Sample client (see below).
    public static void main(String[] args) {
        Bar[] bars = new Bar[3];
        bars[0] = new Bar("bar1", 10, "category1");
        bars[1] = new Bar("bar2", 5, "category1");
        bars[2] = new Bar("bar3", 10, "category1");
        Arrays.sort(bars);
        for (Bar bar : bars) {
            StdOut.println(bar.getName() + ", " + bar.getValue() + ", " + bar.getCategory());
        }
    }
}

Link To: Java Source Code