Source of ThreePillars.java


  1: //ThreePillars.java
  2: //Simple demo of encapsulation, inheritance and polymorphism,
  3: //the "three pillars" of object-oriented programming.
  4: //These comment lines play no part in the documentation
  5: //created by the javadoc utility.

  7: import java.io.*;

  9: /**
 10:  * A driver class for the Worker and Executive classes.
 11:  */
 12: public class ThreePillars
 13: {
 14:     static BufferedReader keyboard =
 15:         new BufferedReader(new InputStreamReader(System.in));
 16:     static PrintWriter screen =
 17:         new PrintWriter(System.out, true);


 20:     /**
 21:      * A utility function that causes a "pause" in the output.
 22:      * The display of output on the standard output pauses
 23:      * and waits for the user to press Enter before continuing.
 24:      * @throws IOException
 25:      */
 26:     public static void pause()
 27:     throws IOException
 28:     {
 29:         screen.print("Press Enter to continue ... ");
 30:         screen.flush();
 31:         keyboard.readLine();
 32:     }


 35:     /**
 36:      * Displays weekly gross wage. The weekly gross
 37:      * wage of the person whose name is supplied as
 38:      * the input parameter is displayed on the standad
 39:      * output.
 40:      * @param person Name of the person whose weekly
 41:      *               gross wage is desired.
 42:      */
 43:     public static void displayGrossWage(Worker person)
 44:     {
 45:         screen.println(person.getName()   + " earns $" +
 46:                        person.getGrossWage() + " per week.");
 47:     }


 50:     /**
 51:      * The main driver function. This function creates a
 52:      * Worker object and an Executive object, then displays
 53:      * the gross weekly wage of each in two ways: first,
 54:      * directly, then with a call to the displayGrossWage()
 55:      * method.
 56:      */
 57:     public static void main(String[] args)
 58:     throws IOException
 59:     {
 60:         Worker janitor = new Worker("Fred");
 61:         janitor.setHoursWorked(35.0f);
 62:         janitor.setRateOfPay(10.25f);

 64:         Executive director = new Executive("Clive");
 65:         director.setAnnualSalary(48108.0f);

 67:         //The output from this is perhaps not that surprising ...
 68:         screen.println();
 69:         screen.println(janitor.getName()    + " earns $" +
 70:                        janitor.getGrossWage()  + " per week.");
 71:         screen.println(director.getName()   + " earns $" +
 72:                        director.getGrossWage() + " per week.");
 73:         pause();

 75:         //The output from this is perhaps more surprising ...
 76:         screen.println();
 77:         displayGrossWage(janitor);
 78:         displayGrossWage(director);
 79:         pause();
 80:     }
 81: }