CMPU 102 Lab 3: Creating and using a simple instantiable class February 13th, 2017 Part I: Creating a class to represent a person 1. Log in to your CS account and change directory to cs102. 2. Change directory into your labs subdirectory. 3. Create a new directory in your labs directory for lab 3 by entering: mkdir lab3 4. Write a class to represent the physical entity we call a person. Call the class Person and the file Person.java a) Define the following instance members in class Person: lastName (String) age (int) gender (char) idNumber (int) b) Define the following class member: static int counter = 0; c) Define a 3-parameter constructor for your Person class that will initialize the lastName, age, and gender instance members by setting them equal to three input parameters. It is only necessary to include a single constructor in this class. Inside the constructor, increment the static counter variable and set the idNumber instance variable to the value of the counter after incrementing the counter. This will assign each Person object you created a different numeric value starting with 1 and increasing as each object is created. d) Create public getter methods for data fields 1 through 4. Call these methods getLastName(), getAge(), getGender(), and getIDNumber(). They should each return the value of the field type they refer to. e) Create a public method called equals that takes a Person object as input and returns true if all the fields (except the idNumber) in the parameter object are equal to the ones in the object the method is called on. Note that you can use == to compare primitive types, but the String lastName must be checked using the equals() method for Strings. If, for example, you had a Person object called p1 that you called as follows: p1.equals(p2) In the equals method you would check if the lastName field is equal to that of Person object p2, using the following syntax: (this.lastName.equals(p2.lastName)) This equals method is necessary to compare 2 Strings because they are objects. Objects are always compared using the equals method. You would also need to check if the age and gender fields of this person and p2 are the same. f) Lastly, create a public method called "toString" that takes no input and returns a concatenated String representation of all the data members in the Person object . The String returned by calling p1.toString() should look something like this (use the tab escape sequence (\t) for tabs between data fields): Person 1: Jones 14 M // note: 4 variable names are used in this output, // concatenated with "Person " and ": " in the indicated // positions. NOTE: COMPILE AND SAVE OFTEN. YOU WILL NOT BE ABLE TO RUN THE PERSON.JAVA FILE UNTIL AFTER YOU FINISH PART II. ////////////////////////////////////////////////////////////////////// Part II: Testing the class from part I Write a procedural application TestPerson in a file called TestPerson.java This tester program should only have a main method in which you should: 1. Prompt for and then read user input from the keyboard to create two Person objects (instantiation in step 2, below), e.g., java.util.Scanner rd = new java.util.Scanner(System.in); System.out.println("Please enter the last name of person 1:"); String name1 = rd.next(); System.out.println("Please enter the age of person 1:"); int age1 = rd.nextInt(); System.out.println("Please enter the gender of person 1 (F/M/T:"); char gender1 = (rd.next().charAt(0)); // Scanner has no method to read // type char, so read a String and // get the character at position 0 You will need another set of the statements like the ones given above to create the second person. 2. Use the keyword "new" and call the 3-parameter constructor written in the Person class to instantiate each Person instance. 3. call the toString method of each object within a System.out.println statement to print all data fields for each object, and 4. print the total number of objects created by accessing the static counter in the Person class, Person.counter. 5. Test the objects for equality with the following code: if (p1.equals(p2)) { System.out.println("Same person"); } else { System.out.println("Not same person"); } To make sure your equals method works properly, create 2 objects with identical fields and test the equals method on them and create 2 objects with different fields and test the equals method on them. You should get different results. //////////////////////////////////////////////////////////////////////////// The output from the tester program should look like this when the Person objects are found to be not equal: Person 1: Jones 14 M // these 2 lines are from Person 2: Schultz 36 F // calls to toString() Not same person Total people = 2 // This is a separate print statement. The output from the tester program should look like this when the Person objects are found to be equal: Person 1: Flintstone 30 M // these 2 lines are from Person 2: Flintstone 30 M // calls to toString() Same person Total people = 1 // This is a separate print statement. You // will need to decrement the counter object // to get it to be 1 Show your files to your professor or a coach and get checked off when you are finished and have tested your programs. Don't forget to log out.