Java 
Programming                                                             Summer 2008 
                                        LAB Solution
                                      Thursday 7/31/2008
    1.  Now we use inheritance to perform payroll calculation based on the type of 
    an employee. Consider the following problem: 
 
            A company pays its employees on a weekly basis. The company has four 
            types of employees: salaried employees, who are paid a fixed weekly salary 
            regardless of the number of hours worked; hour employees, who are paid by 
            the hour (for example, wage * hours) and receive overtime pay (for example, 
            (hours – 40) * wage * 1.5) and the maximum hours worked is 168 hours; 
            commission employees, who are paid a percentage of their sales; and 
            salaried‐commission employees, who receive a base salary plus a percentage 
            of their sales. For the current pay period, the company has decided to reward 
            salaried‐commission employees by adding 10% to their salaries. The 
            company wants to implement a Java application that performs its payroll 
            calculations.  
             
public class Main { 
 
    /** 
     * @param args the command line arguments 
     */ 
    public static void main(String[] args) { 
        // create Employee array 
        Employee[] employees = new Employee[4]; 
         
        // initialize array with Employess 
        employees[0] = new SalariedEmployee("George", "Bush", "111‐22‐3333",1000); 
        employees[1] = new HourlyEmployee("Bill", "Clinton", "222‐33‐4444", 16.75, 
40); 
        employees[2] = new CommissionEmployee("John", "MaCain", "333‐44‐5555", 
10000.0, 0.06); 
        employees[3] = new BasePlusCommissionEmployee("Barack", "Obama", "444‐
55‐6666", 5000.0, 0.04, 300); 
 
        String output = ""; 
         
        // generically process each element in array employees 
         
        for (int i = 0; i < employees.length; i++) { 
            output += employees[i].toString(); 
                                                                                         1 
Java Programming                                                            Summer 2008 
           
            if (employees[i] instanceof BasePlusCommissionEmployee){ 
                BasePlusCommissionEmployee currentEmployee =  
                        (BasePlusCommissionEmployee)employees[i]; 
                 
                double oldBaseSalary = currentEmployee.getBaseSalary(); 
                output += "\nold base salary: $" + oldBaseSalary; 
                 
                currentEmployee.setBaseSalary(1.10 * oldBaseSalary); 
                output += "\nnew base salary with 10% increase is: $" + 
                        currentEmployee.getBaseSalary(); 
            } 
            
            output += "\nearned: $" + employees[i].earnings() + "\n"; 
        } 
           // get type name of each object in employees array 
        for (int j = 0; j < employees.length; j++) 
            output += "\nEmployee " + j + " is a " + employees[j].getClass().getName(); 
         
        System.out.println(output);    
    } 
 
} 
 
public abstract class Employee { 
    private String firstName; 
    private String lastName; 
    private String SSN; 
              
    public Employee (String first, String last, String ssn) { 
          firstName = first; 
             lastName = last; 
             SSN = ssn; 
    } 
              
    public String getFirstName(){ 
             return firstName; 
    } 
 
    public void setFirstName (String first) { 
             firstName = first; 
    } 
              
    public String getLastName(){ 
                                                                                       2 
Java Programming                                                        Summer 2008 
         return lastName; 
    } 
          
    public void setLastName (String last) { 
         lastName = last; 
    } 
          
    public String getSSN(){ 
         return SSN; 
    } 
          
    public void setSSN(String number){ 
         SSN = number; 
    } 
          
    public String toString(){ 
         return getFirstName() + " " + getLastName() + "\nSSN: " + getSSN(); 
    } 
          
    public abstract double earnings(); 
} 
 
public class CommissionEmployee extends Employee{ 
    private double grossSales; 
    private double commissionRate; 
          
    public CommissionEmployee (String first, String last, String SSN, double 
grossWeeklySales, double percent){ 
         super (first, last, SSN); 
         setGrossSales (grossWeeklySales); 
         setCommissionRate (percent); 
    } 
          
    public double getGrossSales(){ 
         return grossSales; 
    } 
          
    public void setGrossSales (double sales){ 
         grossSales = sales < 0.0 ? 0.0 : sales; 
    } 
          
    public double getCommissionRate(){ 
         return commissionRate; 
    } 
                                                                                  3 
Java Programming                                                   Summer 2008 
          
    public void setCommissionRate (double rate){ 
         commissionRate = (rate > 0.0 && rate < 1.0) ? rate : 0.0; 
    } 
          
