Instance Method Development Demo Write a class Person with a constructor that accepts a name and an age as its argument. These values should be stored in the private attributes name and age. Then, write necessary accessors and mutators to support the given class Registra as shown in the below. The desired execution is also presented. Explain when two person records can be identified as the same and when they have the same information (but are different records). Without this explanation, you will lose half of the score of this question. (Hint: for the development to support lines 16, 21, 26, 31, and 34, please refer to slide 21 in the ppt file under the link difference from the class website. It is also inside the zip file of project materials.) 1 public class Registra{ 2 public static void main(string args[]){ 3 Person a = new Person("Smith", 19); 4 Person [] csc240 = {new Person("Kathy", 18), 5 new Person("Martin", 21), new Person("Smith", 19), a}; 6 System.out.println("The person newly registed this class: "+a); 7 System.out.println("--------join in the class-----------"); 8 for(int i = 0; i<csc240.length; i++){ 9 System.out.println("The person already in class: "+csc240[i]); 10 11 if(a==csc240[i]) 12 System.out.println("They are the same person object."); 13 else 14 System.out.println("They are different people."); 15 16 if(a.has_same_name(csc240[i])) 17 System.out.println("They have the same name."); 18 else 19 System.out.println("They have different names."); 20 21 if(a.has_same_age(csc240[i])) 22 System.out.println("They have the same age."); 23 else 24 System.out.println("They have different ages."); 25 26 if(a.has_same_name(csc240[i]) && a.has_same_age(csc240[i])) 27 System.out.println("Personal information is the same (equal)."); 28 else 29 System.out.println("They are different people."); 30 31 if(a.is_younger_than(csc240[i])) 32 System.out.println("The new person is younger than student "+ i 33 + "in class (" + csc240[i]); 34 else if (a.is_older_than(csc240[i])) 35 System.out.println("The new person is older than student "+ i 36 + "in class (" + csc240[i]); 37 else 38 System.out.println("The new person has the same age as student "+ i 39 + "in class (" + csc240[i]); 40 System.out.println("----- end of the check of one person -----\n"); 41 } 42 } 43 }
Solution (including each step): Step 1: Ensure the appropriate compiler and development environment. Can you run the program and get the result like this? Step 2: Add the first a few lines in application class Registra and see if this additional part can be supported. For instance, if we have the class Person like the follows,
the application lacks the support and will give the errors: Q&A1: What to do to support new operation? constructor. So we have the following program implemented in the support class Person: newly added! The result is:
1) No syntax error and 2) execution is good and does not throw any exception. Q&A2: What is the result, and can this be displayed? Step 3: Add two more lines and see if they can be supported: tostring is needed in class Person to avoid displaying a strange memory address. Check if this is enough as the desired support:
Object content, from its attributes Step 4: Continue to add more lines in application class Registra: No need for extra support.
Q&A3: How can I see two identical Smith in the check? -- One is initiated with the same content and the other is the exact same object (i.e., object a ). Step 5: How about adding more lines in the application class? Q&A4: What support is needed from the class of Person? 1) method has_same_name, and 2) the relevant accessors as the support of such a supporting method. Newly added! Cannot always be true! So, Q&A5: How to get the name of host object, and how to get the name of the right object? accessor, i.e., getname. Refer to the ppt document under the link ``difference from the class website.
Q&A6: How to compare these two names? String equals. Newly added! The execution result can be confirmed as: Q&A7: Why the last two Smith objects have different results: one says that they are the same and the other says that they are different? See the object new operation, as we explained in Q&A3. Step 6: Now the entire application class does not have technical obstacle.
Newly added. Step 7: Test the execution of this completed version and compare with the desired one in the assignment sheet.