2 Replies - 1811 Views - Last Post: 08 May 2011 - 08:10 PM Rate Topic: -----

#1 krajr3518  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 11-November 10

Java Driver Program uses array to test but gives input/output errors

Posted 06 May 2011 - 12:47 PM

Our instructor told us to write a driver program, but out textbook doesn't have any instruction about how to write the code for a driver program. I am working on creating the driver program that will test several programs that work together, using inheritance and polymorphism. All of my programs compile correctly, but the driver program's input and output has some errors that I'm not sure how to correct. The programs Employee, ProductionWorker5, Shift Supervisor4, and Team Leader are to be tested by the driver program EmployeeClassesDriver. The console's input and output for the driver program is giving inaccurate output. Here are the codes for the programs and and the driver/starting with the superclass Employee:

import java.util.*; 

public class Employee
{
		Scanner input = new Scanner(System.in);	
    /***************************************/
    //        Instance Variables
    /***************************************/

    private String              strEmpName;
    private String              strSSN;
    public static final int     SSN_SIZE = 9;
    private static final String DEFAULT_EMPLOYEE_NAME = "NO NAME GIVEN";
    private static final String DEFAULT_SSN           = "999999999";
	
	public static void main(String[] args)
	{
		Employee EmployeeIdNum = new Employee( "123456789" );
		System.out.println(EmployeeIdNum + "  Test No. 1"  );   //Test to see if constructor No. 1 is working
	
		Employee EmployeeId = new Employee( "987654321" ,  "John Smith" ); 
		System.out.println(EmployeeId + "  Test No. 2 ");	//Test to see if constructor No. 2 is working
	}
    /***************************************/
    //           Constructors
    /***************************************/

    public Employee( String p_strSSN )
    {
        this.setEmployeeSSN( p_strSSN );
        this.setEmployeeName( "" );
    }

    public Employee( String p_strSSN, String p_strName )
    {
        this.setEmployeeSSN(p_strSSN);
        this.setEmployeeName(p_strName);
    }

    /***************************************/
    //    Transformers or Mutators
    /***************************************/

    public void setEmployeeSSN( String p_strSSN )
    {
        if ( isValidSSN( p_strSSN ) )
            strSSN = p_strSSN;

        else
            strSSN = DEFAULT_SSN;
    }


    public void setEmployeeName( String p_strName )
    {
        //*** check validity of name and set accordingly  ***

        if (p_strName.length()>0)
            strEmpName = p_strName;

        else
            strEmpName = DEFAULT_EMPLOYEE_NAME;

    }

    /***************************************/
    //        Accessors
    /***************************************/

    public String getEmployeeSSN()
    {
        return strSSN;
    }

    public String getEmployeeName()
    {
        return strEmpName;
    }

    public String toString()
    {
        return "Employee --> SSN: " + this.getEmployeeSSN()  +
                         "  Name: " + this.getEmployeeName();
    }

    public double calculateWeeklyPay()
    {
        //this returns 0 because it MUST be overridden in subclasses of Employee
        // Company has some employees who volunteer, thus they get no pay

        return 0;
    }

    public boolean equals( Employee objEmp )
    {
        // if SSNs are equal, then Employees are equal

        if ( strSSN.equals( objEmp.getEmployeeSSN() ) )
            return true;

        else
            return false;
    }

    /***************************************/
    //        Helper Methods
    // Can be called by class or client code
    /***************************************/

		public static boolean isValidSSN( String p_strSSN )
		{
        //*** check validity of SSN ***

        boolean bolValidSSN = true;
        int intCounter = 0;

        if (p_strSSN.length() == SSN_SIZE)     // correct size
        {               // loop to check that all are digits
            while ( bolValidSSN && intCounter<p_strSSN.length() )
            {
                if ( !Character.isDigit(p_strSSN.charAt( intCounter ) ) )
                    bolValidSSN = false;            // didn't find a digit
                intCounter++;
            }
        }

        else                //not valid - incorrect size
            bolValidSSN = false;

        return bolValidSSN;
		}
}



