1.67k likes | 1.78k Views
Comp 114 Foundations of Programming. Instructor: Prasun Dewan. Topics Assumed & Reviewed. Types int, double, char, String Variables, constants, expressions Arrays Assignment, conditionals, loops Procedures/Functions/Subroutines/Methods Parameters/arguments. Hello World.
E N D
Comp 114Foundations of Programming Instructor: Prasun Dewan
Topics Assumed & Reviewed • Types • int, double, char, String • Variables, constants, expressions • Arrays • Assignment, conditionals, loops • Procedures/Functions/Subroutines/Methods • Parameters/arguments
directory/library main header Array of user-supplied arguments Programmer-defined Keyword Predefined Hello World package warmup; publicclass AHelloWorldGreeter { publicstaticvoid main (String[] args) { System.out.println ("Hello World"); } }
user-supplied argument Main Arguments
Second argument First argument Main Arguments package warmup; publicclass AnArgPrinter { publicstaticvoid main (String[] args) { System.out.println (args[0]); } } args[0] args[1] ...
program refers to argument array element user- supples no argument exception! Main Arguments package warmup; publicclass AnArgPrinter { publicstaticvoid main (String[] args) { System.out.println (args[0]); } }
Safe Arg Printer (edit in class) package warmup; publicclass ASafeArgPrinter { publicstaticvoid main (String[] args) { //args.length gives number of elements in args array. }
Safe Arg Printer package warmup; publicclass ASafeArgPrinter { publicstaticvoid main (String[] args) { if (args.length == 1) System.out.println (args[0]); else { System.out.println("Illegal no of arguments:" + args.length + ". Terminating program"); System.exit(-1); } } }
if-else Statement • if (<bool expr>) • <statement 1> • else • <statement 2>
<statement 1> <statement 2> if-else Statement true false <bool expr>
Compound Statement if (args.length == 1) System.out.println (args[0]); else { System.out.println("Illegal no of arguments:" + args.length + ". Terminating program"); System.exit(-1); }
Nested if-else • publicstaticchar toLetterGrade (int score) { • if (score >= A_CUTOFF) • return 'A'; • elseif (score >= B_CUTOFF) • return 'B'; • elseif (score >= C_CUTOFF) • return 'C'; • elseif (score >= D_CUTOFF) • return 'D'; • else • return 'F'; • }
Nested if-else • if (score >= A_CUTOFF) • return 'A'; • else • if (score >= B_CUTOFF) • return 'B'; • else • if (score >= C_CUTOFF) • return 'C'; • else • if (score >= D_CUTOFF) • return 'D'; • else • return 'F';
If Statement • if (args.length = = 1) • System.out.println (”args[0]”); • if (<bool expr>) • <statement>;
Printing Multiple Arguments (edit in class) package warmup; publicclass AnArgsPrinter { publicstaticvoid main(String[] args) { } }
Printing Multiple Arguments package warmup; publicclass AnArgsPrinter { publicstaticvoid main(String[] args) { int argNo = 0; while (argNo < args.length) { System.out.println(args[argNo]); argNo++; } } }
if vs. while Statement • if (<bool expr>) • <statement>; • while (<bool expr>) • <statement>;
if Statement true <bool expr> <statement> false
while Statement true <bool expr> <statement> false
while loop <statement > true <bool expr> false
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’
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[0] s.charAt(0) ‘h’ s.charAt(1) ‘e’ s[1] s.charAt(-1) ... s.charAt(11) s.length() 11 1 “ ”.length() “”.length() 0
StringIndexBounds exceptiom Accessing SubString 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)
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;
Scanning • Scanning image for text. • Scanning frequencies for radio stations. • Finding words in a sentence • Finding identifiers, operators, in a program
token token J o h n F . K e n n e d y token token token Scanning Input stream Token Stream
marker 0 Algorithm 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 J o h n F . K e n n e d y marker 2 Output: J
Algorithm J o h n F . K e n n e d y marker 5 Output: JF
Algorithm J o h n F . K e n n e d y marker 6 Output: JF
Algorithm J o h n F . K e n n e d y marker 8 Output: JFK
Algorithm J o h n F . K e n n e d y marker 9 Output: JFK
Algorithm J o h n F . K e n n e d y marker 14 Output: JFK
Solution (edit in class) package warmup; publicclass AnUpperCasePrinter { publicstaticvoid main(String[] args){ } }
Function call Actual parameter Function definition Formal parameter Solution package warmup; public class AnUpperCasePrinter { publicstatic void main(String[] args){ if (args.length != 1) { System.out.println("Illegal number of arguments:" + args.length + ". Terminating program."); System.exit(-1); } System.out.println("Upper Case Letters:"); int index = 0; while (index < args[0].length()) { if (isUpperCase(args[0].charAt(index))) System.out.print(args[0].charAt(index)); index++; } System.out.println(); } publicstaticboolean isUpperCase(char c) { return (c >= 'A') && (c <= 'Z'); } }