    public double earnings(){ 
         return getCommissionRate() * getGrossSales(); 
    } 
          
    public String toString(){ 
         return "\ncommission empolyee: " + super.toString(); 
    } 
} 
 
public class HourlyEmployee extends Employee { 
    private double wage; 
    private double hours; 
          
    public HourlyEmployee (String first, String last, String SSN, double 
hourlyWage, double hoursWorked) { 
         super (first, last, SSN); 
         setWage (hourlyWage); 
         setHours (hoursWorked); 
    } 
          
    public void setWage (double wageAmount) { 
         wage = wageAmount < 0.0 ? 0.0 : wageAmount; 
    } 
          
    public double getWage(){ 
         return wage; 
    } 
          
    public void setHours (double hoursWorked){ 
         hours = (hoursWorked >= 0.0 && hoursWorked <= 168.0) ? hoursWorked : 
0.0; 
    } 
          
    public double getHours(){ 
         return hours; 
    } 
          
    public double earnings(){ 
         if (hours <= 40) 
                                                                             4 
Java Programming                                                         Summer 2008 
            return wage * hours; 
            else 
            return 40 * wage + (hours ‐ 40) * wage * 1.5; 
    } 
             
    public String toString(){ 
            return "\nhourly employee: " + super.toString(); 
    } 
}            
 
public class BasePlusCommissionEmployee extends CommissionEmployee { 
    private double baseSalary; 
             
    public BasePlusCommissionEmployee (String first, String last, String SSN,  
            double grossSalesAmount, double rate, double baseSalaryAmount){   
            super (first, last, SSN, grossSalesAmount, rate); 
            setBaseSalary (baseSalaryAmount); 
    } 
             
    public void setBaseSalary (double salary){ 
            baseSalary = salary < 0.0 ? 0.0 : salary; 
    } 
             
    public double getBaseSalary(){ 
            return baseSalary; 
    } 
             
    public double earnings(){ 
            return getBaseSalary() + super.earnings(); 
    } 
             
    public String toString(){ 
            return "\nbase‐salaried commission employee: " + super.getFirstName() +   
            " " + super.getLastName() + "\nsocial security number: " + super.getSSN(); 
    } 
} 
             
       
      2. Below is the UML class diagram for another situation. The classes in it 
      represent various types of employees that might be employed at a particular 
      company. The Firm class contains a main driver that creates a Staff of 
      employees and invokes the payday method to pay them all. The program 
      output includes information about each employee and how much each is paid.  
       
                                                                                     5 
Java Programming                                                                                                Summer 2008 
    
                  Firm
       +main(args : String[]):void
                 Staff                           StaffMember
        -staffList:StaffMember[]               #name:String
                                               #address:String
                                               #phone:String
        +payday():void                         +toString():String
                                               +pay():double
                                   Volunteer                            Employee
                                                                    #SSN:String
                                                                    #payRate:double
                            +pay():double                           +toString():String
                                                                    +pay():double
                                                    Executive                                       Hourly
                                      -bonus:double                                      -hourWorked:int
                                      +awardBonus(Bonus:double):void                     +addHours(Hours:int):void
                                      +pay():dobule                                      +pay():double
                                                                                         +toString:String
                                                                              
    
   The Staff class maintains an array of objects that represent individual 
   employee of various types. Note that the array is declared to hold 
   Staffmember references, but it is actually filled with objects created from 
   several other classes, such as Executive, Employee, etc. These classes are 
   all descendents of the StaffMember class, so the assignments are valid. The 
   staffList array is filled with polymorphic reference. 
    
   The payday method of the Staff class scans through the list of employees, 
   printing their information and invoking their pay methods to determine how 
   much each employee should be paid. The invocation of the pay method is 
   polymorphic because each class has its own version of the pay method.  
    
   Note that this time, the executive is awarded $500 for bonus and the hourly 
   employee worked 40 hours.  
                                                                                                                          6 
Java Programming                                    Summer 2008 
     
    public class Firm { 
        public static void main (String[] args){ 
        Staff personnel = new Staff(); 
         
        personnel.payday(); 
        } 
    } 
                               
                                                              7 
Java Programming                                                           Summer 2008 
public class Staff { 
   private StaffMember[] staffList; 
    