Here is the code for the ProductionWorker5Program subclass that extends Employee:


import java.text.*;

public class ProductionWorker5 extends Employee
{
   /***class constants*********************************************************/
    public final static int DAY   = 1;            //first intShift, day intShift
    public final static int NIGHT = 2;            //second intShift, night intShift

    private final double HOURLY_NIGHT_BONUS = 1;  //bonus for second intShift ($)
    private final double OVERTIME_BONUS     = 1.5;//over time bonus (multiplier)
    private final double STANDARD_HOURS     = 40; //hours before overtime

    private final String NEWLINE = "\r\n";        //newline, carriage return

    private final Format money = NumberFormat.getCurrencyInstance(); //$0.00

   /****instance variables*****************************************************/
    int    intShift;             // intShift worked
    double dblHrRate;            // hourly rate of pay
    double dblHrsWorked;         // hours worked this week


   /****constructor methods****************************************************/
    public ProductionWorker5( String p_strSSN )
    /*
     * constructor, takes social security number
     */
    {
        super( p_strSSN );
    }//end constructor()

    public ProductionWorker5( String p_strSSN, String p_strName )
    /*
     * constructor, takes social security number and name
     */
    {
        super( p_strSSN , p_strName );
    }//end constructor()

   /****transformer methods****************************************************/
    public void setShift( int p_intShift )
    /*
     * set a valid intShift, if invalid do nothing
     */
    {
        switch( p_intShift )
        {
            case DAY  : intShift = DAY  ; break;
            case NIGHT: intShift = NIGHT; break;
            default   :                break;
        }
    }//end setShift()

    public void setHourlyPayRate( double p_dblHrRate )
    /*
     * set hourly pay rate if greater than or equal to zero
     */
    {
        if( p_dblHrRate >= 0 )
            dblHrRate = p_dblHrRate;
    }//end setHourlyPayRate()

    public void setHoursWorked( double p_dblHrsWorked )
    /*
     * set hours worked if greater than or equal to zero
     */
    {
        if( p_dblHrsWorked >= 0 )
            dblHrsWorked = p_dblHrsWorked;
    }//end setHoursWorked()


   /****accessor methods*******************************************************/
    public double getHourlyPayRate()
    /*
     * return hourly Pay Rate as a double, not formatted
     */
    {
        return dblHrRate;
    }//end getHourlyPayRate()

    public double getHoursWorked()
    /*
     * return hours worked as a double
     */
    {
        return dblHrsWorked;
    }//end getHoursWorked()

    public String getFormattedWeeklyPay()
    /*
     * format and return weekly pay
     */
    {
        return money.format( calculateWeeklyPay() );
    }//end getFormattedWeeklyPay()

    public double calculateWeeklyPay()
    /*
     * calculate and return the weekly pay
     */
    {
       double dblTotPay           = 0;      //total pay
       double dblRegHrs  = dblHrsWorked;     //regular paid hours
       double dblOTHrs = 0;                  //overtime hours
       double dblPayRate       = dblHrRate; //pay rate minus night bonus

       //if intShift is night, add the hourly night bonus to  total pay
       dblTotPay += ( intShift == NIGHT ? (dblHrsWorked) * HOURLY_NIGHT_BONUS : 0 );

       //if overtime hours were worked
       if( dblHrsWorked > STANDARD_HOURS )
       {
          //calculate the overtime hours
          dblOTHrs = dblHrsWorked - STANDARD_HOURS;

          //set the regular hours to the standard hours
          dblRegHrs  = STANDARD_HOURS;

          //calculate and add overtime pay to total pay
          dblTotPay += dblOTHrs * OVERTIME_BONUS * dblPayRate;
       }

       //add regular hours times pay rate to the total pay
       dblTotPay += dblRegHrs * dblPayRate;

       return dblTotPay;

    }//end calculateWeeklyPay()

