{"id":583,"date":"2013-09-15T04:16:03","date_gmt":"2013-09-15T11:16:03","guid":{"rendered":"http:\/\/blogs.unsw.edu.au\/comp1400\/?p=583"},"modified":"2013-09-15T04:16:39","modified_gmt":"2013-09-15T11:16:39","slug":"week-8-lab-arraylists-and-classes","status":"publish","type":"post","link":"https:\/\/blogs.unsw.edu.au\/comp1400\/blog\/2013\/09\/week-8-lab-arraylists-and-classes\/","title":{"rendered":"Week 8 Lab &#8211; ArrayLists and Classes"},"content":{"rendered":"<p>Download the BlueJ project <a title=\"club.zip\" href=\"http:\/\/www.cse.unsw.edu.au\/~cs1400\/13s2\/examples\/club.zip\">club.zip<\/a> for this week&#8217;s exercises. There are three classes in the project that help you to represent a club and its members.<\/p>\n<p>The <strong>Member<\/strong> class is fully defined and contains all the methods you need. Note a couple of things we haven&#8217;t covered in lectures. The constructor uses a <strong>throw<\/strong> statement to create a Java runtime error. Try entering a month with a number outside the range 1 .. 12 and see what happens.<\/p>\n<p>The <strong>Member<\/strong> class also defines a method called <strong>toString<\/strong>. This returns a string representation of the object. If you try to print the object using System.out.println, the system will used <strong>toString<\/strong> to convert your object into a string for printing.<\/p>\n<p>The <strong>ClubDemo<\/strong> class is the &#8220;main&#8221; class that we use to create a club and members and test the other classes.<\/p>\n<p>The <strong>Club<\/strong> class is incomplete. The methods in the class are empty and your job is to write the code inside to make them work.<\/p>\n<h3>Question 1<\/h3>\n<p>The club class needs a way of storing a collection of members. You should use an ArrayList to do that. The class includes two constructors. Let&#8217;s start with the first one. It should initialise the ArrayList and leave it empty.<\/p>\n<h3>Question 2<\/h3>\n<p>The <strong>Join<\/strong> method is passed a new member as an argument. You should add this member to the ArrayList.<\/p>\n<h3>Question 3<\/h3>\n<p>The <strong>numberOfMembers<\/strong> should return the number of elements in the ArrayList.<\/p>\n<h3>Question 4<\/h3>\n<p>The <strong>listMembers<\/strong> method should go through the ArrayList of members and print each one, one per line.<\/p>\n<h3>Question 5<\/h3>\n<p>The <strong>removeMember<\/strong> method should remove a member from the ArrayList when a member decides to leave the club. The argument to the method is a string, which is the member&#8217;s name. You have to search the ArrayList to find the member with that name. Use a simple algorithm, just go through the list, one-by-one until you find the member with the given name and remove it from the ArrayList.<\/p>\n<h3>Question 6<\/h3>\n<p>The second constructor in the class allows you to pass an array of members. Initialise the club membership with the members in the array.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Download the BlueJ project club.zip for this week&#8217;s exercises. There are three classes in the project that help you to represent a club and its members. The Member class is fully defined and contains all the methods you need. Note a couple of things we haven&#8217;t covered in lectures. The constructor uses a throw statement [&hellip;]<\/p>\n","protected":false},"author":328,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[630],"tags":[],"class_list":["post-583","post","type-post","status-publish","format-standard","hentry","category-lab"],"_links":{"self":[{"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/posts\/583","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/users\/328"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/comments?post=583"}],"version-history":[{"count":2,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/posts\/583\/revisions"}],"predecessor-version":[{"id":585,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/posts\/583\/revisions\/585"}],"wp:attachment":[{"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/media?parent=583"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/categories?post=583"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/tags?post=583"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}