   public Staff(){ 
       staffList = new StaffMember[4]; 
        
       staffList[0] = new Executive ("George", "123 Main Street", "555‐5555", "123‐45‐
6789", 10000.00); 
       staffList[1] = new Employee ("Diane", "456 Grand Ave.", "666‐6666", "012‐34‐
5678", 4000.00); 
       staffList[2] = new Hourly ("Woody", "789 Fifth Ave.", "777‐7777", "234‐56‐
7890", 10.55); 
       staffList[3] = new Volunteer ("Jane", "012 South Street", "888‐8888"); 
        
       ((Executive)staffList[0]).awardBonus(500.00); 
       ((Hourly)staffList[2]).addHours(40); 
   } 
 
   public void payday(){ 
       double amount; 
        
       for (int count = 0; count < staffList.length; count++){ 
            
           // get type name of each object staff array 
            
           System.out.println(staffList[count].getClass().getName()); 
           System.out.println(staffList[count]); 
           amount = staffList[count].pay(); 
            
           if (amount == 0.0) 
               System.out.println("Thanks!"); 
           else 
               System.out.println("Paid: " + amount); 
            
           System.out.println ("‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐"); 
       } 
   } 
} 
 
public abstract class StaffMember { 
    protected String name; 
    protected String address; 
    protected String phone; 
     
                                                                                         8 
Java Programming                                                          Summer 2008 
    public StaffMember (String iName, String iAddress, String iPhone){ 
        name = iName; 
        address = iAddress; 
        phone = iPhone; 
    } 
     
    public String toString(){ 
        String result = "Name: " + name + "\n"; 
         
        result += "Address: " + address + "\n"; 
        result += "Phone: " + phone; 
         
        return result; 
    } 
     
    public abstract double pay(); 
 
} 
 
                                 
                                                                                    9 
Java Programming                                                       Summer 2008 
       
public class Employee extends StaffMember{ 
    protected String SSN; 
    protected double payRate; 
     
    public Employee(String iName, String iAddress, String iPhone, String iSSN, 
double rate){ 
            super (iName, iAddress, iPhone); 
            SSN = iSSN; 
            payRate = rate; 
    } 
 
    public String toString(){ 
        String result = super.toString(); 
         
        result += "\nSSN: " + SSN; 
         
        return result; 
    } 
     
    public double pay(){ 
        return payRate; 
    } 
 
} 
 
public class Volunteer extends StaffMember{ 
    public Volunteer (String iName, String iAddress, String iPhone){ 
        super (iName, iAddress, iPhone); 
    } 
 
    public double pay(){ 
        return 0.0; 
    } 
}                                 
                                                                                  10 
Java Programming                                                        Summer 2008 
    
 
public class Executive extends Employee{ 
    private double bonus; 
     
    public Executive (String iName, String iAddress, String iPhone,  
            String SSN, double rate){ 
        super (iName, iAddress, iPhone, SSN, rate); 
        bonus = 0; 
    } 
     
    public void awardBonus(double execBonus){ 
        bonus = execBonus; 
    } 
 
    public double pay(){ 
        double payment = super.pay() + bonus; 
        bonus = 0; 
        return payment; 
    } 
} 
 
 
public class Hourly extends Employee{ 
    private int hoursWorked; 
     
    public Hourly (String iName, String iAddress, String iPhone, String iSSN, 
double rate){ 
        super (iName, iAddress, iPhone, iSSN, rate); 
        hoursWorked = 0; 
    } 
 
    public void addHours (int moreHours){ 
        hoursWorked += moreHours; 
    } 
     
    public double pay(){ 
        double payment = payRate * hoursWorked; 
        hoursWorked = 0; 
        return payment; 
    } 
     
    public String toString(){ 
        String result = super.toString(); 
                                                                                  11 
Java Programming                                        Summer 2008 
        result += "\nCurrent hours: " + hoursWorked; 
        return result; 
    } 
}                               
                                                                 12 
Java Programming                                                      Summer 2008 
   3. Create a program that calculates the areas and volumes of circle and 
   cylinder using an interface class and inheritance. Below are the shape 
   hierarchy class diagram and the table shows polymorphic interface for the 
   Shape hierarchy classes.  
    
                                     «interface»
                                       Shape
                                        Point
                                        Circle
                                       Cylinder
                                                      
                                            
                                            
                                            
                                            
                     getArea       getVolume       getName           toString 
    Shape              0.0             0.0         abstract       Default Object 
                                                                 Implementation 
    Point               0.0           0.0           “Point”            [x, y] 
    Circle              πr2           0.0           “Circle”      center = [x, y] 
                                                                    radius = r 
  Cylinder          2πr2 + 2πrh      πr2h          “Cylinder”     center = [x, y] 
                                                                    radius = r 
                                                                    height = h 
                                              
    
                                                                                13 
Java Programming                     Summer 2008 
    
public interface Shape { 
       public double getArea(); 
       public double getVolume(); 
       public String getName(); 
} 
                             
                                              14 
Java Programming                                        Summer 2008 
       
public class Point extends Object implements Shape { 
    private int x; 
    private int y; 
     
    public Point (int xValue, int yValue){ 
        x = xValue; 
        y = yValue; 
    } 
     
    public void setX (int xValue){ 
        x = xValue;         
    } 
     
    public int getX(){ 
        return x; 
    } 
     
    public void setY(int yValue){ 
        y = yValue; 
    } 
     
    public int getY(){ 
        return y; 
    } 
     
    public double getArea(){ 
        return 0.0;         
    } 
     
    public double getVolume(){ 
        return 0.0; 
    } 
     
    public String getName(){ 
        return "Point";         
    } 
     
    public String toString(){ 
        return "[" + getX() + ", " + getY() + "]"; 
    } 
} 
 
 
                                                                  15 
Java Programming                                                                Summer 2008 
public class Circle extends Point{ 
    private double radius; 
     
    public Circle (int x, int y, double radiusValue){ 
        super (x, y); 
        setRadius(radiusValue); 
    } 
     
    public void setRadius (double radiusValue){ 
        radius = (radiusValue < 0.0 ? 0.0 : radiusValue); 
    } 
     
    public double getRadius(){ 
        return radius; 
    } 
     
    public double getDiameter(){ 
        return 2*getRadius(); 
    } 
     
    public double getCircumference(){ 
        return Math.PI*getDiameter(); 
    } 
     
    public double getArea(){ 
        return Math.PI * getRadius() * getRadius(); 
    } 
     
    public String getName(){ 
        return "Circle"; 
    } 
     
    public String toString(){ 
        return "Center = " + super.toString() + "; Radius = " + getRadius(); 
    } 
} 
 
public class Cylinder extends Circle{ 
    private double height; 
     
    public Cylinder (int x, int y, double radius, double heightValue){ 
        super(x, y, radius); 
        setHeight(heightValue); 
    } 
                                                                                         16 
Java Programming                                                          Summer 2008 
     
    public void setHeight(double heightValue){ 
        height = (heightValue < 0.0 ? 0.0 : heightValue); 
    } 
     
    public double getHeight(){ 
        return height; 
    } 
     
    public double getArea(){ 
        return 2 * super.getArea() + getCircumference() * getHeight(); 
    } 
     
    public double getVolume(){ 
        return super.getArea() * getHeight(); 
    } 
     
    public String getName(){ 
        return "Cylinder"; 
    } 
     
    public String toString(){ 
        return super.toString() + "; Height = " + getHeight(); 
    } 
} 
 
import java.text.DecimalFormat; 
 
public class ShapeTest { 
    public static void main(String[] args){ 
        DecimalFormat twoDigits = new DecimalFormat("0.00"); 
        Point point = new Point (7, 11); 
        Circle circle = new Circle (22, 8, 3.5); 
        Cylinder cylinder = new Cylinder (20, 30, 3.3, 10.75); 
         
        String output = point.getName() + ": " + point + "\n" + 
                circle.getName() + ": " + circle + "\n" + 
                cylinder.getName() + ": " + cylinder + "\n"; 
         
        Shape[] arrayOfShapes = new Shape [3]; 
         
        arrayOfShapes[0] = point; 
        arrayOfShapes[1] = circle; 
        arrayOfShapes[2] = cylinder; 
                                                                                   17 
Java Programming                                                             Summer 2008 
         
        for (int i = 0; i < arrayOfShapes.length; i++){ 
            output += "\n\n" + arrayOfShapes[i].getName() + ": " +  
                    arrayOfShapes[i].toString() + "\nArea = " + 
                    twoDigits.format(arrayOfShapes[i].getArea()) + "\nVolume = " +  
                    twoDigits.format(arrayOfShapes[i].getVolume()); 
                    //arrayOfShapes[i].getArea() + "\nVolume = " +  
                    //arrayOfShapes[i].getVolume(); 
        } 
         
        System.out.println(output); 
    } 
 
} 
                                                                                       18