CSIS 312 Assignment 2 Employee Class

20.00 $ 10.00 $

Category:

Description

In chapter 9 of your Deitel & Deitel text, you studied an inheritance hierarchy in which class BasePlusCommissionEmployee inherited from class CommissionEmployee. However, not all types of employees are CommissionEmployees.

In this exercise, you’ll create a more general Employee superclass that factors out the attributes and behaviors in class CommissionEmployee that are common to all Employees. The common attributes and behaviors for all Employees are firstName, lastName, socialSecurityNumber, getFirstName, getLastName, getSocialSecurityNumber and a portion of method toString.

Create a new superclass Employee that contains these instance variables and methods and a constructor.

Next, rewrite class CommissionEmployee from Section 9.4.5 as a subclass of Employee. Class CommissionEmployee should contain only the instance variables and methods that are not declared in superclass Employee. Class CommissionEmployee’s constructor should invoke class Employee’s constructor and CommissionEmployee’s toString method should invoke Employee’s toString method.

Once you’ve completed these modifications, run the BasePlusCommissionEmployeeTest app using these new classes to ensure that the app still displays the same results for a BasePlusCommissionEmployee object.

Make sure that your screen shots show your program running and that your runtime display shows that your program does all that is required of it. You only get credit for what you demonstrate.

Submit this assignment by 11:59 p.m. (ET) on Monday of Module/Week 2.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Fig. 9.10: CommissionEmployee.java
// CommissionEmployee class uses methods to manipulate its
// private instance variables.
public class CommissionEmployee
{
   private final String firstName;                             
   private final String lastName;                              
   private final String socialSecurityNumber;                  
   private double grossSales; // gross weekly sales      
   private double commissionRate; // commission percentage
   // five-argument constructor
   public CommissionEmployee(String firstName, String lastName,
      String socialSecurityNumber, double grossSales,
      double commissionRate)
   {
      // implicit call to Object constructor occurs here
      // if grossSales is invalid throw exception
      if (grossSales < 0.0)
         throw new IllegalArgumentException(
            "Gross sales must be >= 0.0");
      // if commissionRate is invalid throw exception
      if (commissionRate <= 0.0 || commissionRate >= 1.0)
         throw new IllegalArgumentException(
            "Commission rate must be > 0.0 and < 1.0");
      this.firstName = firstName;                                   
      this.lastName = lastName;                                   
      this.socialSecurityNumber = socialSecurityNumber;        
      this.grossSales = grossSales;
      this.commissionRate = commissionRate;
   } // end constructor
   // return first name
   public String getFirstName()
   {
      return firstName;
   }
   // return last name
   public String getLastName()
   {
      return lastName;
   }
   // return social security number
   public String getSocialSecurityNumber()
   {
      return socialSecurityNumber;
   }
   // set gross sales amount
   public void setGrossSales(double grossSales)
   {
      if (grossSales < 0.0)
         throw new IllegalArgumentException(
            "Gross sales must be >= 0.0");
      this.grossSales = grossSales;
   }
   // return gross sales amount
   public double getGrossSales()
   {
      return grossSales;
   }
   // set commission rate
   public void setCommissionRate(double commissionRate)
   {
      if (commissionRate <= 0.0 || commissionRate >= 1.0)
         throw new IllegalArgumentException(
            "Commission rate must be > 0.0 and < 1.0");
      this.commissionRate = commissionRate;
   }
   // return commission rate
   public double getCommissionRate()
   {
      return commissionRate;
   }
   // calculate earnings
   public double earnings()
   {
      return getCommissionRate() * getGrossSales();
   }
   // return String representation of CommissionEmployee object
   @Override
   public String toString()
   {
      return String.format("%s: %s %s%n%s: %s%n%s: %.2f%n%s: %.2f",
         "commission employee", getFirstName(), getLastName(),
         "social security number", getSocialSecurityNumber(),
         "gross sales", getGrossSales(),
         "commission rate", getCommissionRate());
   }
} // end class CommissionEmployee
// Fig. 9.11: BasePlusCommissionEmployee.java
// BasePlusCommissionEmployee class inherits from CommissionEmployee
// and accesses the superclass’s private data via inherited
// public methods.
public class BasePlusCommissionEmployee extends CommissionEmployee
{
   private double baseSalary; // base salary per week
   // six-argument constructor
   public BasePlusCommissionEmployee(String firstName, String lastName,
      String socialSecurityNumber, double grossSales,
      double commissionRate, double baseSalary)
   {
      super(firstName, lastName, socialSecurityNumber,
         grossSales, commissionRate);                     
      // if baseSalary is invalid throw exception
      if (baseSalary < 0.0)                  
         throw new IllegalArgumentException(
            "Base salary must be >= 0.0"); 
      this.baseSalary = baseSalary;
   }
   
