CS115 lecture notes, Spring 1999 Week 9, Monday QUIZ! Report from Homework 7 ---------------------- Always do something with the return value from a fruitful method!!! INDENT!!! Test your programs with a variety of cases. Comments on the Java rules for combining different types: 1) promoting characters to integers is stupid and inconsistent char c = 'a'; c++; is legal c = c + 1; is not legal because the RHS is an int c = 'a' + 1; is legal for god know what fucking reason The last is not only dumb, but is a major violation of one of the rules of composition, which is that you can replace any variable in an expression with its value. 2) Promoting things to String is not terrible, but it can be confusing. Think about these things! This kind of discussion is not secondary, or an afterthought. It is central to the material we are covering in this class. Remember: learning to program is just a means to an end. Understanding how computers work -- including programming language design -- is the end (or at least one of them). Packages -------- import command. Returning objects from methods ------------------------------ Examples on the quiz. Another way to get a Rectangle object is to invoke the getBounds method: public void paint (Graphics g) { Rectangle bbox = getBounds (); // find the window size paintBox (g, 0, 0, bbox.width, bbox.height); // draw something! } This only works when there is a Graphics window. You create the variable bbox, but the Java system creates the Rectangle object that contains the bounds. We extract the instance variables of this Rectangle using dot notation. Aliasing -------- Reasons objects are good ------------------------ 1) Chunk related information together. Chunking overcomes human cognitive limitations. 2) Share information between methods in controlled ways. 3) Return more than one value from a method. Dangers of objects ------------------ 1) Don't forget that when you create an object variable, you don't get an object. 2) Beware the NullPointerException! 3) For primitive local variables, methods cannot interfere with each other -- they are encapsulated. Objects undermine encapsulation, allowing methods to interact in complex ways that can be confusing. Functional methods and side-effects ----------------------------------- Homework 7 solutions -------------------- public class Palin { public static void main (String[] args) { System.out.println (isItPalindrome ("")); System.out.println (isItPalindrome ("a")); System.out.println (isItPalindrome ("dad")); System.out.println (isItPalindrome ("abba")); System.out.println (isItPalindrome ("palindromeemordnilap")); System.out.println (isItPalindrome ("dab")); System.out.println (isItPalindrome ("abca")); System.out.println (isItPalindrome ("palindroemordnilap")); System.out.println (convertName ("Allen Downey")); } // middle returns a substring that includes all but the // first and last; only works if s has at least two characters public static String middle (String s) { int length = s.length(); return s.substring (1, length-1); } // first returns the first character of a string // first only works if s has at least one characters public static char first (String s) { return s.charAt (0); } // last returns the last character of a string // last only works if s has at least one characters public static char last (String s) { int len = s.length(); return s.charAt (len - 1); } public static boolean isPalindrome (String s) { int len = s.length(); if (len == 0 || len == 1) { return true; } // at this point we know that len>=2, so it is safe // to invoke first, last, and middle if (first(s) != last(s)) { return false; } else { boolean recurse = isPalindrome (middle (s)); return recurse; } } public static boolean isItPalindrome (String s) { int len = s.length(); int i = 0; int j = len-1; while (i < j) { if (s.charAt(i) != s.charAt(j)) { return false; } i = i + 1; j = j - 1; } return true; } public static boolean hasComma (String s) { int index = s.indexOf(','); return (index != -1); } public static String convertName (String s) { if (hasComma (s)) { return s; } int space = s.indexOf(' '); int len = s.length(); String first = s.substring (0, space); String last = s.substring (space+1, len); return last + ", " + first; } }