    public String getEmployeeShift()
    /*
     * return as string the intShift the employee works
     */
    {
        //default to empty string
        String strShift = "";

        switch( intShift )
        {
            case   DAY: strShift = "day";   break;
            case NIGHT: strShift = "night"; break;
            default:                           break;
        }

        return strShift;
    }//end getEmployeeShift()

    public String toString()
    /*
     * overriding of the objects toString, returns information about employee
     */
    {
       StringBuffer string = new StringBuffer();

       string.append("             SSN: " + this.getEmployeeSSN()     +NEWLINE);
       string.append("            Type: Production Worker"            +NEWLINE);
       string.append("            Name: " + this.getEmployeeName()    +NEWLINE);
       string.append("           Shift: " + this.getEmployeeShift()   +NEWLINE);
       string.append(" Hourly Pay Rate: " + this.getHourlyPayRate()   +NEWLINE);
       string.append("    Hours Worked: " + this.getHoursWorked()     +NEWLINE);
       string.append("Total Weekly Pay: "+this.getFormattedWeeklyPay()+NEWLINE);

        return string.toString();
    }//end toString


   /****main method************************************************************/
    public static void main( String args[] )
    /*
     * main to test Production Worker
     */
    {
        ProductionWorker5 sonya = new ProductionWorker5( "123456789", "Sonya" );
        sonya.setHoursWorked( 60 );
        sonya.setShift( ProductionWorker5.NIGHT );
        sonya.setHourlyPayRate( 10 );
        System.out.println( sonya.toString() );

        ProductionWorker5 austin = new ProductionWorker5( "87654321" ); //test invalid ssn
        austin.setEmployeeName( "Austin" );
        austin.setHoursWorked( 60 );
        austin.setShift( ProductionWorker5.DAY );
        austin.setHourlyPayRate( 10 );
        System.out.println( austin.toString() );
        austin.setEmployeeSSN( "abc" ); //test invalid ssn
        System.out.println( austin.toString() );
        austin.setEmployeeSSN( "123123123" );

        //verify correct output for day intShift, 20 45 60 hrs
        austin.setHoursWorked( 20 );
        System.out.println( austin.toString() );

        austin.setHoursWorked( 45 );
        System.out.println( austin.toString() );

        austin.setHoursWorked( 60 );
        System.out.println( austin.toString() );


        //verify correct output for night intShift, 20 45 60 hrs
        sonya.setHoursWorked( 20 );
        System.out.println( sonya.toString() );

        sonya.setHoursWorked( 45 );
        System.out.println( sonya.toString() );

        sonya.setHoursWorked( 60 );
        System.out.println( sonya.toString() );

        //verify sonya is not equal to austin
        System.out.println( "Sonya equals Austin? " + sonya.equals( austin ) );

    }//end main()
}




Here is the code for the TeamLeader subclass that extends ProductionWorker5:




import java.text.*;

public class TeamLeader extends ProductionWorker5
{	
		//Declare constants
	private final double WEEKLY_BONUS  =  .05; 	//Number of weeks in the year 
				
		//Declare variables
	private double dblWeeklyBonus;					//to store the weekly bonus
	private int intTrainingHours;				   //to store the number of training hours
	private final double HOURLY_NIGHT_BONUS = 1;  //bonus for second intShift ($)
	private final double OVERTIME_BONUS     = 1.5;//over time bonus (multiplier)
	private final double STANDARD_HOURS     = 40; //hours before overtime
	private boolean     TrainingHoursRewardStatus;	 //to store training hours reward qualification status
	private final String NEWLINE = "\r\n";        //newline, carriage return

	private final Format money = NumberFormat.getCurrencyInstance(); //$0.00

		
		 ///constructor methods		    
	public TeamLeader( String p_strSSN )
		//constructor, takes social security number 
		{
        super( p_strSSN );
		}//end constructor()
		
