Java

OOP GUIDE / WORK

 

NAME CLASS

 

Topics

  • Encapsulation
  • Class diagrams
  • Immutable class

 

 

TASK – PART 1 – NAME CLASS

 

Create a Name class based on the following class diagram:

 

Name

-firstName : String

-middleName : String
-lastName : String
-courtesyTitle : String

+Name(first : String, mid : String, last : String, title : String)

+getFirstName() : String

+getMiddleName() : String

+getLastName() : String

+getFullName() : String

+getInitials() : String

+getTitle() : String

 

Click here for a brief explanation on this class diagram.

 

Notice that once you create a Name object, you can no longer change any of its instance variables.  So this class is said to be immutable.

 

Note: To get the first character of the String s, we use s.substring(0,1).  You will need to use this in the getInitials() method.

 

 

TASK – PART 2 – TESTING THE NAME CLASS

 

Create another class called NameTester and follow the following steps:

 

  • Add a main function to the NameTester class.

  • Inside main, Create a Name object that represents yourself.

  • Test all methods (by outputting their results to screen).