Programming Challenge in Java (Netbeans JDK) Design a class named Employee. The
ID: 3767055 • Letter: P
Question
Programming Challenge in Java (Netbeans JDK)
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 Lis aletter within the range A-M.
• Hire date
* 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 and mutator methods
for the class. Demonstrate the classes by writing a program that uses a ProductionWorker
object.
Explanation / Answer
import java.util.*;
public class EmployeeClass
{
String name; // Employee name
String employeeNumber; // Employee number
String hireDate; // Employee hire date
/**
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 = "";
}
}
/**
This constructor initializes an object with a name,
employee number, and hire date.
@param n The employee's name.
@param e The employee's number.
@param h The employee's hire date.
*/
public EmployeeClass(String n, String e, String h)
{
name = n;
setEmployeeNumber(e);
hireDate = h;
}
/**
The no-arg constructor initializes an object with
null strings for name, employee number, and hire
date.
*/
public EmployeeClass()
{
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 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))) ||
//needs to check if between A and M
(!(e.charAt(4)>= 'A' && e.charAt(4)<= 'M')))
{
status = false;
}
}
return status;
}
/**
toString method
@return A reference to a String representation of
the object.
*/
public String toString()
{
String str = "Name: " + name + " Employee Number: ";
if (employeeNumber == "")
{
str += "INVALID EMPLOYEE NUMBER";
}
else
{
str += employeeNumber;
}
str += (" Hire Date: " + hireDate);
return str;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.