	public TeamLeader( String p_strSSN, String p_strName )
		//constructor, takes social security number and name		
		{
        super( p_strSSN , p_strName );
		}//end constructor()

   /****transformer methods****************************************************/
    public void setShift( int p_intShift )
    //set a valid intShift, if invalid do nothing
    {
        switch( p_intShift )
        {
            case DAY  : intShift = DAY  ; break;
            case NIGHT: intShift = NIGHT; break;
            default   :                break;
        }
    }//end setShift()
	
	public void setTrainingHours( int p_TrainingHours )
    //set training hours 
    {
       intTrainingHours  = p_TrainingHours;
    }//end setTrainingHours()

    public void setHourlyPayRate( double p_dblHrRate )
    //set hourly pay rate if greater than or equal to zero
    {
        if( p_dblHrRate >= 0 )
            dblHrRate = p_dblHrRate;
    }//end setHourlyPayRate()

    public void setHoursWorked( double p_dblHrsWorked )
    //set hours worked if greater than or equal to zero
    {
        if( p_dblHrsWorked >= 0 )
            dblHrsWorked = p_dblHrsWorked;
    }//end setHoursWorked()
	
	
	public boolean setTrainingHoursRewardStatus( int p_intTrainingHours )
    //set reward for team leader if minimum training hours requirement is met
    {
        if( p_intTrainingHours >= 52 )
           return true;
		else
            return false;		
    }//end setTrainingHoursReward()

     //****accessor methods*******************************************************/
    public double getHourlyPayRate()
    //return hourly Pay Rate as a double, not formatted
    {
        return dblHrRate;
    }//end getHourlyPayRate()

    public double getHoursWorked()
    //return hours worked as a double
    {
        return dblHrsWorked;
    }//end getHoursWorked()

    public String getFormattedWeeklyPay()
    //format and return weekly pay
    {
        return money.format( calculateWeeklyPay() );
    }//end getFormattedWeeklyPay()

    public double calculateWeeklyPay()
    // calculate and return the weekly pay
    {
       double dblTotPay           = 0;             //total pay
       double dblRegHrs  = dblHrsWorked;   //regular paid hours
       double dblOTHrs = 0;             //overtime hours
       double dblPayRate       = dblHrRate; //pay rate minus night bonus

       //if intShift is night, add the hourly night bonus to  total pay
       dblTotPay += ( intShift == NIGHT ? (dblHrsWorked) * HOURLY_NIGHT_BONUS : 0 );

       //if overtime hours were worked
       if( dblHrsWorked > STANDARD_HOURS )
       {
          //calculate the overtime hours
          dblOTHrs = dblHrsWorked - STANDARD_HOURS;

          //set the regular hours to the standard hours
          dblRegHrs  = STANDARD_HOURS;

          //calculate and add overtime pay to total pay
          dblTotPay += dblOTHrs * OVERTIME_BONUS * dblPayRate;
       }

       //add regular hours times pay rate times Weekly Bonus to the total pay
		if ( TrainingHoursRewardStatus = true ){
			dblTotPay += dblRegHrs * dblPayRate * WEEKLY_BONUS;}
	   else{
			dblTotPay += dblRegHrs * dblPayRate;
		}
       return dblTotPay;

    }//end calculateWeeklyPay()

    public String getTeamLeaderShift()
    // return as string the intShift the TeamLeader works
    {
        //default to empty string
        String strShift = "";

        switch( intShift )
        {
            case   DAY: strShift = "day";   break;
            case NIGHT: strShift = "night"; break;
            default:                           break;
        }

        return strShift;
    }//end getTeamLeaderShift()

