1 / 8

CPSC 233 Tutorial

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 );

gavril
Download Presentation

CPSC 233 Tutorial

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CPSC 233 Tutorial Xin Liu Feb 14, 2011

  2. 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"); } }

  3. 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

  4. 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

  5. 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

  6. 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>"); } } }

  7. 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

  8. 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

More Related