CS151 lecture notes, Fall 1999 Week 7, Friday Friday: finish Strings, start Chapter 8, hand out practice exam Monday: go over practice exam, continue Chapter 8 Wednesday: exam covers up through 8.5 Wednesday: two-part lab, first part you can do immediately, second part depends on all of Chapter 8 Friday: go over exam? finish Chapter 8 CHAPTER 7 Strings are immutable --------------------- toUpperCase and toLowerCase SOUND like they modify the String, but they don't they return a new String. Strings are incomparable ------------------------ Don't use == to compare Strings! Unfortunately, it is syntactically legal, but it does not do what you want. The equals method does. You invoke it on one String and pass the other as an argument, which is weird: s1.equals (s2) compareTo is similar, except that it returns an integer indicating which one is bigger, assuming that they are not equal: s1.compareTo (s2) returns negative if s1 comes before s2 in the alphabet positive if s2 comes before s1, 0 if they are the same The actual return is the DIFFERENCE between the ASCII/UNICODE codes for the first characters in the Strings that differ. Usually that's more than you need to know. sorting with compareTo ---------------------- One of the problems with compareTo is that it does not really know the rules for alphabetizing things. All it knows is the order things appear in the ASCII/UNICODE tables. For example, all the capital letters come before all the lowercase letters. This is a real problem in the world, when people use compareTo blindly. Example: faculty directory Some names have capital letters in funny places Some names are two words (they have a space in them) How does compareTo deal with spaces? Some names begin with lower-case letters. The history of the Colby directory indicates that these things are being ignored or dealt with in an ad hoc way. How can we deal with them ALGORITHMICALLY? I forgot part of the homework assignment. Before we compare names, we have to convert them to all lower (or upper) case. CHAPTER 8 Interesting objects ------------------- Although Strings are objects, they are atypical objects. Now we're going to talk about interesting objects. An object is a collection of storage locations that contain related information. Each storage location has a name, a type and a value, just like a variable. For example, a Point is an object that contains two storage locations, both have type int. They are named x and y. Point is an object type in Java. That means that you can create a variable with type Point. Point blank; The problem is that creating a Point variable does not create a Point object. In order to create a Point object, you have to use the new command: blank = new Point (3, 4); Draw the diagram of these two statements. Strictly speaking, the value of blank is a REFERENCE to the object that contains the values 3 and 4. The object that blank refers two contains two storage locations. The object that blank refers two is called an instance. The storage locations inside the object are called instance variables. Every Point object is an instance of the Point class. Every Point variable should refer to a Point object. Instance variables ------------------ You can access the instance variables of an object using dot notation. int x = blank.x; This would set x to 3. You can modify the instance variables of an object. blank.x = blank.x + 3;