#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    6
    Rep Power
    0

    Employee class for EmployeeTest program


    I have a homework assignment that requires me to define a new class called Employee and a program to test that class called EmployeeTest. I have posted what I have done so far and what the EmployeeTest class looks like. I can't wrap my head around this and I have done to much to it to even figure this out. Please help.

    PHP Code:
    import static java.lang.Math.abs;
    import static java.lang.System.out;

    /**
     * Class to test class Employee.
     *
     * @author    Dr. Bruce K. Haddon, Instructor
     * @version    2012-08-20, CSC-240 Assignment 2, Exercise 3.14
     */
    public class EmployeeTest
    {
    /**
     * Th initial monthly salary for the first employee
     */
    private static final double EMPLOYEE1_MONTHLY_SALARY 2985.00;

    /**
     * Th initial monthly salary for the second employee
     */
    private static final double EMPLOYEE2_MONTHLY_SALARY 3361.75;

    /**
     * Main entry point.
     * <p>
     * Execute:
     * <pre>java edu.frontrange.csc240.a2.EmployeeTest</pre>
     *
     * @param args        unused
     */
    public static void main(String[] args)
    {
        new 
    EmployeeTest().run();        // Run the test
    }

    /**
     * Do the work for the EmployeeTest.
     */
    void run()
    {
        
    /* Create some empty strings which are not the constant "". */
        
    String emptyString1 = new String();
        
    String emptyString2 = new String();
        
    String emptyString3 = new String();

        
    /* Check the compiler has not done too much optimization. */
        
    assert !( (Object) emptyString1 == (Object) emptyString2 ||
                    (Object) 
    emptyString1 == (Object) "" ||
                        (Object) 
    emptyString2 == (Object) emptyString3 ||
                            (Object) 
    emptyString2 == (Object) "" ||
                                (Object) 
    emptyString3 == (Object) emptyString1 ||
                                    (Object) 
    emptyString3 == (Object) "" ):
                            
    "**?? Empty test strings not unique!";

        
    /* This try block is intended to catch any exceptions that may occur.
           Of course, there should not be any.  */
        
    try
        {
            
    /* This is required to ensure that the output directed to the err file
               appears in the correct sequence with the output directed to the out
               file. Otherwise the two files are independent, and the output does
               not necessarily appear in the correct time order. */
            
    System.setErr(out);

            
    out.println("**CSC-240 Assignment 2");
            
    out.println();
            
    out.println("**TESTING CREATING VALID Employee OBJECTS");
            
    out.println();

            
    /* Instantiate two valid Employee objects. */
            
    out.println("**Creating two valid employees");
            
    Employee employee1 =
                    new 
    Employee("Bob""Jones"EMPLOYEE1_MONTHLY_SALARY);
            
    checkEmployee(employee1"Bob""Jones"EMPLOYEE1_MONTHLY_SALARY);


            
    Employee employee2 =
                    new 
    Employee("Susan""Baker"EMPLOYEE2_MONTHLY_SALARY);
            
    checkEmployee(employee2"Susan""Baker"EMPLOYEE2_MONTHLY_SALARY);

            
    /* Increase employee salaries by 10%. */
            
    out.println();
            
    out.println("**INCREASING Employee SALARIES BY 10%");

            
    /* Increase the salary of employee1 by 10% */
            
    employee1.setMonthlySalary(employee1.getMonthlySalary() * 1.10);
            
    /* Check that the salary increase was correctly recorded. */
            
    checkEmployee(employee1"Bob""Jones"EMPLOYEE1_MONTHLY_SALARY 1.10 );

            
    /* Increase the salary of employee2 by 10% */
            
    employee2.setMonthlySalary(employee2.getMonthlySalary() * 1.10);
            
    /* Check that the salary increase was correctly recorded. */
            
    checkEmployee(employee2"Susan""Baker"EMPLOYEE2_MONTHLY_SALARY 1.10);

            
    /* Test that bad Employee values cannot be set. */
            
    out.println();
            
    out.println("**TESTING INVALID CALLS TO Employee METHODS:");

            
    /* Try to set some non-acceptable values. */
            
    out.println("**Setting last name of Bob Jones to \"\"");
            
    employee1.setLast(emptyString1);
            if( 
    employee1.getLast() != null )
                
    out.println("**?? Erroneous \"setLast\": " "did not result in null value");
            
    checkEmployee(employee1"Bob"nullEMPLOYEE1_MONTHLY_SALARY 1.10);

            
    out.println("**Setting first name of Bob Jones to null");
            try
            {
                
    employee1.setFirst(null);
            } catch( 
    NullPointerException npe )
            {
                
    out.println("**?? Erroneous \"setFirst\": " "resulted in NullPointerException");
            }
            if( 
    employee1.getFirst() != null )
                
    out.println("**?? Erroneous \"setFirst\": " "did not result in null value");
            
    checkEmployee(employee1nullnullEMPLOYEE1_MONTHLY_SALARY 1.10);

            
    out.println("**Setting first name of Susan Baker to \"\"");
            
    employee2.setFirst(emptyString2);
            if( 
    employee2.getFirst() != null )
                
    out.println("**?? Erroneous \"setFirst\": " "did not result in null value");
            
    checkEmployee(employee2null"Baker"EMPLOYEE2_MONTHLY_SALARY 1.10);


            
    out.println("**Setting last name of Susan Baker to null");
            try
            {
                
    employee2.setLast(null);
            } catch( 
    NullPointerException npe )
            {
                
    out.println("**?? Erroneous \"setLast\": " "resulted in NullPointerException");
            }
            if( 
    employee2.getLast() != null )
                
    out.println("**?? Erroneous \"setLast\": " "did not result in null value");
            
    checkEmployee(employee2nullnullEMPLOYEE2_MONTHLY_SALARY 1.10);

            
    out.println("**Setting salary of Susan Baker to -1000.0");
            
    employee2.setMonthlySalary(-1000.0);
            if( 
    employee2.getMonthlySalary() != 0.0 )
                
    System.out.println(
                    
    "**?? Erroneous \"setMonthlySalary\": " "did not result in zero value");
            
    checkEmployee(employee2nullnull0.0);

            
    /* Try to create Employee objects with non-acceptable initial values. */
            
    out.println("**Creating \"\" Smith, with salary of -500.0");
            
    Employee employee3 = new Employee(emptyString3"Smith", -500.0);
            
    checkEmployee(employee3null"Smith"0.0);
            if( 
    employee3.getFirst() != null )
                
    out.println("**?? Erroneous constructor (firstName): " "did not result in null value");
            if( 
    employee3.getMonthlySalary() != 0.0 )
                
    out.println(
                    
    "**?? Erroneous constructor (salary): " "did not result in zero value");

            
    out.println("**Creating <null> Baker");
            
    Employee employee4 null;
            try
            {
                
    employee4 = new Employee(null"Baker"500.0);
            } catch( 
    NullPointerException npe )
            {
                
    out.println("**?? Erroneous constructor (firstName): " "resulted in NullPointerException");
            }
            if( 
    employee4 != null)
            {
                
    checkEmployee(employee4null"Baker"500.0);
                if( 
    employee4.getFirst() != null )
                    
    out.println("**?? Erroneous constructor (firstName): " "did not result in null value");
                if( 
    employee4.getMonthlySalary() != 500.0 )
                    
    out.println(
                        
    "**?? Erroneous \"setMonthlySalary\": " "did not result in set value");
            }
        }
        catch( 
    Throwable t )
        {
            
    /* The application under test should not throw any Exceptions or Errors.
               The use of Exceptions will not be studied until a later lesson, but
               they are nevertheless an inescapable part of the Java programming
               system. */
            
    out.println("**?? An exception or error has been thrown");
            
    out.println("**?? The exception is: " t.toString());
            
    out.println("**?? The message in the exception is: \"" t.getMessage()
                    + 
    "\"");
            
    out.println("**?? The source of the error can be found " +
                                                
    "in this stacktrace:");
            
    t.printStackTrace(out);
        }
    }

    /**
     * Check that the given employee has the given attributes. Used to check that
     * displayValues does not have unexpected side effects.
     *
     * @param given            the given Employee object (which is not null)
     * @param firstName        the expected value to be returned by getFirst
     * @param lastName        the expected value to be returned by getLast
     * @param salary        the expected value to be returned by getMonthlySalary
     */
    private void checkEmployee(Employee givenString firstNameString lastName,
            
    double salary)
    {
        
    boolean OK true;

        
    /* Identify what can be identified of the employee. */

        
    out.print("**Checking the employee ");
        
    out.print(firstName == null "<null>" :
                            
    firstName.isEmpty() ? "<empty>" firstName);
        
    out.print(" ");
        
    out.print(lastName == null "<null>" :
                            
    lastName.isEmpty() ? "<empty>" lastName);
        
    out.println();

        
    /* If both the actual and the expected name are null, then OK, otherwise
           if the expected value is null (and thus the actual found value was not)
           report this, otherwise check to see if they are equal, and if not,
           report that. */
        
    if( given.getFirst() != null || firstName != null )

            if( 
    firstName == null )
            {
                
    OK false;
                
    out.printf("**?? Erroneous: First name expected to be null,"
                        
    " found to be %s%n"given.getFirst());
            }
            else if( !
    firstName.equals(given.getFirst()) )
            {
                
    OK false;
                
    out.printf("**?? Erroneous: First name expected to be %s,"
                        
    " found to be %s%n"firstNamegiven.getFirst());
            }


        
    /* If both the actual and the expected name are null, then OK, otherwise
           if the expected value is null (and thus the actual found value was not)
           report this, otherwise check to see if they are equal, and if not,
           report that. */
        
    if( given.getLast() != null || lastName != null )
        {
            if( 
    lastName == null )
            {
                
    OK false;
                
    out.printf("**?? Erroneous: Last name expected to be null,"
                        
    " found to be %s%n"given.getLast());
            }
            else if( !
    lastName.equals(given.getLast()) )
            {
                
    OK false;
                
    out.printf("**?? Erroneous: Last name expected to be %s,"
                        
    " found to be %s%n"lastNamegiven.getLast());
            }
        }

        
    /* Check to see if the salary value is close enough to what is expected
           If not, report it. */
        
    if( abs(given.getMonthlySalary() - salary) > 0.0001 )
        {
            
    OK false;
            
    out.printf("**?? Erroneous: Monthly salary expected to be %.2f,"
                    
    " found to be %.2f%n"salarygiven.getMonthlySalary());
        }

        if( 
    OK )
        {
            
    out.print("**");
            
    out.print(firstName == null "<null>" :
                            
    firstName.isEmpty() ? "<empty>" firstName);
            
    out.print(" ");
            
    out.print(lastName == null "<null>" :
                            
    lastName.isEmpty() ? "<empty>" lastName);
            
    out.println(" is OK");
        }

        
    /* Check that displayValue gives an acceptable result. */
        
    out.println("**The result of \"displayValues\" for that employee");
        
    given.displayValues();
    }

    PHP Code:
      public class Employee 
     
    {
     
         
          private 
    String firstNameField;
          private 
    String lastNameField;
          private 
    Double salaryField;
          
          
          public 
    Employee (String firstNameString lastNamedouble initialSalary)//these describe those objects
                
    {
                   {
                         
    //object          //variable
                         
    firstNameField firstName;
                         
                         
    lastNameField lastName;                   
                         
                         
    salaryField initialSalary;
                         
                         
                                      
                   
     
                   }
             } 
    // end constructor
          
               // method to set first name
           
          
    public void setFirst (String firstName)
                   
                 {
                      
    firstNameField firstName;
                 } 
    // end method setFirstName
           
               // method to set last name      
           
    public void setLast (String lastName)
                {
                     
    lastName null;
                         
                } 
    // end method setLastName
               
               // method to setmonthly salary
           
           
    public String variableTest (String firstNameString lastNamedouble initialSalary)
                {
                      if (
    firstNameField.length() == 0)
                         
    firstNameField firstName;
                      
    System.out.printf ("Error in setting Employee object: lastname is a null or empty string \n");
                      
                      if ( 
    firstNameField != null )
                         
    firstNameField null;
                      
    System.out.printf ("Error in setting Employee object: lastname is a null or empty string \n");

                      
                      if ( 
    firstNameField == null)
                         return 
    firstNameField;
                      
    System.out.printf ("Error in setting Employee object: lastname is a null or empty string \n");
                               
    //one of these should return getfirst
                    
                             
    return firstNameField;  
                                
                 }
      
          public 
    String variableTest2 (String lastName)
                 {      
                                      
                            if (
    lastName == null || lastName.isEmpty())
                        
    System.out.printf ("Error in setting Employee object: lastname is a null or empty string");
                                
                                return 
    lastNameField;
                 }
          public 
    double variableTest3 (double initialSalary)
          {
                  if (
    initialSalary >= 0.00)                             
                     
    salaryField initialSalary;
                  
                  if (
    initialSalary == 0.00 || initialSalary <= 0.00)
                      return 
    salaryField;
                  
                   return 
    salaryField;
          }
          
           public 
    double setMonthlySalary(double initialSalary)
                {
                   return 
    salaryField
                } 
    // end method setLastName
              
               // method toretrieve first name       
           
    public String getFirst()
                {
                      return 
    firstNameField;
                } 
    // end method getFirstName
           
               // method toretrieve last name      
           
    public String getLast()
                {
                      return 
    lastNameField;
                } 
    // end method getFirstName
              
               // method toretrieve first name            
           
    public double getMonthlySalary()
                {
                      return 
    salaryField;
                } 
    // end method getMonthlySalary

              // method to display employee information to user       
           
    public void displayValues()
                {
                      
    System.out.printf("Employee %s"variableTest(String firstName) , "%s:",getLast(), "MonthlySalary: $%.2f"getMonthlySalary(), "Yearly Salary: $%.2fn"getMonthlySalary() * 12 );
                } 
    // end method displayMessage 

      
      
      
    // end class Employee 
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Can you explain what you are having problems with?
    If you are getting errors, please post the full text of the error messages.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    6
    Rep Power
    0
    The errors are what I am supposed to generate if a string is null or empty. the problem I'm having is writing a method to test the variables. Everything with a **?? are the problems that I am having. below is the output from the IDE. I know what I am trying to achieve but I am having trouble with the if statements that my method uses to test the variables.

    PHP Code:
    **CSC-240 Assignment 2

    **TESTING CREATING VALID Employee OBJECTS

    **Creating two valid employees
    **Checking the employee Bob Jones
    Error in setting Employee object
    first name is a null or empty string 

    Error in setting Employee object
    first name is a null or empty string 

    Error in setting Employee object
    last name is a null or empty string 
    Error in setting Employee object
    last name is a null or empty string 
    **?? An exception or error has been thrown
    **?? The exception isjava.lang.NullPointerException
    **?? The message in the exception is"null"
    **?? The source of the error can be found in this stacktrace:
    java.lang.NullPointerException
        at Employee
    .variableTestLast(Employee.java:86)
        
    at Employee.getLast(Employee.java:57)
        
    at EmployeeTest.checkEmployee(EmployeeTest.java:247)
        
    at EmployeeTest.run(EmployeeTest.java:76)
        
    at EmployeeTest.main(EmployeeTest.java:35)
    BUILD SUCCESSFUL (total time0 seconds
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    6
    Rep Power
    0
    here is my other attempt at writing the methods and the output in my last reply is the result of the test program trying to use this latest attempt below.

    PHP Code:
      public class Employee 
     
    {
     
         
         private 
    String firstNameField,//he is typing these to create his programs objects
                        
    lastNameField;
         private 
    double salaryField;
          
          
           public 
    Employee (String firstNameString lastNamedouble initialSalary)//these describe those objects
                
    {
                   {
                         
    //object          //variable
                         
    firstNameField firstName;
                         
                         
    lastNameField lastName;                   
                         
                         
    salaryField initialSalary;
                   }
                } 
    // end constructor
          
               // method to set first name
           
           
    public void setFirst (String firstName)
                   
                {  
                      
    firstNameField firstName;
                } 
    // end method setFirstName
           
               // method to set last name      
           
    public void setLast (String lastName)
                {
                     
    lastNameField lastName;
                         
                } 
    // end method setLastName
               
               // method to setmonthly salary      
           
    public double setMonthlySalary(double initialSalary)
                {
                   return 
    salaryField
                } 
    // end method setLastName
              
               // method toretrieve first name       
           
    public String getFirst()
                {
                      return 
    variableTestFirst();
                } 
    // end method getFirstName
           
               // method toretrieve last name      
           
    public String getLast()
                {
                      return 
    variableTestLast();
                } 
    // end method getFirstName
              
               // method toretrieve first name            
           
    public double getMonthlySalary()
                {
                      return 
    variableTestSalary();
                } 
    // end method getMonthlySalary
           
           
    public String variableTestFirst ()
                {
                      
                      if (
    firstNameField.length() == 0)
                         return 
    firstNameField;
                     
                      if ( 
    firstNameField == null )
                         return 
    firstNameField;
                      
    System.out.println ("Error in setting Employee object: first name"
                                         
    " is a null or empty string \n");
                
                      
                   
                      
                               
    //one of these should return getfirst
                      
    return firstNameField;
                }
                             
           public 
    String variableTestLast () 
                {
                      if (
    lastNameField.length() == 0)
                         
    lastNameField null;
                      
    System.out.printf ("Error in setting Employee object: last name"
                                         
    " is a null or empty string \n");
                     
                      if ( 
    lastNameField != null )
                         
    lastNameField null;
                      
    System.out.printf ("Error in setting Employee object: last name"
                                         
    " is a null or empty string \n");
                
                      
                      if ( 
    lastNameField == null)
                         return 
    lastNameField;
                      
    System.out.printf ("Error in setting Employee object: "
                                         
    "last name is a null or empty string \n");
                               
    //one of these should return getfirst
                                                           
                      
    return lastNameField;
                }      
               
           public 
    double variableTestSalary ()
                {
                   if (
    salaryField <= 0.0)
                     
    salaryField 0.0;
                  
                   if (
    salaryField == 0.0)
                     return 
    salaryField;
                  
                     return 
    salaryField;
                  
                  
                }
      
              
    // method to display employee information to user       
           
    public void displayValues()
                {
                      
    System.out.printf("Employee %s"getFirst() , "%s:",getLast(), "MonthlySalary: $%.2f"getMonthlySalary(), "Yearly Salary: $%.2fn"getMonthlySalary() * 12 );
                } 
    // end method displayMessage 

      
      
      
    // end class Employee 
  8. #5
  9. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    I don't recognize anything in the error messages you posted before this part:
    java.lang.NullPointerException
    at Employee.variableTestLast(Employee.java:86)
    That error says there is a variable on line 86 that has a null value. Find line 86 and see what variable is null and then backtrack to see why it does not have a valid, non-null value.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    6
    Rep Power
    0
    here is the actual assignment.

    PHP Code:
    CSC-240-500 Lesson 2Classes and Objects

    Assignment 2

    Please 
    do Chapter 3 Exercise 3.14 (Employee Class).

    Submit just one file for this assignment.

    Employee.java—file that defines the Employee class.
    In this exercise you will write the code for the class named EmployeeYou do not have to write a test programa test program is provided that you may downloadThis test program creates Employee objects as described in the textbook, and also does the actions to increase the salariesPlease use the examples of the Chapter as a guide to implementing the Employee class. In addition to the features mentioned in the Exercise as described in the textbookalso write:

    A method in Employee named displayValues that displays on the standard output file the employee's complete name, monthly salary, and annual salary. The test program will use calls to that method to display Employee object values.
    The code needed to report errors found in calls made to the constructor or methods of the class. These error messages should be written to the standard output file.
    The test program may be downloaded by using this link to the zip file containing the source code for the EmployeeTest class (see Postscript). You will see that the test program has been assigned to the package edu.frontrange.csc240.a2. You may place your Employee class in the same package, that is, in the same folder with the same import statement as you see in the test program, or, if you are not using packages (which you do not need to do at this time, as this topic has not been covered) then simply place the test program .java file in the same folder as your Employee class file, and delete or change the line starting with package….

    In the example output below, all the lines starting with “**” are lines created by the test program. Your Employee class should generate all the other lines (follow the code through the test program to see what is happening). Lines printed by the test program starting with “**??” are messages indicating an error that the test program has found in the implementation of the class Employee (an error that you should correct).

    Examples of Testing the Employee Class

    The problem effectively defines a class whose UML diagram for public methods and private fields would look like this:

    Employee

    - firstNameField: String
    - lastNameField: String
    - salaryField: double

    +<constructor> Employee(firstName: String, lastName: String, initialSalary: double)
    + setFirst(firstName: String)
    + setLast(lastName: String)
    + setMonthlySalary(salary: double)
    + getFirst(): String
    + getLast(): String
    + getMonthlySalary(): double
    + displayValues()

    You may, if you wish or need to, add private methods of your own, as well as additional private fields.

    Defining the behavior of the Employee class

    Verify the salary value when it is being set that the salary value is not negative. If the salary given is invalid (i.e., is negative), the salary for the employee should be set to zero. You should avoid programming the code for this check more than once. Having more than one copy of code that does the verification is not only wasteful of programming time, but it is also a source of future “bugs” in the code. In a real setting, the rules on verification of salaries could change, and when there are multiple versions of the verification, it becomes an error-prone process to find all the copies of the verification, and then to change each one in an absolutely consistent way. With just one copy, used by all the places that need the verification to be done, then changes can be made once and tested uniformly.
    Similarly, you should check to ensure the Strings used to represent the names of the employee are not null and are not empty. The same considerations about having multiple instances of the checking code apply. You may determine whether a given reference to a String is null using the test “stringVariable == null”. You can determine the length of a String using stringVariable.length(), so you may use this in a test to ensure that you do not set a first or last name to a String of length zero. (The String method isEmpty may also be used for the same purpose—more details are discussed in the “FAQ Why is a null String not an empty String” in the Informational Resources module.) If the last name is invalid, then the variable lastNameField should be set to the value null, and when displayed using the displayValues method,, the message “<Invalid last name>” should be shown. Similarly, if the first name is invalid, then the variable firstNameField should be set to the value null, and when displayed, the message “<Invalid first name>” should be shown.
    In general, the constructor and the set… methods should be very defensive and very suspicious of incoming values (although the subject of preconditions will be discussed later in the course). The get… methods do not have to do any checking so long as the checks when setting the corresponding values are very complete.

    Errors in values that are attempted to be set in the Employee class are to be reported on the standard output file (errors are usually reported to the standard error file, but for the moment, in these exercises, the standard output file will be used both for regular output as well as error output).
    The test program tests values as directed by the problem. (This is a very minimal test program: in real life it would be a good idea to be even more thorough and to make even more tests.) Here'
    s an example of the output from the test program (note that the lines beginning with “Employee:” are generated by the displayValues method of the Employee class, and that the lines beginning with “Error in ...” are produced as error messages from within the Employee class). You do not have to reproduce these messages exactly—the explanation here is simply to make it easier for you to understand what messages come from the test program (i.e., those with "**"), and those that come from the class being tested.

    **
    CSC-240 Assignment 2

    **TESTING CREATING VALID Employee OBJECTS

    **Creating two valid employees
    **Checking the employee Bob Jones
    **Bob Jones is OK
    **The result of "displayValues" for that employee
    Employee
    Bob JonesMonthly Salary2985.00Yearly Salary35820.00
    **Checking the employee Susan Baker
    **Susan Baker is OK
    **The result of "displayValues" for that employee
    Employee
    Susan BakerMonthly Salary3361.75Yearly Salary40341.00

    **INCREASING Employee SALARIES BY 10%
    **
    Checking the employee Bob Jones
    **Bob Jones is OK
    **The result of "displayValues" for that employee
    Employee
    Bob JonesMonthly Salary3283.50Yearly Salary39402.00
    **Checking the employee Susan Baker
    **Susan Baker is OK
    **The result of "displayValues" for that employee
    Employee
    Susan BakerMonthly Salary3697.93Yearly Salary44375.10

    **TESTING INVALID CALLS TO Employee METHODS:
    **
    Setting last name of Bob Jones to ""
    Error in setting Employee objectlast name is a null or empty string
    **Checking the employee Bob <null>
    **
    Bob <nullis OK
    **The result of "displayValues" for that employee
    Employee
    Bob <Invalid last name>; Monthly Salary3283.50Yearly Salary39402.00
    **Setting first name of Bob Jones to null
    Error in setting Employee object
    first name is a null or empty string
    **Checking the employee <null> <null>
    **<
    null> <nullis OK
    **The result of "displayValues" for that employee
    Employee
    : <Invalid first name> <Invalid last name>; Monthly Salary3283.50Yearly Salary39402.00
    **Setting first name of Susan Baker to ""
    Error in setting Employee objectfirst name is a null or empty string
    **Checking the employee <nullBaker
    **<nullBaker is OK
    **The result of "displayValues" for that employee
    Employee
    : <Invalid first nameBakerMonthly Salary3697.93Yearly Salary44375.10
    **Setting last name of Susan Baker to null
    Error in setting Employee object
    last name is a null or empty string
    **Checking the employee <null> <null>
    **<
    null> <nullis OK
    **The result of "displayValues" for that employee
    Employee
    : <Invalid first name> <Invalid last name>; Monthly Salary3697.93Yearly Salary44375.10
    **Setting salary of Susan Baker to -1000.0
    Error in setting Employee object
    Salary is less than zero: -1000.00
    **Checking the employee <null> <null>
    **<
    null> <nullis OK
    **The result of "displayValues" for that employee
    Employee
    : <Invalid first name> <Invalid last name>; Monthly Salary0.00Yearly Salary0.00
    **Creating "" Smithwith salary of -500.0
    Error in setting Employee object
    first name is a null or empty string
    Error in setting Employee object
    Salary is less than zero: -500.00
    **Checking the employee <nullSmith
    **<nullSmith is OK
    **The result of "displayValues" for that employee
    Employee
    : <Invalid first nameSmithMonthly Salary0.00Yearly Salary0.00
    **Creating <nullBaker
    Error in setting Employee object
    first name is a null or empty string
    **Checking the employee <nullBaker
    **<nullBaker is OK
    **The result of "displayValues" for that employee
    Employee
    : <Invalid first nameBakerMonthly Salary500.00Yearly Salary6000.00
    Postscript

    The easiest way in NetBeans to set up the EmployeeTest 
    class in a package called edu.frontrange.csc240.a2 is firstly to unpack the zip fileThis will create a folder called “edu”with other folders within each other (see the package name), and the EmployeeTest.java file in the lastNowin NetBeanscreate a project for the assignment in the usual way (but not checking the box “Create Main Class.The project created will be empty. Nowdrag the folder “edu” onto the “Source Packages” icon in the projectThe result will be the required packagewith the class EmployeeTest in that packageBy right-clicking on the package nameyou can create a new Java class (which you can name Employeeto be the file that you are going to build for this Assignment. If you use the “Files” viewyou will see that NetBeans has created the folder structure to correspond to the package structureYou might review the concept of “packages”by (re-)reading the “FAQ What are Java packages” in the Informational Resources module
  12. #7
  13. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Code:
                      if (firstNameField.length() == 0) 
                         return firstNameField; 
                      
                      if ( firstNameField == null ) 
                         return firstNameField;
    These two if statements don't make sense to me. Can you explain what they are supposed to do?
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    6
    Rep Power
    0
    The objective that I am trying to reach is that if the test program tries to create a "" name (blank) or a null name for a variable type string I want it to print out a error message to the output of the test program. I am sorry if I am writing in such a way that you don't understand.
  16. #9
  17. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    The posted code in #7 doesn't print an error message if the length is 0 or the value is null. It returns the empty String or the null value. Is that what you want?
    writing in such a way that you don't understand.
    If you'd add comments to the code describing what the code is doing, it would be easier to spot errors. Without comments there is no way to know if the code is doing what you want it to do.
    I understand what the code is doing. I don't understand what you want the code to do.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    6
    Rep Power
    0
    The instructions say to set the strings to null (not "null") and set the double*to 0 if its a negative number.* I want to write a method to include in the constructor and also use that method again in the displayValues method to decide what to print.
  20. #11
  21. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    if its a negative number
    If what is a negative number?
    The if statements in post #7 don't set any variables, they cause either an empty String or null value to be returned by the variableTestFirst() method. Is that what the method is supposed to return when there is no data?

    You should decide and document with comments what a method is supposed to do BEFORE writing the code for the method.
    Then as you write the code you can refer to the comments just before the code to see if it is doing what it is supposed to do.

    Look at this example in the code:
    Code:
       // method toretrieve first name        
       public String getFirst()
    Last edited by NormR; August 30th, 2012 at 02:10 PM.

IMN logo majestic logo threadwatch logo seochat tools logo