0% found this document useful (0 votes)
19 views

Java Notes

The document defines four subclasses that extend an abstract Employee class: SalariedEmployee, HourlyEmployee, CommissionEmployee. Each subclass defines constructors, methods to set/get values, and overrides the abstract earnings method from Employee to calculate earnings specific to the subclass type.

Uploaded by

Rehan Gaming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Java Notes

The document defines four subclasses that extend an abstract Employee class: SalariedEmployee, HourlyEmployee, CommissionEmployee. Each subclass defines constructors, methods to set/get values, and overrides the abstract earnings method from Employee to calculate earnings specific to the subclass type.

Uploaded by

Rehan Gaming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

1.

Employee
2. package packageName;

3. //Fig. 10.4: Employee.java

4. // Employee abstract superclass

5. public abstract class Employee {

6. private String firstName;

7. private String lastName;

8. private String socialSecurityNumber;

9. // three-argument constructor

10. public Employee( String first, String last, String ssn )

11. {

12. firstName = first;

13. lastName = last;

14. socialSecurityNumber = ssn;

15. } // end three-argument Employee constructor

16.

17. // set first name

18. public void setFirstName( String first )

19. {

20. firstName = first; // should validate

21. } // end method setFirstName

22.

23. // return first name

24. public String getFirstName()

25. {

26. return firstName;

27. } // end method getFirstName

28.

29. // set last name

1
30. public void setLastName( String last )

31. {

32. lastName = last; // should validate

33. } // end method setLastName

34.

35. // return last name

36. public String getLastName()

37. {

38. return lastName;

39. } // end method getLastName

40.

41. // set social security number

42. public void setSocialSecurityNumber( String ssn )

43. {

44. socialSecurityNumber = ssn; // should validate

45. } // end method setSocialSecurityNumber

46.

47. // return social security number

48. public String getSocialSecurityNumber()

49. {

50. return socialSecurityNumber;

51. } // end method getSocialSecurityNumber

52.

53. // return String representation of Employee object

54. @Override

55. public String toString()

56. {

57. return String.format( "%s %s\nsocial security number: %s",

58. getFirstName(), getLastName(),


getSocialSecurityNumber() );

2
59. } // end method toString

60.

61. // abstract method overridden by concrete subclasses

62. public abstract double earnings(); // no implementation here

63. } // end abstract class Employee

64. ________________________________________________________________________________________

65. SalariedEmployee
66. package packageName;

67. public class SalariedEmployee extends Employee {

68. private double weeklySalary;

69. // four-argument constructor

70. public SalariedEmployee( String first, String last, String ssn,

71. double salary )

72. {

73. super( first, last, ssn ); // pass to Employee constructor

74. setWeeklySalary( salary ); // validate and store salary

75. } // end four-argument SalariedEmployee constructor

76.

77. // set salary

78. public void setWeeklySalary( double salary )

79. {

80. if ( salary >= 0.0 )

81. weeklySalary = salary;

82. else

83. throw new IllegalArgumentException(

84. "Weekly salary must be >= 0.0" );

85. } // end method setWeeklySalary

86. // return salary

87. public double getWeeklySalary()

3
88. {

89. return weeklySalary;

90. } // end method getWeeklySalary

91.

92. // calculate earnings; override abstract method earnings in Employee

93. @Override

94. public double earnings()

95. {

96. return getWeeklySalary();

97. } // end method earnings

98.

99. // return String representation of SalariedEmployee object

100. @Override

101. public String toString()

102. {

103. return String.format( "salaried employee: %s\n%s: $%,.2f",

104. super.toString(), "weekly salary", getWeeklySalary() );

105. } // end method toString

106. } // end class SalariedEmployee

107. _______________________________________________________________________________________

108. HourlyEmployee
109. package packageName;

