Question: INSTRUCTIONS FOR WORK: SPECIFICATIONS Your task is to implement the Name class. This class represents a person's name. It has three instance variables representing the
INSTRUCTIONS FOR WORK:
SPECIFICATIONS
Your task is to implement the Name class. This class represents a person's name. It has three instance variables representing the first, last and middle name (all are string variables). A character instance variable representing a separator (to be used for printing purposes) is also part of the class. The only valid separators we can have are a comma (,), a dash (-), and a pound symbol (#). In addition, a nickname instance variable (string) keeps track of the person's nickname (if any).
For this class you need to define a private method called validSeparator that takes a character as a parameter. The method will return true if the character is one of the three valid separators and false otherwise. Make sure you use this method when you need to validate a separator. In addition, the class will keep track of the number of instances created by using a private static field called nameObjsCount. This field is initialized to zero when you declared it.
The methods associated with the class are provided below. All the methods are non-static unless specified otherwise. You may want to take a look at the files Driver.java, PublicTests.java and pubTest1.txt as they can help you clarify the signature associated with each method and the expected output format.
1) Default Constructor - Initializes a Name object with "NOFIRSTNAME", "NOMIDDLENAME", "NOLASTNAME" as first name, middle name and last name, respectively. In addition, the separator will be the '#' character.
2) Constructor with two parameters - The first parameter is the first name and the second the last name. The middle name will be the empty string and the separator will be a comma.
3) Constructors with three parameters - The parameters are the first name, the middle name, and the last name. The separator will be a comma.
4) Constructors with four parameters - The parameters are the first name, the middle name, the last name and the separator. If an invalid separator is provided, a comma will be used instead.
5) getFirstname - Get method for first name.
6) getMiddlename - Get method for middle name.
7) getLastname - Get method for last name.
8) setNickname - Set method for nickname.
9) getNickname - Get method for the nickname. If there is no nickname, the method returns the empty string. Notice the empty string is not the same as null.
10) setSeparator - Set method for the separator. If the separator parameter is invalid, the separator for the Name object will not be changed.
11) getSeparator - Get method for the separator.
12) equals - Two names are considered equal if they have the same first, middle and last names. The nickname is ignored.
13) toString - Returns a string with the last name, the first name, the middle name (if any), and the nickname in parentheses (if any). The separator must be used to separate each value. If there is no middle name, no separator should be added after the first name. The driver output below and the public tests provide examples of the output your program must generate.
14) compareTo - It will return a negative value if the current object precedes the parameter in alphabetical order, 0 if the current object is equal to the parameter, and a positive value otherwise. You must implement this method by comparing last names, then first names and middle names. You MAY NOT implement this method by converting the Name object to string (using toString()) and then using the String class compareTo method on the resulting strings.
15) getNumberOfNameObjects - Static method that returns the number of objects that have been created.
16) normalize - Static method that takes a Name object and a boolean as parameters. The method returns a new Name object where the first name, the last name and the middle name (if any) have been capitalized (i.e. all letters, not just the first letter) if the boolean parameter is true. If the boolean parameter is false, all the names will be in lowercase (i.e. all letters, not just the first letter). Use the string methods toUpperCase() and toLowerCase() for the normalization. Comma will be the separator for the normalized Name object.
17) validSeparator - Private method that takes a character as a parameter and returns true if the parameter is a valid character. You must use this method whenever you need to verify the validity of a separator. If you define but do not use the method, you will lose credit (code duplication penalty).
OTHER
The initialization of instance variables should take place in the constructors and not at declaration time.
If you get the error message "missing_component" from the submit server, make sure that:
There is only one Name file in your project.
-The spelling of all your methods is correct.
-The normalize method has as parameters a Name object and a boolean.
You will fail release test #1 if your toString() method is not generating the correct string.
The prototype for the equals method is:
public boolean equals(Object obj)
where the parameter's type is Object and not Name. The prototype for the compareTo method is:
public int compareTo(Name n)
where the parameter's type is Name.
-It is normal to see errors in the code distribution we have provided. The code relies on the Name class that you need to implement.
HOW TO START
-Write the constructor that has several parameters.
-Write a toString method that allows you to see the content of the object.
-Continue with the get methods of the class.
Testing
Test your code as you develop it, by writing tests in the StudentTests.java file. Using a JUnit test is equivalent to writing a main method that calls the methods you are implementing. The JUnit test will help you as you don't have to write the main method (just the code that appears inside). For this work, you don't have to worry about using assertions (if you don't know what they are don't worry) with your JUnit tests. Each test will use System.out.println to display results in the Eclipse console.
You should have at least one test for each of the methods that you need to implement. You don't need to explicitly test the validSeparator method as it will be tested indirectly via the setSeparator method.
You should write your tests as you you write your methods (that is the correct approach).
Sample Driver
The following driver relies on the class you need to implement. You will find this driver in the code distribution.
Driver public class Driver { public static void main(String[] args) { Name name1 = new Name("Claudia", "I.", "Smith"); System.out.println(name1); Name name2 = new Name("Rachel", "I.", "Green", '#'); System.out.println(name2); Name name3 = new Name("Joseph", "K", "Falk"); name3.setNickname("Joe"); System.out.println(name3); Name name4 = new Name(); System.out.println(name4); System.out.println("Same: " + name1.equals(name2)); System.out.println("Comparing: " + (name1.compareTo(name2) < 0)); Name name5 = Name.normalize(name3, true); System.out.println("Normalized: " + name5); System.out.println("Total number of name objects: " + Name.getNumberOfNameObjects()); } } Output Smith,Claudia,I. Green#Rachel#I. Falk,Joseph,K(Joe) NOLASTNAME#NOFIRSTNAME#NOMIDDLENAME Same: false Comparing: false Normalized: FALK,JOSEPH,K Total number of name objects: 5 Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
