Employee and ProductionWorker Classes
Design a class named Employee. the class should keep the following information in fields :
- Employee name
- Employee number in the format XXX-L, where each X is a digit within the range 0-9 and the L is a letter within the range A-M.
- Hire date
Join other Subscribers on our YouTube channel and enjoy daily programming tutorials.
Write one or more constructors and the appropriate accessor and mutator methods for the class.
Next, write a class named ProductionWorker that extends the Employee class. The ProductionWorker class should have fields to hold the following information:
- Shift (an integer)
- Hourly pay rate (a double)
The workday is divided into two shifts: day and night. The shift field will be an integer value representing the shift that the employee works. The day shift is shift 1 and the night shift is shift 2.
Write one or more constructors and the appropriate accessor (getter) and mutator (setter) methods for the class. Demonstrate the classes by writing a program that uses a ProductionWorker object.
Recommended
Variables and Literals in Java
Variables are used in many situations. For Instance, game applications use variables to collect...
Solution
Employee Class
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 |
package programmingChallenges; /** The Employee class stores data about an employee for the Employee and ProductionWorker Classes programming challenge 1. */ public class Employee { private String name; // Employee name private String employeeNumber; // Employee number private String hireDate; // Employee hire date /** This constructor initializes an object with a name, employee number, and hire date. @param n The employee's name. @param num The employee's number. @param date The employee's hire date. */ public Employee(String n, String num, String date){ name = n; setEmployeeNumber(num); hireDate = date; } /** The no-arg constructor initializes an object with null strings for name, employee number, and hire date. */ public Employee(){ name = ""; employeeNumber = ""; hireDate = ""; } /** The setName method sets the employee's name. @param n The employee's name. */ public void setName(String n){ name = n; } /** The setEmployeeNumber method sets the employee's number. @param e The employee's number. */ public void setEmployeeNumber(String e){ if (isValidEmpNum(e)) employeeNumber = e; else employeeNumber = ""; } /** The setHireDate method sets the employee's hire date. @param h The employee's hire date. */ public void setHireDate(String h){ hireDate = h; } /** The getName method returns the employee's name. @return The employee's name. */ public String getName(){ return name; } /** The getEmployeeNumber method returns the employee's number. @return The employee's number. */ public String getEmployeeNumber(){ return employeeNumber; } /** The getHireDate method returns the employee's hire date. @return The employee's hire date. */ public String getHireDate(){ return hireDate; } /** isValidEmpNum is a private method that determines whether a string is a valid employee number. @param e The string containing an employee number. @return true if e references a valid ID number, false otherwise. */ private boolean isValidEmpNum(String e){ boolean status = true; if (e.length() != 5) status = false; else { if ((!Character.isDigit(e.charAt(0))) || (!Character.isDigit(e.charAt(1))) || (!Character.isDigit(e.charAt(2))) || (e.charAt(3) != '-') || (!Character.isLetter(e.charAt(4)))) status = false; } return status; } /** toString method @return A reference to a String representation of the object. */ public String toString() { String str = "Name: " + name + "\nEmployee Number: "; if (employeeNumber == "") str += "INVALID EMPLOYEE NUMBER"; else str += employeeNumber; str += ("\nHire Date: " + hireDate); return str; } } |
ProductionWorker Class
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 |
package programmingChallenges; import java.text.DecimalFormat; /** The ProductionWorker class stores data about an employee who is a production worker for the Employee and ProductionWorker Classes programming challenge. */ public class ProductionWorker extends Employee { // Constants for the day and night shifts. public static final int DAY_SHIFT = 1; public static final int NIGHT_SHIFT = 2; private int shift; // The employee's shift private double payRate; // The employee's pay rate /** This constructor initializes an object with a name, employee number, hire date, shift, and pay rate @param n The employee's name. @param num The employee's number. @param date The employee's hire date. @param sh The employee's shift. @param rate The employee's pay rate. */ public ProductionWorker(String n, String num, String date, int sh, double rate) { super(n, num, date); shift = sh; payRate = rate; } /** The no-arg constructor initializes an object with null strings for name, employee number, and hire date. The day shift is selected, and the pay rate is set to 0.0. */ public ProductionWorker() { super(); shift = DAY_SHIFT; payRate = 0.0; } /** The setShift method sets the employee's shift. @param s The employee's shift. */ public void setShift(int s) { shift = s; } /** The setPayRate method sets the employee's pay rate. @param p The employee's pay rate. */ public void setPayRate(double p) { payRate = p; } /** The getShift method returns the employee's shift. @return The employee's shift. */ public int getShift() { return shift; } /** The getPayRate method returns the employee's pay rate. @return The employee's pay rate. */ public double getPayRate() { return payRate; } /** toString method @return A reference to a String representation of the object. */ public String toString() { DecimalFormat dollar = new DecimalFormat("#,##0.00"); String str = super.toString(); str += "\nShift: "; if (shift == DAY_SHIFT) str += "Day"; else if (shift == NIGHT_SHIFT) str += "Night"; else str += "INVALID SHIFT NUMBER"; str += ("\nHourly Pay Rate: $" + dollar.format(payRate)); return str; } } |
WorkerDemo
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 |
package programmingChallenges; /** This program demonstrates a solution to the Employee and ProductionWorker Classes programming challenge. */ public class WorkerDemo { public static void main(String[] args) { String shift; // To hold a shift value // Create a ProductionWorker object and pass the initialization // data to the constructor. ProductionWorker pw = new ProductionWorker("Douglas Wood", "457-C", "25-08-2022", ProductionWorker.DAY_SHIFT, 14.50); // Display the data. System.out.println("Here's the first production worker."); System.out.println(pw); // Create another ProductionWorker object and use the // set methods. ProductionWorker pw2 = new ProductionWorker(); pw2.setName("Mary Addai"); pw2.setEmployeeNumber("512-K"); pw2.setHireDate("19-07-2021"); pw2.setShift(ProductionWorker.NIGHT_SHIFT); pw2.setPayRate(20.50); // Display the data. System.out.println("\nHere's the second production worker."); System.out.println(pw2); } } |
Output
You May Also Like…
double data type in Java
The double data type is also a floating point number. It is called double because it has a double-precision decimal...
The print and println methods in Java
The print() and println() methods are Java's predefined methods used to display text output on the console. These...
float data type in Java
It is a floating point number. It has single precision data type. It stores a floating point number of 7 digits of...
Recent Comments