110. public class HourlyEmployee extends Employee {

111.

112. private double wage; // wage per hour

113. private double hours; // hours worked for week

114.

115. // five-argument constructor

116. public HourlyEmployee( String first, String last, String ssn,

4
117. double hourlyWage, double hoursWorked )

118. {

119. super( first, last, ssn );

120. setWage( hourlyWage ); // validate hourly wage

121. setHours( hoursWorked ); // validate hours worked

122. } // end five-argument HourlyEmployee constructor

123.

124. // set wage

125. public void setWage( double hourlyWage )

126. {

127. if ( hourlyWage >= 0.0 )

128. wage = hourlyWage;

129. else

130. throw new IllegalArgumentException(

131. "Hourly wage must be >= 0.0" );

132. } // end method setWage

133. // return wage

134. public double getWage()

135. {

136. return wage;

137. } // end method getWage

138.

139. // set hours worked

140. public void setHours( double hoursWorked )

141. {

142. if ( ( hoursWorked >= 0.0 ) && ( hoursWorked <= 168.0 ) )

143. hours = hoursWorked;

144. else

145. throw new IllegalArgumentException(

146. "Hours worked must be >= 0.0 and <= 168.0" );

5
147. } // end method setHours

148.

149. // return hours worked

150. public double getHours()

151. {

152. return hours;

153. } // end method getHours

154.

155. // calculate earnings; override abstract method earnings in Employee

156. @Override

157. public double earnings()

158. {

159. if ( getHours() <= 40 ) // no overtime

160. return getWage() * getHours();

161. else

162. return 40 * getWage() + ( getHours() - 40 ) * getWage() * 1.5;

163. } // end method earnings

164.

165. // return String representation of HourlyEmployee object

166. @Override

167. public String toString()

168. {

169. return String.format( "hourly employee: %s\n%s: $%,.2f; %s: %,.2f",

170. super.toString(), "hourly wage", getWage(),

171. "hours worked", getHours() );

172. } // end method toString

173.

174. } // end class HourlyEmployee

175. _____________________________________________________________-------------

6
176. CommissionEmployee
177. package packageName;

178. public class CommissionEmployee extends Employee{

179. private double grossSales; // gross weekly sales

180. private double commissionRate; // commission percentage

181.

182. // five-argument constructor

183. public CommissionEmployee( String first, String last, String ssn,

184. double sales, double rate )

185. {

186. super( first, last, ssn );

187. setGrossSales( sales );

188. setCommissionRate( rate );

189. } // end five-argument CommissionEmployee constructor

190.

191. // set commission rate

192. public void setCommissionRate( double rate )

193. {

194. if ( rate > 0.0 && rate < 1.0 )

195. commissionRate = rate;

196. else

197. throw new IllegalArgumentException(

198. "Commission rate must be > 0.0 and < 1.0" );

199. } // end method setCommissionRate

200.

201. // return commission rate

202. public double getCommissionRate()

203. {

204. return commissionRate;

7
205. } // end method getCommissionRate

206.

207. // set gross sales amount

208. public void setGrossSales( double sales )

209. {

210. if ( sales >= 0.0 )

211. grossSales = sales;

212. else

213. throw new IllegalArgumentException(

214. "Gross sales must be >= 0.0" );

215. } // end method setGrossSales

216.

217. // return gross sales amount

218. public double getGrossSales()

219. {

220. return grossSales;

221. } // end method getGrossSales

222.

223. // calculate earnings; override abstract method earnings in Employee

224. @Override

225. public double earnings()

226. {

227. return getCommissionRate() * getGrossSales();

228. } // end method earnings

229.

230. // return String representation of CommissionEmployee object

231. @Override

232. public String toString()

233. {

234. return String.format( "%s: %s\n%s: $%,.2f; %s: %.2f",

8
235. "commission employee", super.toString(),

236. "gross sales", getGrossSales(),

237. "commission rate", getCommissionRate() );

238. } // end method toString

239.

240. } // end class CommissionEmployee

241. _______________________________________________________________________________________

242. BasePlusCommissionEmployee
243. package packageName;

