{"id":126,"date":"2021-01-01T20:55:42","date_gmt":"2021-01-01T20:55:42","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/practical-java-programming\/?page_id=126"},"modified":"2021-01-01T21:26:47","modified_gmt":"2021-01-01T21:26:47","slug":"assignment-2","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/assignments\/assignment-2\/","title":{"rendered":"Assignment 2"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a6d453c36b50\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a6d453c36b50\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/assignments\/assignment-2\/#NOTE\" >NOTE:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/assignments\/assignment-2\/#Part_A_A_Timer_Program\" >Part A: A Timer Program<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/assignments\/assignment-2\/#Part_B_Practice_with_Strings\" >Part B: Practice with Strings<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/assignments\/assignment-2\/#Notes_on_the_Above\" >Notes on the Above:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/assignments\/assignment-2\/#sort\" >sort<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/assignments\/assignment-2\/#compareTo\" >compareTo<\/a><\/li><\/ul><\/nav><\/div>\n\n<div class=\"wp-block-group has-cyan-bluish-gray-background-color has-background\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h3 class=\"has-text-align-center wp-block-heading\"><span class=\"ez-toc-section\" id=\"NOTE\"><\/span>NOTE:<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Before you start doing any real coding, read the&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/www.oracle.com\/java\/technologies\/javase\/codeconventions-contents.html\" target=\"_blank\" data-type=\"URL\" data-id=\"https:\/\/www.oracle.com\/java\/technologies\/javase\/codeconventions-contents.html\">Java Coding Conventions<\/a>, especially the section on&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/www.oracle.com\/java\/technologies\/javase\/codeconventions-namingconventions.html\" target=\"_blank\">Naming Conventions<\/a>. You don&#8217;t have to inwardly digest every single aspect of these conventions, but try to get a good overview.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is very important to start coding using the proper naming conventions &#8212; they are not optional when it comes to using Java Beans, etc., so let&#8217;s start using good habits early!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I will expect you to follow these conventions in all the code you submit to me.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There is one&nbsp;<em><u>exception<\/u>:<\/em>&nbsp;The use of braces in section&nbsp;<strong>7.2 Compound Statements<\/strong>. Their bracing conventions use the following form:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>while (!done) {\n    statement;\n    statement;\n}<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">I prefer the much more readable form:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>while (!done) \n{\n    statement;\n    statement;\n}<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">While you may choose to use the JavaSoft (aka K&amp;R) convention in your personal code, I request you to use my preferred form in the code you submit for these assignments; I find the K&amp;R convention much harder to read, and that adds unnecessary effort to grading assignments.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><u>NOTE:<\/u>&nbsp;<\/strong>Regardless of brace alignment conventions, I require you to indent statements consistently. Code that does something like:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>while (!done) \n    {\n    statement;\n  statement;\n   statement;\n}<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Is very hard to read, and is often associated with sloppy work. If I encounter this kind of inconsistent formatting, the grade will suffer!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><em><u>Hint:<\/u><\/em>&nbsp;<\/strong>To ensure that your printer doesn&#8217;t turn your nicely aligned statements as seen on the screen into something out of a horror show, you will probably need to set your editor (or IDE editor) to generate spaces in place of tab characters.&nbsp; Most editors allow you to do that, and furthermore, set where your tab stops will be (I recommend every 2, 3, or 4 spaces, no more, or you&#8217;ll tend to use too much horizontal space in your documents).<\/p>\n<\/div><\/div>\n\n\n\n<div style=\"height:27px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Part_A_A_Timer_Program\"><\/span>Part A: A Timer Program<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The following program:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage timer;\n\npublic class Timer\n{\n    public void main(String&#x5B;] args)\n    {\n        Timer m = new Timer();\n    }\n\n    public Timer()\n    {\n        m_start = new Time(0);\n        m_end   = m_start;\n        m_end.addSeconds(45);\n        System.out.println(&quot;Started at &quot; + m_start);\n        System.out.println(&quot;Ended at   &quot; + m_end);\n        System.out.println(&quot;Duration = &quot;\n                           + m_end.secondsBetween(m_start)\n                           + &quot; secs&quot;);\n    }\n\n    \/\/\/\/\/\/\/\/\/\/\/\/\/\/ Data \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n    private Time m_start, m_end;\n}\n\nclass Time\n{\n    public Time(long sec)\n    {\n        m_sec = sec;\n    }\n\n    public void addSeconds(long sec)\n    {\n        m_sec += sec;\n    }\n\n    public long secondsBetween(Time t)\n    {\n        return m_sec - t.m_sec;\n    }\n\n    public String toString()\n    {\n        return &quot;Time: &quot; + m_sec + &quot; secs&quot;;\n    }\n\n    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ Data \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n    private long m_sec;\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">is intended to be a rudimentary&nbsp;application&nbsp;(not an applet) which times the duration between two events.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It compiles, but unfortunately refuses to run.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><em><strong><u>Why?<\/u><\/strong><\/em><\/li><\/ul>\n\n\n\n<ol class=\"wp-block-list\"><li>Fix the program so that it will compile and run as an application.<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><em><u>Hint<\/u><\/em>:&nbsp;<\/strong>There is a single problem only. It is a simple case of omission &#8212; one that is quite a common mistake!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When you finally get the program to run, you will find that it produces the following output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>Started at Time: 45 secs Ended at Time: 45 secs Duration = 0 secs<\/strong><\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li><em><strong><u>Why?<\/u><\/strong><\/em><\/li><\/ul>\n\n\n\n<ol class=\"wp-block-list\" type=\"1\" start=\"2\"><li>Fix the program so that it prints out the correct duration.<\/li><li>Now comment out the entire&nbsp;<strong>toString()<\/strong>&nbsp;method in the&nbsp;<strong>Time<\/strong>&nbsp;class, and recompile and run the program.<\/li><\/ol>\n\n\n\n<ul class=\"wp-block-list\"><li><em><strong><u>What happens? How does this change the behavior of the program?<\/u><\/strong><\/em><\/li><li><em><strong><u>Why?<\/u><\/strong><\/em><\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><em><u>Hint 1<\/u><\/em>:&nbsp;<\/strong>Change the lines in the program that read:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>System.out.println(\"Started at \" + m_start);\nSystem.out.println(\"Ended at   \" + m_end);<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">to read:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>System.out.println(\"Started at \" + m_start.toString());\nSystem.out.println(\"Ended at \" + m_end.toString());<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><u>What effect does this have? Why?<\/u><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><em><u>Hint 2<\/u><\/em>:&nbsp;<\/strong>Use the IDE help or JDK documentation to find the&nbsp;<strong><code>toString()<\/code><\/strong>&nbsp;method. You&#8217;ll see that there are many of them, in different classes. Take special note of the&nbsp;<strong><code>toString()\/code&gt;<\/code><\/strong><code>&lt;&nbsp;method in the&nbsp;<strong><code>java.lang.Object<\/code><\/strong>&nbsp;class.<\/code><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><em><strong><u>What does this tell you you should probably do, as a regular practice?<\/u><\/strong><\/em><\/li><\/ul>\n\n\n\n<div style=\"height:61px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Part_B_Practice_with_Strings\"><\/span>Part B: Practice with Strings<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In this part of the assignment, you will write a program that accepts a set of strings of people&#8217;s names, of the form:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>first-name last-name<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For example, here&#8217;s a set to work with:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\"fred bloggs\", \"MARY STUART\",\n\"eRIC SuttoN\", \"Francis Bailey\",\n\"Archimedes von trapp\", \"Abel GRISWOLD\",\n\"Jeffrey Guay\", \"Alan Swasinski\",\n\"Leroy de la Grange\", \"Don Ho\"<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">(I&#8217;ve intentionally mixed up the case of several of these strings to ensure that you write the code to set the case properly.)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Assume that:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>the first and last names are separated by a single space character<\/li><li>the first name&nbsp;<em>does not&nbsp;<\/em>contain any spaces<\/li><li>the last name&nbsp;<em>may or may not&nbsp;<\/em>contain one or more spaces (for example, Bailey;&nbsp; von trapp, de la Grange)<\/li><\/ul>\n\n\n\n<ol class=\"wp-block-list\"><li><em><strong><u>Make sure that each string is properly formatted<\/u><\/strong><\/em><strong>:<\/strong><\/li><\/ol>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Ensure that each string satisfies the following:<\/p><\/blockquote>\n\n\n\n<ul class=\"wp-block-list\"><li>the first name has its first character in upper case, and its remaining characters in lower case.<\/li><\/ul>\n\n\n\n<ul class=\"wp-block-list\"><li>the last name has its first character&nbsp;<em>in upper case<\/em>, and its remaining characters<em>&nbsp;in lower case<\/em>.<ul><li>However, if the last name contains any spaces,&nbsp;<em>all but its last part should be in lower case<\/em>, and&nbsp;<em>the first character of the last part should be upcased&nbsp;<\/em>(for example, &#8220;Archimedes von trapp&#8221; should result in &#8220;Archimedes von Trapp&#8221; and&nbsp;<em><strong>not<\/strong><\/em>&nbsp;&#8220;Archimedes Von trapp&#8221;)<\/li><\/ul><\/li><\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><em><u><strong>Explanation:<\/strong><\/u><\/em>&nbsp;In some languages, last (family) names may consist of multiple words. For example, in French, the name &#8220;de la Grange&#8221; is a family name, and in German, the name &#8220;von Trapp&#8221; is a family name. Typically, the last word is a proper name (often a place name) and so should have its first letter upcased, while the words before it are not proper names (they are often prepositions and\/or definite articles) and should not be upcased. The words &#8220;de la&#8221; in French mean &#8220;of the&#8221; or &#8220;from the&#8221;, and the word &#8220;von&#8221; in German means &#8220;of&#8221; or &#8220;from&#8221;.<\/p><\/blockquote>\n\n\n\n<ol class=\"wp-block-list\" type=\"1\" start=\"2\"><li><em><strong><u>Sort the names in ascending first name order.<\/u><\/strong><\/em>&nbsp;&nbsp;The order should be case insensitive &#8212; that is, the sort should not differentiate between upper and lower case characters. Print out the resulting array of strings.<\/li><li><em><strong><u>Sort the names in ascending last name order.<\/u><\/strong><\/em>&nbsp;&nbsp;The order should be&nbsp;case insensitive. Print out the resulting array of strings.<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Here are some skeleton Java sources which I expect you to start from. Note that I&#8217;ve already done a lot of the work for you, since the main point of this exercise is for you to learn how to use the String class.&nbsp;&nbsp; The structure of the program is already present;&nbsp; all you have to do is fill in the gaps with your code.&nbsp; Look for&nbsp;<strong>[fill in here]<\/strong>&nbsp;indicators, for where you have to supply code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can cut and paste from the following Java sources:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage names;\n\n\/**\n*   Class to represent a person&#039;s name.\n*\/\npublic class Name implements Comparable\n{\n    \/**\n    *   Constructor\n    *\/\n    public Name(String first, String last)\n    {\n        m_first = first;\n        m_last = last;\n    }\n\n    \/**\n    *   Returns whether the names are to be sorted\n    *   by last name (true) or first name (false)\n    *\/\n    public boolean isSortByLast()\n    {\n        return m_sortByLast;\n    }\n\n    \/**\n    *   Sets whether the names are to be sorted\n    *   by last name (true) or first name (false)\n    *\/\n    public void setSortByLast(boolean b)\n    {\n        m_sortByLast = b;\n    }\n\n    \/**\n    *   Compare this object with another object.\n    *   (The other object must also be an instance of Name)\n    *   NOTE: This comparison is case insensitive;  that is,\n    *         letters are sorted regardless of case.\n    *\/\n    public int compareTo(Object o)\n    {\n        Name that = (Name) o;\n        String comp1, comp2;\n        int result = 0;\n        if (m_sortByLast)\n        {\n            &#x5B;fill in here]\n        }\n        else\n        {\n            &#x5B;fill in here]\n        }\n        return result;\n    }\n\n    \/**\n    *   Returns a string of the form:\n    *       &lt;first-name&gt; &lt;last-name&gt;\n    *   where the two parts of the name are separated by a space.\n    *\/\n    public String toString()\n    {\n        return m_first + &quot; &quot; + m_last;\n    }\n\n    \/\/\/\/\/ Private data \/\/\/\/\/\/\n    private boolean m_sortByLast = true;\n    private String m_first;\n    private String m_last;\n}\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage names;\n\nimport java.util.Arrays;\n\n\/**\n*   Class to test the sorting of Names.\n*\/\npublic class NamesTest\n{\n    \/**\n    *   Main entry point.\n    *\/\n    public static void main(String&#x5B;] args)\n    {\n        Name&#x5B;] names = new Name&#x5B;m_names.length];\n        \/\/ populate the array with normalized names\n        for (int i = 0; i &lt; m_names.length; i++)\n        {\n            String name = m_names&#x5B;i];\n            String first = getFirstName(name);\n            String last  = getLastName(name);\n            names&#x5B;i] = new Name(first, last);\n        }\n        \/\/ Sort it by last name\n        Arrays.sort(names);\n        System.out.println(&quot;   Names sorted by last name:&quot;);\n        for (int i = 0; i &lt; names.length; i++)\n        {\n            System.out.println(names&#x5B;i]);\n        }\n        \/\/ Sort it by first name\n        for (int i = 0; i &lt; names.length; i++)\n            names&#x5B;i].setSortByLast(false);\n        Arrays.sort(names);\n        System.out.println(&quot;   Names sorted by first name:&quot;);\n        for (int i = 0; i &lt; names.length; i++)\n        {\n            System.out.println(names&#x5B;i]);\n        }\n    }\n\n    \/**\n    *   Extracts the first name from the specified name,\n    *   normalizes it, and then returns it.\n    *\/\n    private static String getFirstName(String name)\n    {\n        \/\/ Get rid of any leading or trailing space\n            &#x5B;fill in here]\n        \/\/ Find the first space\n            &#x5B;fill in here]\n        \/\/ Extract everything before it as the first name\n            &#x5B;fill in here]\n        \/\/ Initial cap the entire first name\n            &#x5B;fill in here]\n        \/\/ Return the first name\n    }\n\n    \/**\n    *   Extracts the last name from the specified name,\n    *   normalizes it, and then returns it.\n    *\/\n    private static String getLastName(String name)\n    {\n        \/\/ Get rid of any leading or trailing space\n            &#x5B;fill in here]\n        \/\/ Find the first space\n            &#x5B;fill in here]\n        \/\/ Extract everything beyond it as the last name\n            &#x5B;fill in here]\n        \/\/ Find out whether it has an embedded space, \n        \/\/ &amp; if it does, find the last one in the name\n            &#x5B;fill in here]\n        if (&#x5B;fill in here]) \/\/ No space found\n        {\n            \/\/ No space found, so just Init cap the entire last name\n            &#x5B;fill in here]\n        }\n        else\n        {\n            \/\/ Space found, so extract the first part\n            &#x5B;fill in here]\n            \/\/ and lowercase it\n            &#x5B;fill in here]\n            \/\/ Now extract the last part\n            &#x5B;fill in here]\n            \/\/ and init cap it\n            &#x5B;fill in here]\n            \/\/ Then reconstruct the name again, \n            \/\/ from the first and last parts\n            &#x5B;fill in here]\n        }\n        \/\/ Return the resulting normalized last name\n            &#x5B;fill in here]\n    }\n\n    \/**\n    *   Utility method to take a string (name) and set the\n    *   first character to uppercase, and all the other\n    *   characters to lowercase.\n    *\/\n    private static String initialCap(String name)\n    {\n            &#x5B;fill in here]\n    }\n\n    \/\/\/\/ Private data \/\/\/\/\/\n\n    \/\/ The initial list of un-normalized names.\n    private static String&#x5B;] m_names =\n    {\n        &quot;fred bloggs&quot;, &quot;MARY STUART&quot;,\n        &quot;eRIC SuttoN&quot;, &quot;Francis Bailey&quot;,\n        &quot;Archimedes von trapp&quot;, &quot;Abel GRISWOLD&quot;,\n        &quot;Jeffrey Guay&quot;, &quot;Alan Swasinski&quot;,\n        &quot;Leroy de la Grange&quot;, &quot;Don Ho&quot;\n    };\n}\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Notes_on_the_Above\"><\/span>Notes on the Above:<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The key to understanding this is to look up the&nbsp;<code><strong>java.lang.Comparable<\/strong><\/code>&nbsp;interface in the javadocs (It&#8217;s in JDK 1.2 on up).&nbsp;&nbsp; Class Name implements the&nbsp;<code><strong>Comparable<\/strong><\/code>&nbsp;interface, which means that it&#8217;s required to implement the method:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>public int compareTo(Object o)<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This method is used by the&nbsp;<code><strong>Arrays.sort(names)<\/strong><\/code>&nbsp;method (also in JDK 1.2 on up).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>(Note:&nbsp; for any of you who aren&#8217;t using JDK 1.2 or higher &#8212; please upgrade!)<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s what the JDK 1.2 docs say about the <code><strong>lang.util.Arrays<\/strong><\/code> class&#8217;s <code>sort()<\/code>method (I&#8217;ve underlined the critical point):<\/p>\n\n\n\n<hr class=\"wp-block-separator is-style-wide\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"sort\"><\/span>sort<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">public static void sort(Object[] a)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Sorts the specified array of objects into ascending order, according to the natural ordering of its elements.&nbsp;All elements in the array must implement the Comparable interface.&nbsp;Furthermore, all elements in the array must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the array).<br>This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The sorting algorithm is a modified mergesort (in which the merge is omitted if the highest element in the low sublist is less than the lowest element in the high sublist). This algorithm offers guaranteed n*log(n) performance, and can approach linear performance on nearly sorted lists.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Parameters:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>a &#8211; the array to be sorted.<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">Throws:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>ClassCastException &#8211; if the array contains elements that are not mutually comparable (for example, strings and integers).<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">See Also:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Comparable<\/p><\/blockquote>\n\n\n\n<hr class=\"wp-block-separator is-style-wide\"\/>\n\n\n\n<p class=\"wp-block-paragraph\"><br>You&#8217;ll note that the above refers to the Comparable interface.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s what the JDK 1.2 docs say about the Comparable interface&#8217;s&nbsp;<code>compareTo<\/code>&nbsp;method:<\/p>\n\n\n\n<hr class=\"wp-block-separator is-style-wide\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"compareTo\"><\/span>compareTo<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">public int compareTo(Object o)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.<br>The implementor must ensure&nbsp;<code>sgn(x.compareTo(y)) == -sgn(y.compareTo(x))<\/code>&nbsp;for all x and y. (This implies that&nbsp;<code>x.compareTo(y)<\/code>&nbsp;must throw an exception iff&nbsp;<code>y.compareTo(x)<\/code>&nbsp;throws an exception.)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The implementor must also ensure that the relation is transitive:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(x.compareTo(y)&gt;0 &amp;&amp; y.compareTo(z)&gt;0) implies x.compareTo(z)&gt;0.<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Finally, the implementer must ensure that&nbsp;<code>x.compareTo(y)==0<\/code>&nbsp;implies that&nbsp;<code>sgn(x.compareTo(z)) == sgn(y.compareTo(z))<\/code>, for all z.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is strongly recommended, but not strictly required that&nbsp;<code>(x.compareTo(y)==0) == (x.equals(y))<\/code>. Generally speaking, any class that implements the Comparable interface and violates this condition should clearly indicate this fact. The recommended language is &#8220;Note: this class has a natural ordering that is inconsistent with equals.&#8221;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Parameters:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>o &#8211; the Object to be compared.<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">Returns:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">Throws:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>ClassCastException &#8211; if the specified object&#8217;s type prevents it from being compared to this Object.<\/p><\/blockquote>\n\n\n\n<hr class=\"wp-block-separator is-style-wide\"\/>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>NOTE: Before you start doing any real coding, read the&nbsp;Java Coding Conventions, especially the section on&nbsp;Naming Conventions. You don&#8217;t have to inwardly digest every single aspect of these conventions, but try to get a good overview. It is very important to start coding using the proper naming conventions &#8212; they are not optional when it [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":102,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_eb_attr":"","_uag_custom_page_level_css":"","ocean_front_end_style_editor":"no","ocean_post_layout":"left-sidebar","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"ocs-assignments-sidebar","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","footnotes":""},"class_list":["post-126","page","type-page","status-publish","hentry","entry"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"ocean-thumb-m":false,"ocean-thumb-ml":false,"ocean-thumb-l":false},"uagb_author_info":{"display_name":"Bryan Higgs","author_link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/author\/bryan\/"},"uagb_comment_info":0,"uagb_excerpt":"NOTE: Before you start doing any real coding, read the&nbsp;Java Coding Conventions, especially the section on&nbsp;Naming Conventions. You don&#8217;t have to inwardly digest every single aspect of these conventions, but try to get a good overview. It is very important to start coding using the proper naming conventions &#8212; they are not optional when it&hellip;","_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/126","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/comments?post=126"}],"version-history":[{"count":9,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/126\/revisions"}],"predecessor-version":[{"id":141,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/126\/revisions\/141"}],"up":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/102"}],"wp:attachment":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/media?parent=126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}