{"id":594,"date":"2013-09-22T06:01:18","date_gmt":"2013-09-22T13:01:18","guid":{"rendered":"http:\/\/blogs.unsw.edu.au\/comp1400\/?p=594"},"modified":"2013-09-22T18:57:52","modified_gmt":"2013-09-23T01:57:52","slug":"week-9-lab-classes-and-arraylist","status":"publish","type":"post","link":"https:\/\/blogs.unsw.edu.au\/comp1400\/blog\/2013\/09\/week-9-lab-classes-and-arraylist\/","title":{"rendered":"Week 9 Lab &#8211; Classes and ArrayLists"},"content":{"rendered":"<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 <a href=\"http:\/\/www.cse.unsw.edu.au\/~cs1400\/13s2\/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 <strong>Course<\/strong>, that contains an <strong>ArrayList<\/strong>, called <strong>students<\/strong>, that stores all the students in a class. It should also have fields for the class id, like COMP1400 or INFS1609 and a <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 <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 <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>Classes Consider a database of student records for the university. What kind of data would be associated with each student? Download the Lab9 BlueJ project. It defines a new class Student which contains some student information Create a few different students in BlueJ. Use the Inspector to examine their fields. Use the getName() method 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-594","post","type-post","status-publish","format-standard","hentry","category-lab"],"_links":{"self":[{"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/posts\/594","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=594"}],"version-history":[{"count":4,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/posts\/594\/revisions"}],"predecessor-version":[{"id":596,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/posts\/594\/revisions\/596"}],"wp:attachment":[{"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/media?parent=594"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/categories?post=594"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/tags?post=594"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}