244. public class BasePlusCommissionEmployee extends CommissionEmployee{

245. private double baseSalary; // base salary per week

246.

247. // six-argument constructor

248. public BasePlusCommissionEmployee( String first, String last,

249. String ssn, double sales, double rate, double salary )

250. {

251. super( first, last, ssn, sales, rate );

252. setBaseSalary( salary ); // validate and store base salary

253. } // end six-argument BasePlusCommissionEmployee constructor

254.

255. // set base salary

256. public void setBaseSalary( double salary )

257. {

258. if ( salary >= 0.0 )

259. baseSalary = salary;

260. else

261. throw new IllegalArgumentException(

262. "Base salary must be >= 0.0" );

263. } // end method setBaseSalary

9
264.

265. // return base salary

266. public double getBaseSalary()

267. {

268. return baseSalary;

269. } // end method getBaseSalary

270.

271. // calculate earnings; override method earnings in CommissionEmployee

272. @Override

273. public double earnings()

274. {

275. return getBaseSalary() + super.earnings();

276. } // end method earnings

277.

278. // return String representation of BasePlusCommissionEmployee object

279. @Override

280. public String toString()

281. {

282. return String.format( "%s %s; %s: $%,.2f",

283. "base-salaried", super.toString(),

284. "base salary", getBaseSalary() );

285. } // end method toString

286.

287. } // end class BasePlusCommissionEmployee

288. _______________________________________________________________________________________

289. PayrollSystemTest
290.

291. package packageName;

292. public class PayrollSystemTest {

10
293. public static void main(String[] args) {

294. // create subclass objects

295. SalariedEmployee salariedEmployee =

296. new SalariedEmployee( "John", "Smith", "111-11-1111",


800.00 );

297. HourlyEmployee hourlyEmployee =

298. new HourlyEmployee( "Karen", "Price", "222-22-2222",


16.75, 40 );

299. CommissionEmployee commissionEmployee =

300. new CommissionEmployee(

301. "Sue", "Jones", "333-33-3333",


10000, .06 );

302. BasePlusCommissionEmployee basePlusCommissionEmployee =

303. new BasePlusCommissionEmployee(

304. "Bob", "Lewis", "444-44-4444",


5000, .04, 300 );

305. System.out.println( "Employees processed individually:\n" );

306.

307. System.out.printf( "%s\n%s: $%,.2f\n\n",

308. salariedEmployee, "earned",


salariedEmployee.earnings() );

309. System.out.printf( "%s\n%s: $%,.2f\n\n",

310. hourlyEmployee, "earned", hourlyEmployee.earnings() );

311. System.out.printf( "%s\n%s: $%,.2f\n\n",

312. commissionEmployee, "earned",


commissionEmployee.earnings() );

313. System.out.printf( "%s\n%s: $%,.2f\n\n",

314. basePlusCommissionEmployee,

315. "earned", basePlusCommissionEmployee.earnings() );

316.

317. // create four-element Employee array

318. Employee[] employees = new Employee[ 4 ];

319. // initialize array with Employees

11
320. employees[ 0 ] = salariedEmployee;

321. employees[ 1 ] = hourlyEmployee;

322. employees[ 2 ] = commissionEmployee;

323. employees[ 3 ] = basePlusCommissionEmployee;

324. System.out.println( "Employees processed polymorphically:\n" );

325.

326. // generically process each element in array employees

327. for ( Employee currentEmployee : employees )

328. {

329. System.out.println(currentEmployee);

330.

331. if( currentEmployee instanceof BasePlusCommissionEmployee )

332. {

333. // downcast Employee reference to

334. // BasePlusCommissionEmployee reference

335. BasePlusCommissionEmployee employee =

336. ( BasePlusCommissionEmployee )
currentEmployee;

337. employee.setBaseSalary( 1.10 *


employee.getBaseSalary() );

338.

339. System.out.printf(

340. "new base salary with 10%% increase is:


$%,.2f\n",

341. employee.getBaseSalary() );

342. } // end if

343.

344. System.out.printf(

345. "earned $%,.2f\n\n",


currentEmployee.earnings());

346. } // end for

347. // get type name of each object in employees array

12
348. for ( int j = 0; j < employees.length; j++ )

349. System.out.printf( "Employee %d is a %s\n", j,

350. employees[ j ].getClass().getName() );

351. } // end main

352. } // end class PayrollSystemTest

353.

13

You might also like