   // set base salary
   public void setBaseSalary(double baseSalary)
   {
      if (baseSalary < 0.0)                  
         throw new IllegalArgumentException(
            "Base salary must be >= 0.0"); 
      this.baseSalary = baseSalary;               
   }
   // return base salary
   public double getBaseSalary()
   {
      return baseSalary;
   }
   // calculate earnings
   @Override
   public double earnings()
   {
      return getBaseSalary() + super.earnings();
   }
   // return String representation of BasePlusCommissionEmployee
   @Override
   public String toString()
   {
      return String.format("%s %s%n%s: %.2f", "base-salaried",
         super.toString(), "base salary", getBaseSalary());  
   }
} // end class BasePlusCommissionEmployee
// BasePlusCommissionEmployeeTest.java
// Testing class BasePlusCommissionEmployee.
public class BasePlusCommissionEmployeeTest
{
   public static void main(String[] args)
   {
      // instantiate CommissionEmployee object
      CommissionEmployee employee1 = new CommissionEmployee(
         "Sue", "Jones", "222-22-2222", 10000, .06);     
      
      // get commission employee data
      System.out.println(
         "Employee information obtained by get methods:");
      System.out.printf("%n%s %s%n", "First name is",
         employee1.getFirstName());
      System.out.printf("%s %s%n", "Last name is",
         employee1.getLastName());
      System.out.printf("%s %s%n", "Social security number is",
         employee1.getSocialSecurityNumber());
      System.out.printf("%s %.2f%n", "Gross sales is",
         employee1.getGrossSales());
      System.out.printf("%s %.2f%n", "Commission rate is",
         employee1.getCommissionRate());
      employee1.setGrossSales(5000);
      employee1.setCommissionRate(.1);
      
      System.out.printf("%n%s:%n%n%s%n",                               
         "Updated employee information obtained by toString", employee1);
      
      // instantiate BasePlusCommissionEmployee object
      BasePlusCommissionEmployee employee2 =
         new BasePlusCommissionEmployee(
            "Bob", "Lewis", "333-33-3333", 5000, .04, 300);
      
      // get base-salaried commission employee data
      System.out.println(
         "Employee information obtained by get methods:");
      System.out.printf("%n%s %s%n", "First name is",
         employee2.getFirstName());
      System.out.printf("%s %s%n", "Last name is",
         employee2.getLastName());
      System.out.printf("%s %s%n", "Social security number is",
         employee2.getSocialSecurityNumber());
      System.out.printf("%s %.2f%n", "Gross sales is",
         employee2.getGrossSales());
      System.out.printf("%s %.2f%n", "Commission rate is",
         employee2.getCommissionRate());
      System.out.printf("%s %.2f%n", "Base salary is",
         employee2.getBaseSalary());
      employee2.setBaseSalary(1000);
      
      System.out.printf("%n%s:%n%n%s%n",
         "Updated employee information obtained by toString",
         employee2.toString());
   } // end main
} // end class BasePlusCommissionEmployeeTest