{"id":144,"date":"2021-01-01T21:45:19","date_gmt":"2021-01-01T21:45:19","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/practical-java-programming\/?page_id=144"},"modified":"2021-01-02T21:01:12","modified_gmt":"2021-01-02T21:01:12","slug":"assignment-3","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/assignments\/assignment-3\/","title":{"rendered":"Assignment 3"},"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-6a6d468e80ba2\" 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-6a6d468e80ba2\"  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-3\/#Data_Abstraction_Encapsulation_and_Arrays\" >Data Abstraction, Encapsulation, and Arrays<\/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-3\/#A_Card_Game\" >A Card Game<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Data_Abstraction_Encapsulation_and_Arrays\"><\/span>Data Abstraction, Encapsulation, and Arrays<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"A_Card_Game\"><\/span>A Card Game<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The assignment is to write a simple card game program, using Java classes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Because I&#8217;m not expecting you to come up with your own design for such a thing at this stage of the game (pun intended), I&#8217;ve come up with a&nbsp;<a href=\"http:\/\/bhiggs.x10hosting.com\/Javadocs\/card-docs\/\" target=\"_blank\" rel=\"noreferrer noopener\">Program Specification<\/a>, which is in the form of&nbsp;javadoc-generated web pages.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As you&#8217;ll see, I&#8217;ve broken down the problem into classes that represent a number of&nbsp;<strong>abstractions<\/strong><strong>:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Card<\/strong>, representing a playing card (surprise!)<\/li><li><strong>CardFace<\/strong>, which represents the face value of a card<\/li><li><strong>CardSuit<\/strong>, which represents a card&#8217;s suit<\/li><li><strong>CardDeck<\/strong>, representing a deck of cards<\/li><li><strong>CardHand<\/strong>, representing a hand of cards<\/li><li><strong>CardGame<\/strong>, representing a game of cards<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">I&#8217;ve also introduced a couple of abstractions that have to do with the ability to sort:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The abstract class&nbsp;<code><strong>Sort<\/strong><\/code>, which contains a static method&nbsp;<code><strong>quicksort()<\/strong><\/code>&nbsp;which sorts an array of&nbsp;<code><strong>Sortable<\/strong><\/code>s<\/li><li>The interface&nbsp;<code><strong>Sortable<\/strong><\/code>, which defines what it means for a class to be sortable, using the class&nbsp;<code><strong>Sort<\/strong><\/code><\/li><\/ul>\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;At this point in the course, we probably won&#8217;t have covered interfaces yet. Don&#8217;t worry about that; you won&#8217;t need to know too much about them, other than the syntax to use when you&#8217;re writing one (an interface actually looks quite similar to a class except for the keyword&nbsp;<code>interface<\/code>) and how to use it from another class (the&nbsp;<code>implements<\/code>&nbsp;keyword).<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">I&#8217;ve placed all the classes that relate to the card game into a single package,&nbsp;<code><strong>cards<\/strong><\/code>, while I&#8217;ve separated the&nbsp;<code><strong>Sort<\/strong><\/code>&nbsp;class and the&nbsp;<code><strong>Sortable<\/strong><\/code>&nbsp;interface into a different package,&nbsp;<code><strong>sort<\/strong><\/code>.&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><em><strong><u>Why do think I did this?<\/u><\/strong><\/em><\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The specification should be abundantly clear about what you&#8217;re required to implement; it simply leaves the implementation details (how to do it) up to you! Note that I&#8217;ve designed the classes in such a way that their contents are&nbsp;<em><strong>encapsulated<\/strong><\/em>; that is, their internals are not exposed to the rest of the world &#8212; the functionality of the class is available only through a well-defined set of public methods.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><em><strong>You may not change the specification, add to it, nor subtract from it: the implementation must match the specification correctly, and without change. In particular, the public and private attributes should not be changed &#8212; this is crucial to the concept of encapsulation and data hiding &#8212; and the packages specified should be used.<\/strong><\/em><\/p><\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><em><strong>There&#8217;s one exception to the above: You may add a&nbsp;<code>main()<\/code>&nbsp;method to any of the classes &#8212; having a&nbsp;<code>main()<\/code>&nbsp;method is very useful to allow you to test each class in isolation as you develop it. Once you&#8217;ve spent time creating such&nbsp;<code>main()<\/code>&nbsp;methods, it&#8217;s a good idea not to remove them; eventually, you&#8217;ll likely augment or change a class, based on an updated specification, and you don&#8217;t want to have to reinvent the wheel all over again.<\/strong><\/em><\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>A few points:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>I&#8217;ve tried to structure things in such a way that it becomes obvious what the objects are in our problem, and how they are represented by Java classes. It should also be clear what attributes and behavior belong in each class.<\/li><li>I&#8217;m using\u00a0<code><strong>CardFace<\/strong><\/code>\u00a0and\u00a0<code><strong>CardSuit<\/strong><\/code>\u00a0primarily as mechanisms to improve type safety, and also to encapsulate the various constants (ACE, HEARTS, etc.).<\/li><li>In this application,\u00a0<code><strong>CardGame<\/strong><\/code>\u00a0is pretty dumb. Its\u00a0<code><strong>play()<\/strong><\/code>\u00a0method plays a card from each hand, and the highest card wins; then the play repeats until all the cards have been played.<\/li><li>While you&#8217;re implementing these classes, you&#8217;d be well advised to develop each class as much in isolation as possible, using a\u00a0<code><strong>main()<\/strong><\/code>\u00a0method in each class as a test harness. Since the classes have interdependencies, you&#8217;ll find it easier to create them in a specific order.<br>In my solution, I developed the classes in the approximate order:<ul><li><a rel=\"noreferrer noopener\" href=\"http:\/\/bhiggs.x10hosting.com\/Courses\/JavaProgramming\/java_source\/Cards\/CardSuit.java\" target=\"_blank\">CardSuit<\/a>\u00a0and\u00a0<a rel=\"noreferrer noopener\" href=\"http:\/\/bhiggs.x10hosting.com\/Courses\/JavaProgramming\/java_source\/Cards\/CardFace.java\" target=\"_blank\">CardFace<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"http:\/\/bhiggs.x10hosting.com\/Courses\/JavaProgramming\/java_source\/Cards\/Sortable.java\" target=\"_blank\">Sortable<\/a>\u00a0and\u00a0<a rel=\"noreferrer noopener\" href=\"http:\/\/bhiggs.x10hosting.com\/Courses\/JavaProgramming\/java_source\/Cards\/Sort.java\" target=\"_blank\">Sort<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"http:\/\/bhiggs.x10hosting.com\/Courses\/JavaProgramming\/java_source\/Cards\/Card.java\" target=\"_blank\">Card<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"http:\/\/bhiggs.x10hosting.com\/Courses\/JavaProgramming\/java_source\/Cards\/CardDeck.java\" target=\"_blank\">CardDeck<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"http:\/\/bhiggs.x10hosting.com\/Courses\/JavaProgramming\/java_source\/Cards\/CardHand.java\" target=\"_blank\">CardHand<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"http:\/\/bhiggs.x10hosting.com\/Courses\/JavaProgramming\/java_source\/Cards\/CardGame.java\" target=\"_blank\">CardGame<\/a><\/li><\/ul><\/li><li>I&#8217;ve even made things easier for you! Click on each of the above classes to get a skeleton version of the source for that class.\u00a0 It gets better &#8212; <code><strong>CardSuit<\/strong><\/code>, <code><strong>CardFace<\/strong><\/code>, <code><strong>Sortable<\/strong><\/code>, <code><strong>Sort<\/strong><\/code> and<code><strong>CardGame<\/strong><\/code> are already completed for you. So all you have to do is complete <code><strong>Card<\/strong><\/code>, <code><strong>CardDeck<\/strong><\/code>, and <code><strong>CardHand<\/strong><\/code>.You can use <code><strong>CardSuit<\/strong><\/code> and <code><strong>CardFace<\/strong><\/code> as examples of how to implement aspects of some of the other classes. You can use <code><strong>CardGame<\/strong><\/code>, ultimately, as a test of your implementation.<\/li><li>I fully expect you to have questions as you work on implementing to this spec. Don&#8217;t be afraid to contact me!<\/li><li>Now it&#8217;s time to go off and study the <a href=\"http:\/\/bhiggs.x10hosting.com\/Javadocs\/card-docs\/\" target=\"_blank\" rel=\"noreferrer noopener\">specification<\/a> in more detail!<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Data Abstraction, Encapsulation, and Arrays A Card Game The assignment is to write a simple card game program, using Java classes. Because I&#8217;m not expecting you to come up with your own design for such a thing at this stage of the game (pun intended), I&#8217;ve come up with a&nbsp;Program Specification, which is in the [&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-144","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":"Data Abstraction, Encapsulation, and Arrays A Card Game The assignment is to write a simple card game program, using Java classes. Because I&#8217;m not expecting you to come up with your own design for such a thing at this stage of the game (pun intended), I&#8217;ve come up with a&nbsp;Program Specification, which is in the&hellip;","_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/144","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=144"}],"version-history":[{"count":4,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/144\/revisions"}],"predecessor-version":[{"id":241,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/144\/revisions\/241"}],"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=144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}