{"id":204,"date":"2021-01-02T17:40:42","date_gmt":"2021-01-02T17:40:42","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/practical-java-programming\/?page_id=204"},"modified":"2021-01-02T18:56:23","modified_gmt":"2021-01-02T18:56:23","slug":"assignment-7","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/assignments\/assignment-7\/","title":{"rendered":"Assignment 7"},"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-6a6d468e24501\" 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-6a6d468e24501\"  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-7\/#A_Graphical_Shuttle\" >A Graphical Shuttle<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/assignments\/assignment-7\/#Part_1_A_Graphical_Shuttle_Program\" >Part 1: A Graphical Shuttle Program<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/assignments\/assignment-7\/#Part_2_A_Shuttle_Component\" >Part 2: A Shuttle Component<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/assignments\/assignment-7\/#Part_3_A_Virtual_Supermarket\" >Part 3: A Virtual Supermarket<\/a><ul class='ez-toc-list-level-5' ><li class='ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/assignments\/assignment-7\/#Heres_an_important_note_about_how_to_go_about_this\" >Here&#8217;s an important note about how to go about this:<\/a><\/li><\/ul><\/li><\/ul><\/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-7\/#Part_1_A_Graphical_Shuttle_Program-2\" >Part 1: A Graphical Shuttle Program<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/assignments\/assignment-7\/#Part_2_A_Shuttle_Component-2\" >Part 2: A Shuttle Component<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/assignments\/assignment-7\/#Part_3_A_Virtual_Supermarket-2\" >Part 3: A Virtual Supermarket<\/a><\/li><\/ul><\/nav><\/div>\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"A_Graphical_Shuttle\"><\/span>A Graphical Shuttle<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A graphical shuttle, also know as a Dual Listbox, or Dual Multiselect, or List Builder, or List Shuttle is a graphical entity that allows the user to move items between two lists. It is often used to allow the user to choose from a number of items in a list (usually on the left) and move them to the other list (usually on the right).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s what it looks like in our assignment:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"412\" height=\"234\" src=\"https:\/\/bhiggs.x10hosting.com\/practical-java-programming\/wp-content\/uploads\/2021\/01\/shuttle1.gif\" alt=\"\" class=\"wp-image-210\"\/><\/figure><\/div>\n\n\n\n<div style=\"height:18px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">This assignment consists of three parts:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Part_1_A_Graphical_Shuttle_Program\"><\/span><a href=\"#part-1\">Part 1: A Graphical Shuttle Program<\/a><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Part_2_A_Shuttle_Component\"><\/span><a href=\"#part-2\">Part 2: A Shuttle Component<\/a><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Part_3_A_Virtual_Supermarket\"><\/span><a href=\"#part-3\">Part 3: A Virtual Supermarket<\/a><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<div style=\"height:26px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Please do the three parts in the order specified. You will find that the assignment builds on the previous section, so you should complete each part before moving on to the next part.<\/p><\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>I want to see your results for each part&nbsp;<strong><em>separately<\/em><\/strong>, so I can see how you built things up.<\/p><\/blockquote>\n\n\n\n<div class=\"wp-block-group has-white-background-color has-background\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h5 class=\"has-text-align-center wp-block-heading\"><span class=\"ez-toc-section\" id=\"Heres_an_important_note_about_how_to_go_about_this\"><\/span>Here&#8217;s an important note about how to go about this:<span class=\"ez-toc-section-end\"><\/span><\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">(Much of the following probably won&#8217;t make much sense until you&#8217;ve read the details in Part 1.)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Take some time to think about what you\u2019re writing.&nbsp; If you find yourself writing code that looks very similar to code that you\u2019ve already written, then what does that suggest you should do? &nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Please do&nbsp;<strong><em><strong><em>not<\/em><\/strong><\/em><\/strong>&nbsp;say \u201cCut and paste!\u201d !!!!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You will probably find a number of opportunities to fold otherwise repetitive code into a small number of common (most likely private) methods \u2013 with suitable method arguments.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, you can cause an entry in one list to move to the other list in two ways:<\/p>\n\n\n\n<ol class=\"wp-block-list\" type=\"1\"><li>You can select the item, and then click on the appropriate button, or:<\/li><li>You can simply double-click on the item.<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Do&nbsp;<strong><em><strong><em>not<\/em><\/strong><\/em><\/strong>&nbsp;write duplicated code for these two approaches!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Another observation:&nbsp; The actions to move an item from the left list to the right list are identical to the actions necessary to move an item from the right list to the left list, except for which list is the source and which is the target.&nbsp; Again, try to avoid writing duplicate code!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By the end of this course, I\u2019d like to believe that you can all do appropriate factoring of functionality to come up with a reasonably optimal, minimally code-duplicated solution.&nbsp; So here\u2019s an opportunity to practice that!<\/p>\n<\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator is-style-wide\"\/>\n\n\n\n<div style=\"height:62px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"part-1\"><span class=\"ez-toc-section\" id=\"Part_1_A_Graphical_Shuttle_Program-2\"><\/span>Part 1: A Graphical Shuttle Program<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Write a Java application that implements a &#8220;Shuttle control&#8221;.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It should look like:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"412\" height=\"234\" src=\"https:\/\/bhiggs.x10hosting.com\/practical-java-programming\/wp-content\/uploads\/2021\/01\/shuttle1.gif\" alt=\"\" class=\"wp-image-210\"\/><\/figure><\/div>\n\n\n\n<div style=\"height:26px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">In other words, two <strong>List<\/strong> boxes &#8212; a left <strong>List<\/strong> and a right <strong>List<\/strong> &#8212; with two <strong>Button<\/strong>s placed between them. (The contents of the <strong>List<\/strong>s are just representative of what can be placed in there.)<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><em><strong><u>Note:<\/u><\/strong><\/em>&nbsp;I want&nbsp;<strong><em>you<\/em><\/strong>&nbsp;to write the code to do this; do&nbsp;<strong><em>not<\/em><\/strong>&nbsp;merely use your Java IDE (JBuilder, NetBeans, Eclipse, etc.) forms generator to do it! (Although you can learn a lot about how to do it by experimenting with your IDE forms generator, if you have one!! However, that is not the point of this assignment.)<\/p><\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong><em><u>Note:<\/u><\/em><\/strong>&nbsp;Be sure to place the class in the package&nbsp;<strong><code>shuttle<\/code><\/strong>!<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">Now implement the program actions as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The upper button (the one labelled &#8220;<code><strong>&gt;&gt;<\/strong><\/code>&#8220;) is enabled if an item is selected in the left list, and disabled otherwise. (Allow only a single selection.)<\/li><li>The lower button (the one labelled &#8220;<code><strong>&lt;&lt;<\/strong><\/code>&#8220;) is enabled if an item is selected in the right list, and disabled otherwise. (Allow only a single selection.)<\/li><\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>For example:<\/p><\/blockquote>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img decoding=\"async\" width=\"412\" height=\"234\" src=\"https:\/\/bhiggs.x10hosting.com\/practical-java-programming\/wp-content\/uploads\/2021\/01\/shuttle2.gif\" alt=\"\" class=\"wp-image-214\"\/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img decoding=\"async\" width=\"412\" height=\"234\" src=\"https:\/\/bhiggs.x10hosting.com\/practical-java-programming\/wp-content\/uploads\/2021\/01\/shuttle3.gif\" alt=\"\" class=\"wp-image-215\"\/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"412\" height=\"234\" src=\"https:\/\/bhiggs.x10hosting.com\/practical-java-programming\/wp-content\/uploads\/2021\/01\/shuttle4.gif\" alt=\"\" class=\"wp-image-216\"\/><\/figure><\/div>\n\n\n\n<div style=\"height:32px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<ul class=\"wp-block-list\"><li>When the upper button (&#8220;<code><strong>&gt;&gt;<\/strong><\/code>&#8220;) is clicked, the selected item in the left list is removed from the left list and inserted into the right list. Any selection in the right list is not changed.<\/li><li>When the lower button (&#8220;<code><strong>&lt;&lt;<\/strong><\/code>&#8220;) is clicked, any selected item in the left list is removed from the right list and inserted into the left list. Any selection in the left list is not changed.<\/li><li>In other words, list items are &#8220;shuttled&#8221; backwards and forwards between the list boxes.<\/li><li>After an item is selected in a list and then moved to the other list, the selection in the source list after the move will be as follows:<ul><li>If there is an item immediately following the original item, then it will be selected.<\/li><li>Otherwise, if there is an item immediately preceding the original item, then it will be selected.<\/li><li>Otherwise, the list is empty, and there can be no selected item.<\/li><\/ul><\/li><li>Double-clicking on an item is equivalent to selecting it, and moving it to the other list; it is&nbsp;<em>exactly equivalent<\/em>&nbsp;in all respects to selecting it, and clicking on the appropriate move button. (This should suggest opportunities for code reuse.)<\/li><li>Be sure that clicking on the&nbsp;<strong>x<\/strong>&nbsp;in the upper right hand corner of the application window causes the program to exit. (Use the Swing facilities to do this.)<\/li><\/ul>\n\n\n\n<ul class=\"wp-block-list\"><li>When the upper button (&#8220;<code><strong>&gt;&gt;<\/strong><\/code>&#8220;) is clicked, the selected item in the left list is removed from the left list and inserted into the right list. Any selection in the right list is not changed.<\/li><li>When the lower button (&#8220;<code><strong>&lt;&lt;<\/strong><\/code>&#8220;) is clicked, any selected item in the left list is removed from the right list and inserted into the left list. Any selection in the left list is not changed.<\/li><li>In other words, list items are &#8220;shuttled&#8221; backwards and forwards between the list boxes.<\/li><li>After an item is selected in a list and then moved to the other list, the selection in the source list after the move will be as follows:<ul><li>If there is an item immediately following the original item, then it will be selected.<\/li><li>Otherwise, if there is an item immediately preceding the original item, then it will be selected.<\/li><li>Otherwise, the list is empty, and there can be no selected item.<\/li><\/ul><\/li><li>Double-clicking on an item is equivalent to selecting it, and moving it to the other list; it is&nbsp;<em>exactly equivalent<\/em>&nbsp;in all respects to selecting it, and clicking on the appropriate move button. (This should suggest opportunities for code reuse.)<\/li><li>Be sure that clicking on the&nbsp;<strong>x<\/strong>&nbsp;in the upper right hand corner of the application window causes the program to exit. (Use the Swing facilities to do this.)<\/li><\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><em><strong>NOTE:<\/strong><\/em> You will be using events;&nbsp;<em><strong>use the JDK 1.1+ event model; do&nbsp;<u>not<\/u>&nbsp;use the JDK 1.0 event model<\/strong><\/em>. (The JDK 1.1+ event model is the one that uses event listeners.)&nbsp; You will have to investigate what events are generated by the various components you&#8217;re using, and handle those events appropriately.<br>In particular, the&nbsp;<code><strong>JList<\/strong><\/code>&nbsp;Swing component has two distinct types of events it can generate.&nbsp; You will have to read the&nbsp;<code><strong>JList<\/strong><\/code>&nbsp;API (javadoc pages) carefully to determine both of them.<br>You will also most likely need to set up a model for your <strong>JList<\/strong> components.&nbsp; I found&nbsp;<code><strong>DefaultListModel<\/strong><\/code>&nbsp;to be a convenient model to use.<\/p><\/blockquote>\n\n\n\n<hr class=\"wp-block-separator is-style-wide\"\/>\n\n\n\n<div style=\"height:63px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"part-2\"><span class=\"ez-toc-section\" id=\"Part_2_A_Shuttle_Component-2\"><\/span>Part 2: A Shuttle Component<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Now, convert the Shuttle program you wrote in the last assignment into a <strong>Shuttle<\/strong>&nbsp;<strong><code>Component<\/code><\/strong>&nbsp;that may be added to a container (as other components like&nbsp;<code><strong>JButton<\/strong><\/code>,&nbsp;<code><strong>JList<\/strong><\/code>, etc. may be added to a container).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s what to do:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>Create a&nbsp;<code>Shuttle<\/code>&nbsp;Component ClassCreate a class called&nbsp;<code>Shuttle<\/code>, as follows:<\/strong><ul><li>It should extend&nbsp;<code><strong>JPanel<\/strong><\/code>. Why?<\/li><li>Create a default constructor for the class.&nbsp;<ul><li>It should contain roughly the code you placed in the equivalent class in the first section of this assignment. (Things like setting the background color, adding\/placing the left and right lists and the two buttons, etc.)<\/li><li>However, it should&nbsp;<em><strong>not<\/strong><\/em>&nbsp;contain code that moves anything into the two lists. That&#8217;s because we want to make those operations driven by other classes that will be making use of&nbsp;<strong>Shuttle<\/strong>&nbsp;components.<\/li><\/ul><\/li><li>Have the class implement the following methods:<ul><li><code><strong>public void addToLeft(String item)<\/strong><\/code><\/li><li><code><strong>public void addToLeft(String[] items)<\/strong><\/code><\/li><li><code><strong>public void removeFromLeft(String item)<\/strong><\/code><\/li><li><code><strong>public void removeFromLeft(String[] items)<\/strong><\/code><\/li><li><code><strong>public void addToRight(String item)<\/strong><\/code><\/li><li><code><strong>public void addToRight(String[] items)<\/strong><\/code><\/li><li><code><strong>public void removeFromRight(String item)<\/strong><\/code><\/li><li><code><strong>public void removeFromRight(String[] items)<\/strong><\/code><br>(It should be obvious what each of these methods will do.)<\/li><\/ul><\/li><li>You&#8217;ll have to have the&nbsp;<strong><code>Shuttle<\/code><\/strong>&nbsp;class implement the appropriate Listeners for the events that it will have to handle, and create the necessary methods for these Listeners. (Or use equivalent, perhaps inner, classes to handle them.)<\/li><\/ul><\/li><\/ol>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\"><li><strong>Add Support for a&nbsp;<code>ShuttleListener<\/code><\/strong><\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">We&#8217;re going to turn our&nbsp;<strong><code>Shuttle<\/code><\/strong>&nbsp;into a real component that generates its own events, so that other classes can express interest in listening for and handling those events. To do this, we&#8217;ll follow the JDK 1.1+ event model and create:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>A&nbsp;<strong><code>ShuttleEvent<\/code><\/strong>&nbsp;class,<\/li><li>A&nbsp;<strong><code>ShuttleListener<\/code><\/strong>&nbsp;class, and<\/li><li>A&nbsp;<strong><code>ShuttleAdapter<\/code><\/strong>&nbsp;class.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">What kind of events are we interested in for a Shuttle?&nbsp; Regardless of the internal details, the only things of external interest are:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>An item was added to the left list<\/li><li>An item was removed from the left list<\/li><li>An item was added to the right list<\/li><li>An item was removed from the right list<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">(Note that, if several items are added to a list in one operation (using the&nbsp;<strong><code>addToLeft(String[] items)<\/code><\/strong>&nbsp;or&nbsp;<strong><code>addToRight(String[] items)<\/code><\/strong>&nbsp;methods, or equivalent remove methods), an appropriate event should be generated&nbsp;<strong><em>for each separate item added<\/em><\/strong>.)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">What kinds of information are we interested in for each of these events? In each case, the information we wish to convey is the string being added or removed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So, create a public&nbsp;<strong><code>ShuttleEvent<\/code><\/strong>&nbsp;class, in package&nbsp;<strong><code>shuttle<\/code><\/strong>, as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>It should extend from class&nbsp;<strong><code>ComponentEvent<\/code><\/strong>.&nbsp;Why?<\/li><li>It should contain a set of&nbsp;<strong><code>public static final int<\/code><\/strong>s with unique values:&nbsp;<strong><code>LEFT_ITEM_ADDED, LEFT_ITEM_REMOVED, RIGHT_ITEM_ADDED, RIGHT_ITEM_REMOVED<\/code><\/strong><\/li><li>A public constructor that accepts the same datatypes as the <strong><code>ComponentEvent<\/code><\/strong> constructor, plus the string item that was added to, or removed from, the list. It should save the item string in the&nbsp;<strong><code>ShuttleEvent<\/code><\/strong>&nbsp;instance.<\/li><li>A&nbsp;<code><strong>public getItem()<\/strong><\/code>&nbsp;method that returns the item string.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Now, create a public&nbsp;<strong><code>ShuttleListener<\/code><\/strong>&nbsp;interface in package&nbsp;<strong><code>shuttle<\/code><\/strong>, as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>It should specify four methods:<ul><li><strong><code>public void leftItemAdded(ShuttleEvent event);<\/code><\/strong><\/li><li><strong><code>public void leftItemRemoved(ShuttleEvent event);<\/code><\/strong><\/li><li><strong><code>public void rightItemAdded(ShuttleEvent event);<\/code><\/strong><\/li><li><code><strong>public void rightItemRemoved(ShuttleEvent event);<\/strong><\/code><\/li><\/ul><\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">That was easy! Now, create a public&nbsp;<strong><code>ShuttleAdapter<\/code><\/strong>&nbsp;class in package&nbsp;<strong><code>shuttle<\/code><\/strong>, as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>What should it extend? Implement?<\/li><li>What methods should it contain?<\/li><\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><em><u><strong>Hint:<\/strong><\/u><\/em>&nbsp;Take a look at other similar classes in the JDK 1.x class library.<\/p><\/blockquote>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\"><li><strong>Use the&nbsp;<code>ShuttleListener<\/code>&nbsp;functionality<\/strong><\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Now, we can start to make use of these classes.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>To the&nbsp;<strong><code>Shuttle<\/code><\/strong>&nbsp;class, add the following methods:<ul><li><strong><code>public void addShuttleListener(ShuttleListener listener)<\/code><\/strong><\/li><li><strong><code>public void removeShuttleListener(ShuttleListener listener)<\/code><\/strong><\/li><\/ul><\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">You will need to implement the code to add and remove a <strong><code>ShuttleListener<\/code><\/strong>, and also add and implement the necessary methods within the&nbsp;<strong><code>Shuttle<\/code><\/strong>&nbsp;class to cause all the registered&nbsp;<strong><code>ShuttleListener<\/code><\/strong>s to be notified when an event of interest happens.<em><strong><u>Hints<\/u><\/strong><u>:<\/u>&nbsp;<\/em>You will have to be very careful to write code that can deliver the event notifications to the registered Listeners, while at the same time handling requests to add and remove&nbsp;<code><strong>ShuttleListener<\/strong><\/code>s. I recommend that you use the <code><strong>javax.swing.event.EventListenerList<\/strong><\/code> class to implement this;&nbsp; see its&nbsp;<a rel=\"noreferrer noopener\" target=\"_blank\" href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/javax\/swing\/event\/EventListenerList.html\">API javadocs<\/a>&nbsp;for details of how to do this.<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"4\"><li><strong>Generating Events from the Add\/Remove Methods<\/strong><\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Each of the methods:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code><strong>public void addToLeft(String item)<\/strong><\/code><\/li><li><code><strong>public void removeFromLeft(String item)<\/strong><\/code><\/li><li><code><strong>public void addToRight(String item)<\/strong><\/code><\/li><li><code><strong>public void removeFromRight(String item)<\/strong><\/code><\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">should not only perform the obvious addition or removal of an item to\/from the corresponding list,&nbsp;<strong><em>but should also<\/em><\/strong>&nbsp;<em><strong>generate an appropriate event corresponding to the event types listed above<\/strong><\/em>, in the&nbsp;<code><strong>ShuttleEvent<\/strong><\/code>&nbsp;class.&nbsp; <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By&nbsp;<em>generate an event<\/em>&nbsp;I mean the following:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Create an instance of&nbsp;<code><strong>ShuttleEvent<\/strong><\/code>, with the appropriate constructor arguments. This is the generated event.<\/li><li>Use the&nbsp;<code><strong>EventQueue<\/strong><\/code>&#8216;s&nbsp;<code><strong>postEvent()<\/strong><\/code>&nbsp;method to cause the event to be &#8220;fired&#8221; (see&nbsp;<a rel=\"noreferrer noopener\" target=\"_blank\" href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/EventQueue.html#postEvent(java.awt.AWTEvent)\">here<\/a>&nbsp;for documentation of&nbsp;<code><strong>postEvent<\/strong><\/code>)&nbsp;<\/li><li>The result of calling&nbsp;<code><strong>postEvent<\/strong><\/code>&nbsp;will be to cause the event to be delivered in the&nbsp;<em>Event Dispatch thread<\/em>, which will cause the component&#8217;s&nbsp;<code><strong>processEvent<\/strong><\/code>&nbsp;method to be invoked with this event as a parameter. (See&nbsp;<a rel=\"noreferrer noopener\" href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/Component.html#processEvent(java.awt.AWTEvent)\" target=\"_blank\">here<\/a>&nbsp;for documentation on&nbsp;<code><strong>processEvent<\/strong><\/code>)&nbsp; You will need to override the&nbsp;<strong>processEvent<\/strong>&nbsp;method in the shuttle component to cause it to process&nbsp;<code><strong>ShuttleEvent<\/strong><\/code>s &#8212;&nbsp;<strong>don&#8217;t forget to call the&nbsp;<\/strong><code><strong>processEvent<\/strong><\/code>&nbsp;<strong>method in the shuttle component&#8217;s superclass to process events that are&nbsp;<em>not<\/em>&nbsp;<\/strong><code><strong>ShuttleEvents<\/strong><\/code><strong>.&nbsp;<\/strong><\/li><li>By analogy with the existing methods such as&nbsp;<code><strong>processComponentEvent<\/strong><\/code>, etc., I recommend that you write a <code><strong>processShuttleEvent(ShuttleEvent ev)<\/strong><\/code> method to do the processing of&nbsp;<code><strong>ShuttleEvent<\/strong><\/code>s, called from your overriding <code><strong>processEvent<\/strong><\/code> method.&nbsp; The&nbsp;<code><strong>processShuttleEvent<\/strong><\/code>&nbsp;method should be responsible for determining what kind of&nbsp;<code><strong>ShuttleEvent<\/strong><\/code>&nbsp;is being delivered, and as a result, which entry point in each of the <code><strong>ShuttleListener<\/strong><\/code>s will be called.&nbsp; This is how your&nbsp;<code><strong>ShuttleEvent<\/strong><\/code>s are delivered to your&nbsp;&nbsp;<code><strong>ShuttleListener<\/strong><\/code>s.<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Note that the methods:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code><strong>public void addToLeft(String[] items)<\/strong><\/code><\/li><li><code><strong>public void removeFromLeft(String[] items)<\/strong><\/code><\/li><li><code><strong>public void addToRight(String[] items)<\/strong><\/code><\/li><li><code><strong>public void removeFromRight(String[] items)<\/strong><\/code><\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">should simply delegate the real processing of each individual item to the appropriate method to add or remove a single item.&nbsp; Otherwise, you&#8217;re making work for yourself!<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"5\"><li><strong>Test It!!!<\/strong><\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">By now, you should already have created some kind of test for the functionality you&#8217;ve created above. I created a&nbsp;<code><strong>ShuttleTest<\/strong><\/code>&nbsp;class that exercised the functionality as thoroughly as I could, and this allowed me to debug where things didn&#8217;t work.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In particular, I set up a class that acted as a&nbsp;<code><strong>ShuttleListener<\/strong><\/code>, which printed out a suitable message whenever a&nbsp;<code><strong>ShuttleEvent<\/strong><\/code>&nbsp;was received.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I expect you to do something very similar.&nbsp; In fact, I expect you to test all your assignments thoroughly!<\/p>\n\n\n\n<hr class=\"wp-block-separator is-style-wide\"\/>\n\n\n\n<div style=\"height:71px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"part-3\"><span class=\"ez-toc-section\" id=\"Part_3_A_Virtual_Supermarket-2\"><\/span>Part 3: A Virtual Supermarket<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Now write a Java program that uses several of these <strong>Shuttle<\/strong> components &#8212; a &#8220;Virtual Supermarket&#8221;.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is how it should look:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"590\" height=\"512\" src=\"https:\/\/bhiggs.x10hosting.com\/practical-java-programming\/wp-content\/uploads\/2021\/01\/VirtualSupermarket.gif\" alt=\"\" class=\"wp-image-223\"\/><\/figure><\/div>\n\n\n\n<div style=\"height:35px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">As you can see, it contains two <strong>Shuttle<\/strong> controls, labeled &#8220;Fresh Vegetables and Fruits&#8221; and &#8220;Groceries&#8221;, plus a &#8220;Shopping Cart&#8221; list on the right.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><em><strong><u>Hint:<\/u><\/strong><\/em>&nbsp;In order to get the alignment right, you will most likely have to use various components nested within panels, within panels, each using an appropriate choice of layout manager. Experiment!&nbsp; I expect this learning process to take some time, but I think you&#8217;ll find it worth the time.<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">As you move items in each shuttle control to the right, the item should be replicated in the shopping cart list, so you get a &#8220;roll-up&#8221; of all the items selected from each shuttle. Similarly, if you move items back to the left in each shuttle control, the corresponding entry should be removed from the shopping cart list.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Once you have the layout done, you&#8217;ve done most of the work! All you have to do, then, is supply the appropriate list of items to be added to the left hand side of each shuttle control, and then connect the shuttle control behavior up to the main class, so that you can &#8220;intercept&#8221; when items are added to, or removed from, the right list of each shuttle control. That, given the functionality you&#8217;ve added to the shuttle class as specified above, I think you&#8217;ll find it pretty easy to hook things up!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The virtual supermarket should use the shuttle events that are emitted by the shuttles as a result of user actions on those shuttles, in order to populate or to depopulate the Shopping Cart.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s what to do for the main class for this application,&nbsp;<strong><code>SuperMarket<\/code><\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Make it extend&nbsp;<code><strong>JFrame<\/strong><\/code>.<\/li><li>Choose a class which will implement&nbsp;<strong><code>ShuttleListener<\/code><\/strong>&nbsp;which will listen for&nbsp;<strong><code>ShuttleEvent<\/code><\/strong>s emitted from each Shuttle.<\/li><li><strong><code>SuperMarket<\/code><\/strong>&nbsp;will have a&nbsp;<strong><code>main<\/code><\/strong>&nbsp;entry point for the class, so you can run it as an application, or you can choose to make it an applet instead (or both).<\/li><li>The default constructor,&nbsp;<strong><code>SuperMarket()<\/code><\/strong>, should do most of the work in terms of setting up the layout for the appropriate set of components.&nbsp;<br>It will likely take some time to get the layout correct, and you will probably use a bunch of different layout managers in nested containers. I found that it simplified my code to use a number of inner classes, one for each panel in the interface;&nbsp; that made it easy to isolate the code for a panel, and know where things were happening.&nbsp;&nbsp;<em>Check that your frame behaves reasonably when you resize it.<\/em><br>In addition, you&#8217;ll need to populate the left lists of the two shuttle components with the appropriate sets of vegetable\/fruit or grocery items. Supply the two sets of items in the&nbsp;<strong><code>SuperMarket<\/code><\/strong>&nbsp;class.&nbsp;&nbsp;<br>Finally, it should register a&nbsp;<strong><code>ShuttleListener<\/code><\/strong>&nbsp;for both shuttle components.<\/li><li>In your shuttle listener class, if you are implementing the <strong><code>ShuttleListener<\/code><\/strong> interface directly, you must implement all of the four methods specified in that interface. Two of them will do nothing; the two that you truly implement will cause any items added to or removed from the right lists of the two shuttle controls to cause the same items to be entered into, or removed from, the &#8220;shopping cart&#8221; list.&nbsp;&nbsp;<\/li><li>Consider using a class that extends&nbsp;<strong><code>ShuttleAdapter<\/code><\/strong>&nbsp;for this purpose.&nbsp; You can&#8217;t do this if your&nbsp;<code><strong>JFrame<\/strong><\/code>&nbsp;or&nbsp;<code><strong>JPanel<\/strong><\/code>&nbsp;class is the listener, but If you use a separate class that extends&nbsp;<strong><code>ShuttleAdapter<\/code><\/strong>&nbsp;class, then you only have to implement two methods;&nbsp;<strong><code>ShuttleAdapter<\/code><\/strong>&nbsp;will already implement the remaining methods (which do nothing).<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Once you&#8217;ve implemented the basic stuff, like the layout of the above window, the work of connecting things together should be quite simple.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong>Note:<\/strong>&nbsp;If you implemented Part B correctly, you should need to make&nbsp;<strong><em>zero changes<\/em><\/strong>&nbsp;to your Shuttle classes.&nbsp; You should only be writing the necessary class(es) to use those Shuttle classes.&nbsp; I don&#8217;t want to see any new versions of the Shuttle classes supplied with Part C.&nbsp; If you can&#8217;t get your Part B classes to work in part C, go back to Part B and fix them, and only then return to working on Part C.<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">YAY! You&#8217;re done with this assignment! I hope you learned a lot!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A Graphical Shuttle A graphical shuttle, also know as a Dual Listbox, or Dual Multiselect, or List Builder, or List Shuttle is a graphical entity that allows the user to move items between two lists. It is often used to allow the user to choose from a number of items in a list (usually on [&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-204","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":"A Graphical Shuttle A graphical shuttle, also know as a Dual Listbox, or Dual Multiselect, or List Builder, or List Shuttle is a graphical entity that allows the user to move items between two lists. It is often used to allow the user to choose from a number of items in a list (usually on&hellip;","_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/204","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=204"}],"version-history":[{"count":9,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/204\/revisions"}],"predecessor-version":[{"id":225,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/204\/revisions\/225"}],"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=204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}