I am having problems with the output of my program. The progam should 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
Write one or more constructors and the appropriate accessor and mutator methods for the class.
Next, write a class named ProductionWorker that inherits from 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 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

Employee Class

import java.util.*;

   public class Employee
   {
      String name;             // Employee name
      String employeeNumber;   // Employee number
      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 e The employee's number.
      @param h The employee's hire date.
   */
      
      public Employee(String n, String e, String h)
      {
         name = n;
         employeeNumber = e;
         hireDate = h;
      }
   /**
      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)))  ||
             
				 //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 + "\nEmployee Number: ";
      
         if (employeeNumber == "")
         {
            str += "INVALID EMPLOYEE NUMBER";
         }
         else
         {
            str += employeeNumber;
         }
         
         str += ("\nHire Date: " + hireDate);
         return str;
      }
   }

ProductionWorkerA Class

import java.util.*;
 import java.text.DecimalFormat;
 import java.text.DateFormat;
 
public class ProductionWorkerA extends EmployeeA
{
   // 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 ProductionWorkerA(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 ProductionWorkerA()
   {
      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;
   }
}

ProductionWorkerDemo

import java.util.*;
   import java.text.DecimalFormat;
   import java.text.DateFormat;
	
	/**
		This program  uses the Production Worker class that extends the employee 
		class.
	*/

   public class ProductionWorkerDemo
   {
      public static void main(String[] args)
      {
         String name;             // Employee name
         String employeeNumber;   // Employee number
         String hireDate;         // Employee hire date
         int shift;		  			 // Employee shift
         double payRate;  			 // Employee pay
      
      	// Creates Scanner object
         Scanner s = new Scanner(System.in);
      	
      	// Gets the user's name.	
         System.out.println("Enter your name: ");
         name = s.nextLine();
      	
      	// Gets the user's employee number.	
         System.out.println("Enter your employee number: ");
         employeeNumber = s.nextLine();
      
      	// Gets the user's hire date.	
         System.out.println("Enter your hire date: ");
         hireDate = s.nextLine();
      	
      	// Gets the user's payrate.
         System.out.println("Enter your payrate: ");
         payRate = s.nextDouble();
      	
      	// Gets the user's shift.
         System.out.println("Enter your shift: ");
         shift = s.nextInt();			 
      	
      	// Creates an Production worker object.
         ProductionWorkerA pw = 
            	new ProductionWorkerA();
      	
			System.out.println();
         System.out.println("Name: " + pw.getName());
         System.out.println("Employee Number: " + pw.getEmployeeNumber());
         System.out.println("Hire Date: " + pw.getHireDate());
         System.out.println("Pay Rate: " + pw.getPayRate());
         System.out.println("Shift: " + pw.getShift());
      	
      }
   }

Output when I run the ProductionWorkerDemo
jGRASP exec: java ProductionWorkerDemo

Enter your name:
Glen
Enter your employee number:
333-l
Enter your hire date:
12/20/2009
Enter your payrate:
12.45
Enter your shift:
2

Name:
Employee Number:
Hire Date:
Pay Rate: 0.0
Shift: 1

main line 44
You create the pw using the no-args constructor which you document as follows:

The no-arg constructor initializes an object with null* strings for name, employee number, and hire date.

You then immediately print it put and get empty data - just like you said!

Nowhere do you actually put the data into the the new object. Either you have to use the constructor with all the values, OR use the set... methods to add the data.

ps * "null" has a specific meaning in Java - your values are empty Strings, not nulls.

Where is the as follows???

main line 44
You create the pw using the no-args constructor which you document as follows:

You then immediately print it put and get empty data - just like you said!

Nowhere do you actually put the data into the the new object. Either you have to use the constructor with all the values, OR use the set... methods to add the data.

ps * "null" has a specific meaning in Java - your values are empty Strings, not nulls.

ProductionWorkerA lines 31,32

commented: Thanks for your guidance! +1

I have the set methods for name, employeeNumber, and hireDate in the Employee class.

Thanks for your guidance. I figured it out!!!!!!!!!!!!!

i still didnt get why it is not working !!! i mean what should he/she edit??

i still didnt get why it is not working !!! i mean what should he/she edit??

ProductionWorkerA pw = new ProductionWorkerA();

Initializes the object using the default constructor:

public ProductionWorkerA()
   {
      super();
      shift = DAY_SHIFT;
      payRate = 0.0;
   }

Since nothing is passed to the superclass, pw.getName(), getEmployeeNumber(), pw.getHireDate() are all empty strings (""). pw.getPayRate() and pw.getShift() have values because they are initialized in the default constructor for ProductionWorkerA.

The proposed solution is to pass the data you stored in the variables when creating the new object to use the constructor below instead:

public ProductionWorkerA(String n, String num, String date, int sh, double rate)

HTH.

I have 2 problems with my code now. The program is not checking the employee number through the isvalid method nor is it giving the invalid shift error. Is there anyone that can help me with this problem?

Hi blknmld69,

The program is not checking the employee number through the isvalid method

I've tried the code you have for checking the employee number and it works the way I think you want it to work (ie: true: "123-C", false: "123:C"). I don't see it being used at all in your demo class though. It's there, it works, but as far as I can see it's never used (that's if you're still using the same demo class posted above).

nor is it giving the invalid shift error

Same as above. The ProductionWorkerA's toString() is never invoked in the demo class. Try the System.out.println(productionWorkerObject); to see if you will get "Day", "Night", or "INVALID SHIFT NUMBER". It works for me :)

HTH.

The employee number should be 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.

The employee number should be 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.

... so "123-C" = true and "123:C" = false yes? If so, the code you already have to check the format already works. Why do you say "The program is not checking the employee number through the isvalid method"? Did you write a new demo class where you're using the isValidEmpNum() method?

I read through my post and it was actually confusing :$

The problem is really in the constructor of Employee (your superclass). If you've already made modifications to your ProductionWorkerDemo to pass the user input as parameters when creating a new ProductionWorkerA object, the constructor for Employee is just assigning the value to the field:

public Employee(String n, String e, String h)
      {
         name = n;
         employeeNumber = e;
         hireDate = h;
      }

You have perfectly working code to check the employee number but it's not being utilized. Instead the constructor will just assign any user input to employeeNumber. You have this method though:

public void setEmployeeNumber(String e)

Why not use it in the constructor of your Employee class instead of assigning 'e' to employeeNumber directly? Something like:

setEmployeeNumber(e);

Try it out and see if this works ;)

HTH.

I get no output for the employee number when i use the setEmployeeNumber(e);

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 + "\nEmployee Number: ";
      
         if (employeeNumber == "")
         {
            str += "INVALID EMPLOYEE NUMBER";
         }
         else
         {
            str += employeeNumber;
         }
         
         str += ("\nHire Date: " + hireDate);
         return str;
      }
   }

I get no output for the employee number when i use the setEmployeeNumber(e);

There are a few possibilities why this is happening.

1) EmployeeClass object is created with no parameters. In this case, the default constructor runs setting name, employeeNumber, and hireDate to an empty string:

public EmployeeClass()
      {
         name = "";
         employeeNumber = "";
         hireDate = "";
      }

2) EmployeeClass object is created with parameters. The alternate constructor runs and setEmployeeNumber() is invoked. Depending if the format is correct or not, there are two possible outcomes:

public void setEmployeeNumber(String e)
      {
         if (isValidEmpNum(e))
         {
            employeeNumber = e; // If isValidEmpNum() == true user input is assigned.
         }
         else
         {
            employeeNumber = ""; // If isValidEmpNum() == false empty string is assigned.
         }
      }

Maybe the object is created without parameters or the employee number is invalid which both result in an empty string for employeeNumber.

HTH.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.