870 likes | 1.24k Views
Scanning. Scanning image for text. Scanning frequencies for radio stations. Finding words in a sentence Finding identifiers, operators, in a program char String Variable-size type Indexing Enumeration. Primitive Types. int, double, boolean, long, short, float, byte char.
E N D
Scanning • Scanning image for text. • Scanning frequencies for radio stations. • Finding words in a sentence • Finding identifiers, operators, in a program • char • String • Variable-size type • Indexing • Enumeration
Primitive Types • int, double, boolean, long, short, float, byte • char
Primitive Types • Constants (Literals & Named Constants) • Operations with Invocation Syntax
16 bits char Escape sequence newline char Constants {letters, digits, operators ...} ‘\n’ ‘a’ ‘1’ ‘’’ ‘\’ ‘ ‘ ‘’ ‘\’’ ‘A‘ ‘< ‘ ‘ ‘\\’
ordinal number (integer code) Ordering Characters ‘’ …. ‘a’ …. position in ordered character list
‘’ …. ‘a’ ‘b’ ‘c’ …. ‘z’ …. ‘’ ‘’ …. …. ‘0’ ‘A’ ‘B’ ‘1’ ‘2’ ‘C’ …. …. ‘Z’ ‘3’ …. …. Ordering Characters ‘a’ > ‘b’ false ‘a’ > ‘A’ ??? ‘a’ > ‘0’ ??? ‘B’ > ‘A’ true ‘4’ > ‘0’ true ‘0’ > ‘’ true
Implicit cast to wider type Converting between Characters and their Ordinal Numbers (int) ‘a’ ordinal number of ’a’ (char) 55 character whose ordinal number is 55 (int) ‘c’ - (int) ‘a’ 2 (int) ‘’ 0 ‘c’ - ‘a’ 2 (char) 0 ‘’ (int) ‘d’ ??? (char) (‘c’ - 2) ‘a’ (char) 1 ??? (char) (‘A’ + 2) ‘C’ (char) -1 (char) (‘C’ - ‘A’ + ‘a’) ‘c’
A Useful Character Operation Character.isLetter(c) true if c is a letter Character.isLetter(‘a’) true Character.isLetter(‘A’) true Character.isLetter(‘1’) false Character.isLetter(‘ ’) false
variable size Object Type String constants String {sequences of characters} “hello\n\n123” “hello” “hello 123” ‘a’ “” “123” “a” “\” “\\”
index StringIndexBounds exceptiom Accessing String Components String s = “hello world”; s.getFirstChar() s.charAt(0) ‘h’ s.charAt(1) ‘e’ s.getSecondChar() s.charAt(-1) ... s.charAt(11) s.length() 11 1 “ ”.length() “”.length() 0
StringIndexBounds exceptiom Accessing SubString public String substring (int beginIndex, int endIndex) s.substring(beginIndex, endIndex) s.charAt(beginIndex) .. s.charAt(endIndex-1) “hello world”.substring(4,7) “o w” “hello world”.substring(4,4) “” “hello world”.substring(7,4)
three different instances Changing Strings? Strings are read-only (immutable) “hello” + “world” “hello world”
Useful String Operations s.toLowerCase() copy of s with letters converted to lower case s.toUpperCase() copy of s with letters converted to upper case “Hello World”.toLowerCase() “hello world” “Hello World”.toUpperCase() “HELLO WORLD”
String Processing • int i = 0; • while (i < s.length()) { • System.out.println (s.charAt(i)); • i++; • } prints each character on separate line
loop condition loop body Dissecting a Loop • int i = 0; • while (i < s.length()) { • System.out.println (s.charAt(i)); • i++; • }
initalizing loop variables loop condition real body Resetting loop variable Finer-grained Dissection • int i = 0; • while (i < s.length()) { • System.out.println (s.charAt(i)); • i++; • } • for (int i=0; i<s.length(); i++) • System.out.println(s.charAt(i));
Meaning of For Loop for (S1; E; S2) S3 • for (; E; S2) • S3 • for (; E; ) • S3 • for (; ; ) • S3 S1; while ( E) { S3; S2; } while ( E) { S3; S2; } while ( E) { S3; } while ( true) S3;
token token Multi-line input stream Line stream Line 1 Line 2 J o h n F . K e n n e d y token token token Scanning Input stream Token Stream
Solution • Monolithic Solution • Class Decomposition
Algorithm String inputLine
marker 0 Algorithm String inputLine J o h n F . K e n n e d y Output: J
Algorithm String inputLine J o h n F . K e n n e d y marker 1 Output: J
Algorithm String inputLine J o h n F . K e n n e d y marker 2 Output: J
Algorithm String inputLine J o h n F . K e n n e d y marker 5 Output: JF
Algorithm String inputLine J o h n F . K e n n e d y marker 6 Output: JF
Algorithm String inputLine J o h n F . K e n n e d y marker 8 Output: JFK
Algorithm String inputLine J o h n F . K e n n e d y marker 9 Output: JFK
Algorithm String inputLine J o h n F . K e n n e d y marker 14 Output: JFK
public class UpperCasePrinter { • publicstaticvoid main (String args[]) { • String input = getInput(); • int index = 0; • System.out.println("Upper Case Letters :"); • while (index < input.length()) { • char nextChar = input.charAt(index); • if (Character.isUpperCase(nextChar)) • System.out.print(nextChar); // token processing • index++; • } • } • publicstatic String getInput() { • System.out.println("Please enter a string"); • return Keyboard.readLine(); • } • } Monolithic Solution
Storing instead of printing tokens String inputLine J o h n F . K e n n e d y marker 14 String s = “JFK”;
No reuse in Monolithic Solutions String s = ""; // token processing int index = 0; while (index < input.length()) { char nextChar = input.charAt(index); if (Character.isUpperCase(nextChar)) s += nextChar; // token processing index++; } int index = 0; System.out.println("Upper Case Letters :");//token processing while (index < input.length()) { char nextChar = input.charAt(index); if (Character.isUpperCase(nextChar)) System.out.print(nextChar); // token processing index++; }
Class Decomposition? Main Class
Class Decomposition instantiate Scanner Class Scanner Object calls Scanner User Main Class (Input & Output)
Class Decomposition instantiate DataInputStream Instance DataInputStream readLine() Scanner User Main Class (Input & Output)
Scanner User-Scanner Object Interaction • DataInputStream dataIn = new DataInputStream (System.in); • int product = 1; • while (true) { • int num = Integer.parseInt (dataIn.readLine()); • if (num < 0) break; • product = product*num; • } • System.out.println (product);
DataInputStream Operations Multi-line input stream Line stream Line 1 Line 2 dataIn.readLine() Line 1 dataIn.readLine() Line 2 dataIn.readLine() IOException
Scanner Interface? Input stream Token Stream token 1 token 2 scanner.nextElement() token 1 scanner.nextElement() token 2 scanner.nextElement() ScannerException
Scanner Interface? Input stream Token Stream token 1 token 2 scanner.nextElement() token 1 scanner.nextElement() token 2 scanner.hasMoreElements() false scanner.nextElement() ???
J o h n F . K e n n e d y token 1 token 2 token 3 Uppercase Scanner Interface? scanner.nextElement() ‘J’ scanner.nextElement() ‘F’ scanner.nextElement() ‘K’ scanner.hasMoreElements() false scanner.nextElement() ???