Question: Java program to implement comparable and use it to sort list of names with the requirements shown below. 2. Let's implement Comparable and use it

Java program to implement comparable and use it to sort list of names with the requirements shown below. Java program to implement comparable and use it to sort list of

2. Let's implement Comparable and use it to sort a list of Names 1. Create a package called ca.bcit.comp1510.lab10 2. Create a new class in this package called Name. 3. This Name contains a String for first, middle, and last. 4. This Name is immutable. There are no mutators, only accessors, and the instance variables are final. Validate the parameters in the constructor. Nulls are accepted for middle names, but not first names or last names. No names may be empty Strings or Strings containing nothing but whitespace. Throw an IllegalArgumentException (see slide 30 in Chapter 7) if any parameter is invalid. S. 6. Name implements Comparable. Add the words implements ComparableName> to the Name class header. The compiler will remind you that when you implement an Interface, you must implements its methods. Go ahead and do it! Scroll down and you will see a new empty compareTo method. Note its parameter is another Name object. Remember the compare To contract: Return a negative 7. 2 of 6 COMP 1510 Bruce Link, Keith Tang, Patrick Guichon, Jeffrey Yim, Chris Thompson integer, zero, or a positive integer if this object is less than, equal to, or greater than the object specified as the parameter. We want to compare names or sort names alphabetically, so we can use the Strings's compare To method. A good algorithm a. Compare the two last names. If they're not the same, return the value variable, passing the Name parameter's last name as the parameter. they're not the same, return the value returned when you invoke the could be something like this: returned when you invoke the compare To method on the last name instance If the two last names are the same, compare the two middle names. If compareTo method on the middle name instance variable, passing the Name b. parameter's middle name as the parameter c. If the two middle names are the same, the we must compare the two first names. Use the same method described above to compare the Strings. 8. Create a Driver class with a main method. 9. In the main method, create some names, and add them to an ArrayList 10. Here's the neat part. Make sure you have some Kleenex because you will surely spill your coffee when you see how amazing this next bit is 11. The ArrayList class has a sort method. Usually we pass an instance of a class that implements the Comparator interface. We don't have to do this though if we are storing stuff in the ArrayList that implements Comparable. 12. Invoke the sort method on your full ArrayList, and then print out the results 13. You may now wipe up your spilled coffee

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!