class Tank { // Does provide for a tank of some fluid // Attributes private int capacity; private int level; public Tank (int c) { capacity = c; level = 0; }//end constructor Tank public int theLevel() { // Does return the level in this tank return level; }//end function theLevel public void add (int amount) { // Does add an amount to this tank // Check no overflow level += amount; }//end routine add fluid public void sub (int amount) { // Does subtract amount from tank // Check tank does contain enough level -= amount; }//end routine subtract amount public void fill () { // Does fill up this Tank to capacity this.level = capacity; }//end fill public void empty() { // Does empty out this tank level = 0; }//end routine empty public void pourFrom (Tank s, int amount) { //Does pour an amount from tank s to this tank // Check s.sub (amount); add (amount); }//end routine pour an amount from s into this public boolean isEmpty () { // Does tell if this tank is empty return (level == 0); }//end routine isEmpty public static void main (String [] args) { // Does test the Tank class Tank small, large; small = new Tank (4); large = new Tank (7); // Fill the smaller one small.fill(); System.out.println ("Small now has " + small.theLevel() ); System.out.println ("Large now has " + large.theLevel() ); // Pour it into the larger one large.pourFrom (small, 4); System.out.println ("Small now has " + small.theLevel() ); System.out.println ("Large now has " + large.theLevel() ); // Fill smaller one again, and pour into larger till filled small.fill(); while ( large.theLevel() < large.capacity ) //not pure!! large.pourFrom (small, 1); System.out.println ("Small now has " + small.theLevel() ); System.out.println ("Large now has " + large.theLevel() ); // Empty larger one and pour the smaller into it large.empty(); while ( small.theLevel() > 0) large.pourFrom(small,1); System.out.println ("Small now has " + small.theLevel() ); System.out.println ("Large now has " + large.theLevel() ); // Fill the smaller one and pour it into the larger small.fill(); while ( small.theLevel() > 0) large.pourFrom(small,1); System.out.println ("Small now has " + small.theLevel() ); System.out.println ("Large now has " + large.theLevel() ); // The larger one finally has 5 unit in it. }//end routine main }//end class Tank class CountSort { // Does provide a sort (ranking by counting) public static void countSort (int[] theArray) { // Does sort by counting number of values greater than each int value; int num = theArray.length; for (int i = 0; i < num; i++) { value = theArray[i]; System.out.print (value + " "); System.out.println (bigCount (theArray, value) ); }//end for }//end routine countSort public static int bigCount (int[] arr, int val) { // Does count ... int count = 0; for (int i = 0; i < arr.length; i++) if (val > arr[i]) count ++; return count; }//end function bigCount public static void main (String[] args) { // Does test int intArray[] = { 0, 1,2,3,4,5,9,8}; countSort (intArray); }//end routine main }//end class CountSort class Till { // Does provide cash inventory of coins // Attributes: coin denominations private int pennies; private int nickels; private int dimes; private int quarters; public Till (int p, int n, int d, int q) { pennies = p; nickels = n; dimes = d; quarters= q; }//end constructor Till public boolean areEqual (Till that) { // Does tell if this till equals that in detail if ( (this.pennies == that.pennies ) && (this.nickels == that.nickels ) && (this.dimes == that.dimes ) && (this.quarters== that.quarters) ) return true; else return false; }// end Function areEqual public int worth () { return pennies + 5*nickels + 10*dimes + 25*quarters; }//end Function worth public boolean areEquiv (Till that ) { // Does tell if worth of this till equals that one return (this.worth() == that.worth() ); }// end function areEquivalent public static void main (String args[]) { // Does test Tell class Till mine, yours; mine = new Till (4,2,1,3); yours = new Till (4,4,0,3); if (mine.areEqual (yours)) System.out.println ("equal"); else if (mine.areEquiv( yours)) System.out.println ("equivalent"); else System.out.println ("different"); }//end routine main }//end class Till