{"id":768,"date":"2014-09-22T00:13:45","date_gmt":"2014-09-21T14:13:45","guid":{"rendered":"http:\/\/blogs.unsw.edu.au\/comp1400\/?p=768"},"modified":"2014-09-22T00:14:21","modified_gmt":"2014-09-21T14:14:21","slug":"week-9-lab-classes-and-arraylists","status":"publish","type":"post","link":"https:\/\/blogs.unsw.edu.au\/comp1400\/blog\/2014\/09\/week-9-lab-classes-and-arraylists\/","title":{"rendered":"Week 9 Lab &#8211; Classes and ArrayLists"},"content":{"rendered":"<p>The self-test quiz for week9 can be found <a href=\"http:\/\/cse.unsw.edu.au\/~cs1400\/14s2\/quizzes\/Week%209%20Quiz.pdf\">here<\/a>.<\/p>\n<p><strong>Classes<\/strong><\/p>\n<p>Consider a database of student records for the university. What kind of data would be associated with each student?<\/p>\n<ul>\n<li>Download the\u00a0<a href=\"http:\/\/www.cse.unsw.edu.au\/~cs1400\/14s2\/examples\/Lab9.zip\">Lab9 BlueJ project<\/a>. It defines a new class Student which contains some student information<\/li>\n<li>Create a few different students in BlueJ. Use the Inspector to examine their fields.<\/li>\n<li>Use the getName() method to access the student\u2019s name.<\/li>\n<li>Write your own accessor methods to get the address and year enrolled.<\/li>\n<li>Add a field to record the student\u2019s gender. Update the constructor to initialise this value appropriately using a parameter. Add an accessor method.<\/li>\n<li>Add a field to record the student\u2019s age. Update the constructor to initialise the age to the default value of 18.<\/li>\n<li>Add a second constructor that allows you to specify an age other than the default.<\/li>\n<li>Add a static final int constant to the class for the default age, to avoid using 18 as a magic number in your code.<\/li>\n<\/ul>\n<p>Consider designing a class representing a car in a database for a used car lot.<\/p>\n<ul>\n<li>What kinds of data would be recorded about each car?<\/li>\n<li>What are the types of this data?<\/li>\n<li>Write a Car class representing an entry in this database. Give it:\n<ol>\n<li>Fields recording the above data.<\/li>\n<li>A public constructor.<\/li>\n<li>Public accessor methods to read the data.<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<p><strong>ArrayLists<\/strong><\/p>\n<ul>\n<li>Create a new class\u00a0<strong>Course<\/strong>, that contains an\u00a0<strong>ArrayList<\/strong>, called\u00a0<strong>students<\/strong>, that stores all the students in a class. It should also have fields for the class id, like COMP1400 and a\u00a0<strong>String<\/strong> type for the course name.<\/li>\n<li>Call the size method to check that the list is initially empty.<\/li>\n<li>Call the add method to add some students to the list.<\/li>\n<li>Use the inspector to see how the fields have changed.<\/li>\n<li>How does the elementData field change as you add names to the list? What happens if you add more than 10 names?<\/li>\n<li>Use the get method to read items off the list. What happens if you try to read an item beyond the end of the list? Explain the message you receive.<\/li>\n<\/ul>\n<p>Create a new class called StringUtils with the following methods:<br \/>\npublic String findLongest(ArrayList)<\/p>\n<p>This method should search through a list of strings to find the longest one. You can use the\u00a0<a href=\"http:\/\/docs.oracle.com\/javase\/6\/docs\/api\/java\/lang\/String.html#length()\">length() method<\/a> on the String class to find this value. Test this on the ArrayList of students.<\/p>\n<p>public ArrayList findStartsWith(String prefix)<br \/>\nThis method should search through the ArrayList and collect all strings that start with the given prefix. You can use the\u00a0<a href=\"http:\/\/docs.oracle.com\/javase\/6\/docs\/api\/java\/lang\/String.html#startsWith(java.lang.String,%20int)\">startsWith() method<\/a> to help you.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The self-test quiz for week9 can be found here. Classes Consider a database of student records for the university. What kind of data would be associated with each student? Download the\u00a0Lab9 BlueJ project. It defines a new class Student which contains some student information Create a few different students in BlueJ. Use the Inspector to [&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-768","post","type-post","status-publish","format-standard","hentry","category-lab"],"_links":{"self":[{"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/posts\/768","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=768"}],"version-history":[{"count":3,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/posts\/768\/revisions"}],"predecessor-version":[{"id":771,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/posts\/768\/revisions\/771"}],"wp:attachment":[{"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/media?parent=768"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/categories?post=768"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/tags?post=768"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}