    public String toString()
    //overriding of the objects toString, returns information about TeamLeader
    {
       StringBuffer string = new StringBuffer();

       string.append("             SSN: " + this.getEmployeeSSN()     +NEWLINE);
       string.append("            Type: Team Leader"                   +NEWLINE);
       string.append("            Name: " + this.getEmployeeName()    +NEWLINE);
       string.append("           Shift: " + this.getTeamLeaderShift()  +NEWLINE);
       string.append(" Hourly Pay Rate: " + this.getHourlyPayRate()   +NEWLINE);
       string.append("    Hours Worked: " + this.getHoursWorked()     +NEWLINE);
       string.append("Total Weekly Pay: "+this.getFormattedWeeklyPay()+NEWLINE);

        return string.toString();
    }//end toString


   /****main method************************************************************/
    public static void main( String args[] )
   
   //main to test TeamLeader
    {
        TeamLeader Marge = new TeamLeader( "123456789", "Marge" );
        Marge.setHoursWorked( 60 );
        Marge.setShift( TeamLeader.NIGHT );
		Marge.setTrainingHours( 50 );
        Marge.setHourlyPayRate( 12 );
        System.out.println( Marge.toString() );

        TeamLeader Adam = new TeamLeader( "87654321" ); //test invalid ssn
        Adam.setEmployeeName( "Adam" );
		Adam.setTrainingHours( 52 );
        Adam.setHoursWorked( 60 );
        Adam.setShift( TeamLeader.DAY );
        Adam.setHourlyPayRate( 12 );
        System.out.println( Adam.toString() );
        Adam.setEmployeeSSN( "abc" ); //test invalid ssn
        System.out.println( Adam.toString() );
       
        System.out.println( Adam.toString() );

        Adam.setHoursWorked( 45 );
        System.out.println( Adam.toString() );

        Adam.setHoursWorked( 60 );
        System.out.println( Adam.toString() );


        //verify correct output for night intShift, 20 45 60 hrs
        Marge.setHoursWorked( 20 );
        System.out.println( Marge.toString() );

        Marge.setHoursWorked( 45 );
        System.out.println( Marge.toString() );

        Marge.setHoursWorked( 60 );
        System.out.println( Marge.toString() );

        //verify Marge is not equal to Adam
        System.out.println( "Marge equals Adam? " + Marge.equals( Adam ) );

    }//end main()
}



Here is the code for the ShiftSupervisorProgram that extends Employee:


import java.text.*;

public class ShiftSupervisor4 extends Employee
{
   //***class constants*********************************************************/
	private final double WEEKS_IN_YEAR = 52;	//constant for Number of weeks in the year 
    private final String NEWLINE = "\r\n"; 		 //constant for formatting output newline, carriage return
    private final Format money = NumberFormat.getCurrencyInstance(); //$0.00

   //****declare variables*****************************************************/
    private double AnnualSalary = 0;    				//to store AnnualSalary
    private double Bonus  = 0;   					//to store ProductionBonus
	private String strShift;				    	//store the shift//shift supervised

	
   //****constructor methods****************************************************/
     ///constructor methods		    
    public ShiftSupervisor4( String p_strSSN )
		//constructor, takes social security number 
		{
        super( p_strSSN );
		}//end constructor()

    public ShiftSupervisor4( String p_strSSN, String p_strName )
    //constructor, takes social security number and name
		{
			super( p_strSSN , p_strName );
		}//end constructor()

   /****transformer methods****************************************************/
  public void setShift( String p_strShift )  
    //set a valid intShift  
		{  
			strShift  = p_strShift;   
		}//end setShift()  

    public void setAnnualSalary( double p_AnnualSalary )
    //Set the AnnualSlary if greater than zero
		{
			if( p_AnnualSalary > 0 )
				AnnualSalary = p_AnnualSalary;
		}//end setSalary()

    public void setBonus( double p_Bonus )
		//set the Bonus if greater than zero
		{
			if( p_Bonus > 0 )
				Bonus = p_Bonus;
		}//end setBonus()


   /****accessor methods*******************************************************/
    public double getAnnualSalary()
		//return dblAnnualSalary as a double, not formatted
		{
			return AnnualSalary;
		}//end getdblAnnualSalary()

