Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Improve the constructor for Employee. It should discard anyleading or trailing w

ID: 3612874 • Letter: I

Question

Improve the constructor for Employee. It should discard anyleading or trailing whitespace in the strings that are passed to itand

<!--[if!supportLists]-->·        <!--[endif]-->Check the names passed to it: they must consistof just letters and spaces. The Employee name must contain at leastone letter, but the boss can be a string with no letters. We won'tworry about capitalization for now.

<!--[if!supportLists]-->·        <!--[endif]-->Check the dob passed to it: it must be in theformat /mm/dd/yyyy where mm is in 01..12 (Jan to Dec),dd must be a day consistent with mm (allow Feb 29) and yyyy must bein 1900 .. now

If a name is bad throw a BadEmployeeException which tells whichname was bad and what the given "name" was. If the names are ok butthe dob is bad, throw a BadEmployeeException whose message says itwas a bad dob and tells what the offending dob was.

(Otherwise, it constructs the Employee. For now, don't try to"fix" the names or dob.

Test your improved Employee class by putting test cases intoTester, and running it to verify that the desired behavior occurs(including the messages on the Exceptions). Just add more and moretestcases to Tester -- each time it runs let it run all the tests.This way if your debugging screws up an old test case you will beable to see that you still have a problem.

==========================================================================

Improve Employee so that printing an Employee gives goodinformation. Specifically override thetoString method that Employee already inherits from Object so theSystem.out.println(myEmp) will

package employees;

public class Employee{

    private Stringdob; //   mm/dd/yyyy

    private Stringname;

    private Stringboss; // null if no boss? or empty?

   //------------------------------------------------------

    public Employee(Stringdob, String name, String boss){

       this.dob = dob; this.name = name; this.boss = boss;

    }

    int   getAge() {

       System.out.println("getAge is just a stub"); return -99;

    }

    StringgetName(){

       return name;

    }

    StringgetBoss(){

       return boss;

    }

    StringgetDob(){

       return dob;

    }

}

Explanation / Answer


public Employee(String dob, String name,String boss)
{
   //discard any leading ortrailing whitespace in the strings that arepassed
   this.dob=dob.trim();
   this.boss=boss.trim();

// returnstrue if all characters are names or spaces
public boolean checkName(String name)
{
   for(char c:name.toCharArray())
   {
      if(!Character.isLetter(c)&& c!=' ')
      {
         returnfalse;
      }
   }
   return true;
}

improving toString():
public String toString()
{
   return getName()+", dob="+getDob()+","+(boss!=null?boss:"no boss");
}