Shaun Conlon CS 211 Section 6 1/30/2003 Notes 1) Any Problems with Exercise 1/Assignment 1? You should submit them both ASAP if you haven't already. 2) Integer Division and Modulo Arithmatic in Java -Integer Division (/): Java does integer division by returning the quotient without the remainder; so, a/b is equivalent to floor(a/b) Ex: 5/3 = 1 7/2 = 3 15/6 = 2 -Modulo (%): Java returns the remainder from a division with % Ex: 5%3 = 2 7%2 = 1 15%6 = 3 Notice: a = (a/b) * b + a % b Ex: 15 = (15 / 6) * 6 + 15 % 6 = 2 * 6 + 3 = 12 + 3 = 15 3) Using / and % to extract digit(s) from a number: Given: x = 5621781105 How do we get: 62, 178, 11, 8? -Notice that each time we divide by 10, we chop off one digit from the right hand side. Also, notice that % 10 gets the rightmost digit (% 100 gets the rightmost 2, etc.) So, to get a sequence of digit(s) from a large number, we: 1) Trim the right hand side with / 10^x 2) Extract the digits with % 10^y where x = number of digits to right of desired number y = number of digits in desired number Ex: 62: (x / 100000000) % 100 178: (x / 100000) % 1000 11: (x / 100) % 100 8: (x / 10000) % 10 4) Getter/setter methods + Encapsulation -The Person class given in lecture is not written in proper Java style. We usually want to hide the data that a class is holding by making that data private and guarding access to the data with getter/setter methods. This is called encapsulating the data. This prevents users from setting the value of a class variable to something bad and it can also restrict access to variables. See the attached Person.txt for a better version of the Person class. 5) Introduction to Linked Lists: -Linked lists are chains of nodes that each hold one piece of data. In Java, we use a variable of type Object to hold data so that anything can be stored in the linked list (primitive types can be stored via wrapper classes - ex: Integer is a wrapper class for the type int). Diagram of a simple linked list: ___ ___ ___ ___ |A| ---> |B| ---> |C| ---> |D| ---> --- --- --- --- | V - - - (the arrow to the right with the three hyphens represents a null reference) See the file ListNode.txt for the code for the ListNode class. 6) Searching a Linked List: We can search a linked list iteratively by checking each list element with the .equals() function. The static search method in ListNode.txt demonstrates this. The method is static because it pertains to the entire class, rather than one particular instance of the class. Email me (sc235) with any questions.