    public double getBonus() 
		//return double dblProductionBonus
		{
			return Bonus;
		}//end getdblBonus()
	
	 public String getShift()
		//return as string the intShift 
		{
			return strShift;
		}//end getShiftSupervisorShift()

    public String getFormatteddblAnnualSalary()
		//return salary as formatted string
		{
			return money.format( AnnualSalary );
		}//end getFormattedSalary()

    public String getFormattedBonus()
		//return dblBonus as a formatted string
		{
			return money.format( Bonus );
		}// getFormattedBonus()

    public String getFormattedWeeklyPay()
		//format and return weekly pay
		{
			return money.format( calculateWeeklyPay() );
		}//end getFormattedWeeklyPay()

    public double calculateWeeklyPay()
		//calculate and return the weekly pay
		{
			return AnnualSalary / WEEKS_IN_YEAR;
		}//end calculateWeeklyPay()

    public String toString()
    //overriding of the objects toString, returns information about employee
    {
       StringBuffer string = new StringBuffer();

       string.append("                    SSN: " 	+ this.getEmployeeSSN()    			+NEWLINE);
       string.append("                   Type: Shift Supervisor"           				+NEWLINE);
       string.append("                   Name: " 	+ this.getEmployeeName()   			+NEWLINE);
	   string.append("                  Shift: "    + this.getShift()					+NEWLINE);
       string.append("          Annual Salary: " 	+ this.getAnnualSalary()        	+NEWLINE);
       string.append("                  Bonus: " 	+ this.getBonus()                   +NEWLINE);
       string.append("       Total Weekly Pay: "	+this.getFormattedWeeklyPay()		+NEWLINE);

       return string.toString();
    }//end toString


   /****main method************************************************************/
    public static void main( String args[] )
	//main to test ShiftSupervisor4
    {
		ShiftSupervisor4 Melody = new ShiftSupervisor4( "123454321", "Melody" );
		Melody.setShift( "Day" );
        Melody.setBonus( 3225 );
        Melody.setAnnualSalary( 115000 );
		System.out.println( Melody.toString()   );

        ShiftSupervisor4 Paul = new ShiftSupervisor4( "376123457" );   
		Paul.setEmployeeName( "Paul" );
		Paul.setShift( "Night" );
		Paul.setAnnualSalary( 110352 );
        Paul.setBonus( 3151 );
		System.out.println( Paul.toString() );
		
        //verify Melody is not equal to Paul
        System.out.println( "Melody equals Paul? " + Melody.equals( Paul ) );
    
    }//end main()
}




Here is the code for the EmployeeClassesDriver that tests all of the programs, which is the one that is giving the inaccurate output:


import java.text.Format;
import java.text.NumberFormat;
import java.util.Scanner; 


public class  EmployeeClassesDriver
{

	//****instance variables*****************************************************/
    Employee[] CompanyEmployee = new Employee[9];   //array to hold Employees
    int selectedEmployee = 0;                       //count of all Employees
    Scanner Input;                                  //user Input

    private final Format money = NumberFormat.getCurrencyInstance(); //$0.00
	
	//****helper methods*********************************************************/
    public void run()
    {
	
        Input = new Scanner( System.in);

        //create Employees
        CompanyEmployee[ selectedEmployee ] = createEmployee();
        CompanyEmployee[ selectedEmployee ] = createProductionWorker5();
        CompanyEmployee[ selectedEmployee ] = createShiftSupervisor4();
        CompanyEmployee[ selectedEmployee ] = createTeamLeader();
        CompanyEmployee[ selectedEmployee ] = createProductionWorker5();

        double totalWagesPaid = 0; //holds all paid in wages this week

        try{
        //call calculate pay and add to total until Employee doesn't exist
            for( int i = 0; i < CompanyEmployee.length  ; i++ )
                totalWagesPaid += calculatePay( CompanyEmployee[ i ] );
        }catch(Exception e){
        }finally{
             System.out.println(">>Total paid in wages for this week: "
                                + money.format( totalWagesPaid) );
        }
		
		 try{
        //display Employee info until Employee doesn't exist(end of list)
            System.out.println("***Employee Summary***");

            for( int i = 0; i <= selectedEmployee; i++ )
            {
                System.out.println( i );
                displayEmployeeInfo( CompanyEmployee[ i ] );
            }
        } catch( Exception e ){ }
    }//end run()

