{"id":433,"date":"2012-09-16T19:01:58","date_gmt":"2012-09-17T02:01:58","guid":{"rendered":"http:\/\/blogs.unsw.edu.au\/comp1400\/?p=433"},"modified":"2012-09-16T22:17:22","modified_gmt":"2012-09-17T05:17:22","slug":"tut-wk-9-classes-and-lists","status":"publish","type":"post","link":"https:\/\/blogs.unsw.edu.au\/comp1400\/blog\/2012\/09\/tut-wk-9-classes-and-lists\/","title":{"rendered":"Tut Wk 9 &#8211; Classes and Lists"},"content":{"rendered":"<h3>Classes<\/h3>\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\/labs\/Lab9.zip\">Lab9 BlueJ project<\/a>. It defines a new class <code>Student<\/code> which contains some student information<\/li>\n<li>Create students a number of students in BlueJ. Use the Inspector to examine their  fields.<\/li>\n<li>Use the <code>getName()<\/code> method to access the student&#8217;s 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&#8217;s 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&#8217;s 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 <code>static final<\/code> 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:<\/li>\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<\/ul>\n<h3>Lists<\/h3>\n<ul>\n<li>Create an ArrayList of strings in BlueJ using the <strong>Tools &gt; Use Library Class&#8230;<\/strong> menu.<\/li>\n<li>Right click on the list object to see the <strong>public interface<\/strong>, the list of available methods.<\/li>\n<li>Double click on the object to inspect the <strong>private implementation<\/strong>, the fields internal to the list object.<\/li>\n<li>Call the <code>size<\/code> method to check that the list is initially empty.<\/li>\n<li>Call the <code>add<\/code> method to add some names to the list.<\/li>\n<li>Use the inspector to see how the fields have changed.<\/li>\n<li>How does the <code>elementData<\/code> field change as you add names to the list? What happens if you add more than 10 names?<\/li>\n<li>Use the <code>get<\/code> 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 <code>StringUtils<\/code> with the following methods:<br \/>\n<code>public String findLongest(ArrayList)<\/code><\/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()\"><code>length()<\/code> method<\/a> on the String class to find this value.<\/p>\n<p><code>public ArrayList findStartsWith(String prefix)<\/code><br \/>\nThis method should search through the list 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, int)\"><code>startsWith()<\/code> method<\/a> to help you.<\/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 students a number of students in BlueJ. Use the Inspector to examine their fields. Use the getName() method [&hellip;]<\/p>\n","protected":false},"author":80,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[630,1],"tags":[],"class_list":["post-433","post","type-post","status-publish","format-standard","hentry","category-lab","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/posts\/433","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\/80"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/comments?post=433"}],"version-history":[{"count":7,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/posts\/433\/revisions"}],"predecessor-version":[{"id":440,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/posts\/433\/revisions\/440"}],"wp:attachment":[{"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/media?parent=433"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/categories?post=433"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/tags?post=433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}