Site Search

CS 111: Lab 4 main content

Student Learning Objectives

Demonstrate the ability to

Lab 4 Assignment

Do Chapter 9, Programming Challenge 1: Employee and ProductionWorker Classes

Sample Session

Follow the sample sessions shown below:

Enter the name of the input file: employees.txt

Name:               Ed Gellenbeck
Employee Number:    123-D
Hire Date:          10/2/2001
Shift:              Day
Hourly Pay Rate:    $12.50

Name:               Fred Stanley
Employee Number:    345-A
Hire Date:          1/20/2000
Shift:              Night
Hourly Pay Rate:    $25.65

Name:               Jim Schwing
Employee Number:    899-B
Hire Date:          05/12/2002
Shift:              Day
Hourly Pay Rate:    $100.33

You may use the following employees.txt input file:

Ed Gellenbeck
123-D
10/2/2001
1
12.50
Fred Stanley
345-A
1/20/2000
2
25.65
Jim Schwing
899-B
05/12/2002
1
100.33

Employee.java

Use the code below for your Employee class. You do not need to modify this code

   public abstract class Employee
   {
   //Private instance variables
      private String name;
      private String number;
      private String hireDate;
      
   
   // No Args Constructor
      public Employee()
      {
         name = "NO NAME";
         number = "NO NUMBER";
         hireDate = "NO HIRE DATE";
      }
   
   
   // Constructor
      public Employee(String employeeName, String employeeNumber, String hireDate)
      {
         setEmployeeName(employeeName);
         setEmployeeNumber(employeeNumber);
         setHireDate(hireDate);
      }
   
   
   // Sets name to employeeName if legal, otherwise sets to "NO NAME"
      public void setEmployeeName(String employeeName)
      {
         if (employeeName.matches("[a-zA-Z ]+"))
            name = employeeName;
         else
            name = "NO NAME";
      }
   
   
   // Sets number to employeeNumber if legal, otherwise sets to "NO NUMBER"
      public void setEmployeeNumber(String employeeNumber)
      {
         if (employeeNumber.matches("\\d{3}-[A-M]"))
            number = employeeNumber;
         else
            number = "INVALID NUMBER";
      }
   
   
   // Sets hireDate to dateHired if legal, otherwise sets to "NO HIRE DATE"
      public void setHireDate(String dateHired)
      {
         if (dateHired.matches("\\d\\d?/\\d\\d?/\\d\\d\\d\\d"))
            hireDate = dateHired;
         else
            hireDate = "NO HIRE DATE";
      }
   
   
   // Returns the employee's name
      public String getEmployeeName()
      {
         return name;
      }
   
   
   // Returns the employee's number
      public String getEmployeeNumber()
      {
         return number;
      }
   
   
   // Returns the employee's hire date
      public String getHireDate()
      {
         return hireDate;
      }
   
   
   // Returns the name, number and hire date
      public String toString()
      {
         return "Name:\t\t" + name + 
                "\nEmployee Number:\t" + number +   
                "\nHire Date:\t\t" + hireDate;
      }
   }

To Receive Credit

Programming Challenge

If you finish early, get yourself checked off first! As a programming challenge (not required but recommended):

Modify your program so that the

  1. The int shift variable in ProductionWorker uses an enumerated type instead. If a value other than 1 or 2 is read from input, assign shift to the default value Shift.None
  2. The hourly pay rate input value is error checked (use String.matches) for a legal pay rate amount. Assign payRate the default value 0.0 if the input value is in the incorrect format
public enum ShiftType {None, Day, Night}