	private Employee createEmployee()
	 //prompt, create, and return Input from user
	 {
        Employee Employee;

         try{
             System.out.println( "*New Employee" );
            prompt( "            SSN: " );
                Employee = new Employee( Input.next() );
                check( Employee );
            prompt( "           Name: " );
                Employee.setEmployeeName( Input.next() );

                //increment Employee count
                selectedEmployee++;

                return Employee;

        }catch( Exception e ){
        //print exception, then recall method
            System.out.println( e.getMessage() );
            return createEmployee();
        }

    }//end createEmployee()
	
	 private ProductionWorker5 createProductionWorker5()
    //prompt, create, and return ProductionWorker5 from Input
    {
        ProductionWorker5 Employee;

        try{
            System.out.println( "*New Production Worker" );
            prompt( "            SSN: " );
                Employee = new ProductionWorker5( Input.next() );
                check( Employee );
            prompt( "           Name: " );
                Employee.setEmployeeName( Input.next() );
            prompt( "   Hours Worked: " );
                Employee.setHoursWorked( Input.nextDouble() );
            prompt( "          Shift: " );
                Employee.setShift( Input.nextInt() );
            prompt( "     Hourly Pay: " );
                Employee.setHourlyPayRate( Input.nextDouble() );

                //increment Employee count
                selectedEmployee++;

                return Employee;

        }catch( Exception e ){
        //print exception, then recall current method
            System.out.println( e.getMessage() );
            return createProductionWorker5();
        }
    }// end createProductionWorker5()

	
	 private ShiftSupervisor4 createShiftSupervisor4()
    //prompt, create, and return Shift Supervisor from Input
    {
        ShiftSupervisor4 Employee;

        try{
             System.out.println( "*New Shift Supervisor" );
            prompt( "            SSN: " );
                Employee = new ShiftSupervisor4( Input.next() );
                check( Employee );
            prompt( "           Name: " );
                Employee.setAnnualSalary( Input.nextDouble() );
            prompt( "Production Bonus: " );
                Employee.setBonus( Input.nextDouble() );

            //increment number of Employees
            selectedEmployee++;

            return Employee;

        }catch( Exception e ){
        //print exception then recall current method
            System.out.println( e.getMessage() );
            return createShiftSupervisor4();
        }

    }//end createShiftSupervisor4()
	

	
    private TeamLeader createTeamLeader()
    //prompt create and return team leader
    {
            TeamLeader Employee;

          try{
            System.out.println( "New Team Leader" );
            prompt( "            SSN: " );
                String strSSN = Input.next();
            prompt( " Training Hours: " );
                Employee = new TeamLeader( strSSN , Input.next() );
                check( Employee );
            prompt( "           Name: " );
                Employee.setEmployeeName( Input.next() );
            prompt( "   Hours Worked: " );
                Employee.setHoursWorked( Input.nextDouble() );
            prompt( "          Shift: " );
                Employee.setShift( Input.nextInt() );
            prompt( "     Hourly Pay: " );
                Employee.setHourlyPayRate( Input.nextDouble() );

            //increment number of Employees
            selectedEmployee++;

            return Employee;

        }catch( Exception e ){
        //print exception and recall method
           System.out.println( e.getMessage() );
           return createTeamLeader();
       }

    }//end createTeamLeader
	
	
	 private void check( Employee Employee ) throws Exception
    //check all previous Employees to ensure that the accepted Employee does not equal it.  
	//If it is, throw exception
    {
        for( int i = selectedEmployee-1; i >= 0 ; i-- )
        {
            if( Employee.equals(CompanyEmployee[i]) )
            {
               throw new Exception( "ERROR: Employee exists" );
            }
        }
    }//end check()

