80 likes | 233 Views
CPSC 233 Tutorial. Xin Liu Feb 14, 2011. Tips on keyboard input. How to detect that the user just hit enter when prompted for a string. import java.util.Scanner ; class StringShort { public static void main (String [] args ) { Scanner in = new Scanner(System.in );
E N D
CPSC 233 Tutorial Xin Liu Feb 14, 2011
Tips on keyboard input • How to detect that the user just hit enter when prompted for a string import java.util.Scanner; class StringShort { public static void main (String [] args) { Scanner in = new Scanner(System.in); String st = in.nextLine (); if (st.length() == 0) System.out.println("Blank"); else System.out.println("Not blank"); } }
String Comparison • aString.compareTo (String anotherString)aString.compareToIgnoreCase (String anotherString) • returns • negative integer if aString < anotherString • positive integer if aString > anotherString • 0 if aString == anotherString
String Comparison– an example public class StringCompare { public static void main (String [] args) { String s1; String s2; int comparison; // Case 1: s1 before s2 s1 = "aaa"; s2 = "aab"; comparison = s1.compareToIgnoreCase(s2); System.out.println(s1 + " " + s2 + " " + comparison); // Case 2: s2 before s1 s1 = "aaa"; s2 = "a"; comparison = s1.compareToIgnoreCase(s2); System.out.println(s1 + " " + s2 + " " + comparison); // Case 1: s1, s2 identical s1 = "wei!"; s2 = "wei!"; comparison = s1.compareToIgnoreCase(s2); System.out.println(s1 + " " + s2 + " " + comparison); } } The output: aaaaab -1 aaa a 2 wei! wei! 0
Debug Mode • What is “Debug Mode”? • A mode in which some detailed info is printed to help find errors • How to implement? • Make a boolean-type “global variable” (using a static attribute) • Print out some info when the boolean variable is true
Debug mode – an example public class Mode { public static boolean debug = false; } public class DriverDebug { public static void main (String [] args) { if (Mode.debug == true) { // First debug message: never appears because by default mode is off System.out.println(">>Debug message: <Insert debugging message here>"); } // Second debug message: appears because it's now turned on. Mode.debug = true; if (Mode.debug == true) { System.out.println(">>Debug message: <Insert debugging message here>"); } } }
Draw UML with MS Word • Classes • Insert a “text box” • Insert a 2-row table in the text box • write class name in the first row • write attributes and methods in the second row • use a “-” for private • use a “+” for public • Relationships • Draw lines with/without arrow heads • Multiplicities • Insert a “text box” • write a number in the text box
Linked list -- continued • Add “debug mode” • Add new methods • insertInOrder () • insert new nodes to positions so that the list is in an ascending order • removeWithName () • remove a node with given book name