The Java Program: Elephant.java

  1 import java.util.ArrayList;
  2 import java.util.Collections;
  3 import java.util.Scanner;
  4 
  5 final class Elephant  {
  6 
  7    private static int count=0;
  8    public final int index, weight, IQ;
  9 
 10    private Elephant (int i, int w, int s) { index=i; weight=w; IQ=s; }
 11 
 12    // Elephants are indexed starting with 0, 1, 2, ...
 13    public  Elephant (int w, int s)        { this (count++, w, s); }
 14 
 15    public boolean heavierAndDumberThan (final Elephant e) {
 16       return weight > e.weight && IQ < e.IQ;
 17    }
 18 
 19    public String toString () {
 20       return String.format ("%03d [%d,%d]", index, weight, IQ);
 21    }
 22 
 23    public static void main (final String[] args) {
 24       final Scanner scan = new Scanner (System.in);
 25 
 26       // Read in elephants from standard input; add to collection
 27       final ArrayList<Elephant> list = new ArrayList<Elephant> ();
 28       read: while (scan.hasNextInt()) {
 29          list.add (new Elephant (scan.nextInt(), scan.nextInt()));
 30       }
 31       System.out.println (list);
 32    }
 33 
 34 }