    private void prompt( String strTxt )
    //prompt text using System.out.print
		{
			System.out.print( strTxt );
		}//end prompt

    private double calculatePay( Employee Employee )
		//calculate the weekly pay for an Employee
		{
			return Employee.calculateWeeklyPay();
		}//end calculatePay()

    private void displayEmployeeInfo( Employee Employee )
		//display Employee info using toString()
		{
			System.out.println( Employee.toString() );
		}//end displayEmployeeInfo()

   /****main method************************************************************/
    public static void main( String args[] )
    // main
    {
         EmployeeClassesDriver test = new EmployeeClassesDriver();
         test.run();
    }//end main()
}


Since I have never created an array befor, I don't know if I have used the proper coding for setting up the array. Here is a sample of some of the input/output from the console of Eclipse when I ran this test program:
*New Employee
SSN: 123456896
Name: John
*New Production Worker
SSN: 569874123
Name: Emily
Hours Worked: 40
Shift: DAY
null
*New Production Worker
SSN: Name: Jacob
Hours Worked: 45
Shift: day
null
*New Production Worker
SSN: Name: Rosa
Hours Worked: 38
Shift: NIGHT
null
*New Production Worker
SSN: Name: Rosa
Hours Worked: 38
Shift: night
null
*New Production Worker
SSN: Name: Rosa
Hours Worked: 38
Shift: 2
Hourly Pay: 10
*New Shift Supervisor
SSN: 123569874
Name: Thomas
null
*New Shift Supervisor
SSN: ERROR: Employee exists
*New Shift Supervisor
SSN: 365896743
Name: Thomas
null
*New Shift Supervisor
SSN: ERROR: Employee exists
*New Shift Supervisor
SSN: 895321657
Name: Larry
null
*New Shift Supervisor
SSN: ERROR: Employee exists
*New Shift Supervisor
SSN: 567893124
Name: Mark
null
*New Shift Supervisor
SSN: ERROR: Employee exists
*New Shift Supervisor
SSN: 234659782
Name: Amie
null
*New Shift Supervisor
SSN: ERROR: Employee exists
*New Shift Supervisor
SSN: 235963754
Name: Pinto
null
*New Shift Supervisor
SSN: ERROR: Employee exists
*New Shift Supervisor
SSN:

Is This A Good Question/Topic? 0
  • +

Replies To: Java Driver Program uses array to test but gives input/output errors

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java Driver Program uses array to test but gives input/output errors

Posted 07 May 2011 - 12:03 AM

A Driver program is just an external program use to test your classes

Write a class TestEmployee cut and paste into it your main() method from your Employee class
Et voila

class EmployeeDriver {

	public static void main(String[] args)
	{
		Employee EmployeeIdNum = new Employee( "123456789" );
		System.out.println(EmployeeIdNum + "  Test No. 1"  );   //Test to see if constructor No. 1 is working
	
		Employee EmployeeId = new Employee( "987654321" ,  "John Smith" ); 
		System.out.println(EmployeeId + "  Test No. 2 ");	//Test to see if constructor No. 2 is working
	}
}


You can remove your main() from Employee or leave it there
Good programming practice will leave it there.
It is always a good idea to have a main() method in all classes to perform unit tests on your class as you do by testing the two constructors.Later on the main() method of your EmployeeDriver class can evolve and be used to test the interrelations between Employee and another class.

Happy coding
Was This Post Helpful? 0
  • +
  • -

#3 krajr3518  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 11-November 10

Re: Java Driver Program uses array to test but gives input/output errors

Posted 08 May 2011 - 08:10 PM

Thank you for your reply. I worked on the code in each of my classes and closely examined my Driver Program and did get them all to work properly.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1