1 / 167

Comp 114 Foundations of Programming

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.

druce
Download Presentation

Comp 114 Foundations of Programming

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. Comp 114Foundations of Programming Instructor: Prasun Dewan

  2. Topics Assumed & Reviewed • Types • int, double, char, String • Variables, constants, expressions • Arrays • Assignment, conditionals, loops • Procedures/Functions/Subroutines/Methods • Parameters/arguments

  3. Hello World

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

  5. user-supplied argument Main Arguments

  6. Second argument First argument Main Arguments package warmup; publicclass AnArgPrinter { publicstaticvoid main (String[] args) { System.out.println (args[0]); } } args[0] args[1] ...

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

  8. Safe Arg Printer

  9. Safe Arg Printer (edit in class) package warmup; publicclass ASafeArgPrinter { publicstaticvoid main (String[] args) { //args.length gives number of elements in args array. }

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

  11. if-else Statement • if (<bool expr>) • <statement 1> • else • <statement 2>

  12. <statement 1> <statement 2> if-else Statement true false <bool expr>

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

  14. 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'; • }

  15. 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'; 

  16. Nested if-else

  17. If Statement • if (args.length = = 1) • System.out.println (”args[0]”); • if (<bool expr>) • <statement>;

  18. Printing Multiple Arguments

  19. Printing Multiple Arguments (edit in class) package warmup; publicclass AnArgsPrinter { publicstaticvoid main(String[] args) { } }

  20. Printing Multiple Arguments package warmup; publicclass AnArgsPrinter { publicstaticvoid main(String[] args) { int argNo = 0; while (argNo < args.length) { System.out.println(args[argNo]); argNo++; } } }

  21. if vs. while Statement • if (<bool expr>) • <statement>; • while (<bool expr>) • <statement>;

  22. if Statement true <bool expr> <statement> false

  23. while Statement true <bool expr> <statement> false

  24. while loop <statement > true <bool expr> false

  25. Scanning Problem

  26. 16 bits char Escape sequence newline char Constants {letters, digits, operators ...} ‘\n’ ‘a’ ‘1’ ‘’’ ‘\’ ‘ ‘ ‘’ ‘\’’ ‘A‘ ‘< ‘ ‘ ‘\\’

  27. Useful Escape Sequences

  28. ordinal number (integer code) Ordering Characters ‘’ …. ‘a’ …. position in ordered character list

  29. ‘’ …. ‘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

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

  31. variable size Object Type String constants String {sequences of characters} “hello\n\n123” “hello” “hello 123” ‘a’ “” “123” “a” “\” “\\”

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

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

  34. String Processing • int i = 0; • while (i < s.length()) { • System.out.println (s.charAt(i)); • i++; • } prints each character on separate line

  35. loop condition loop body Dissecting a Loop • int i = 0; • while (i < s.length()) { • System.out.println (s.charAt(i)); • i++; • }

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

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

  38. Scanning Problem

  39. Scanning • Scanning image for text. • Scanning frequencies for radio stations. • Finding words in a sentence • Finding identifiers, operators, in a program

  40. token token J o h n F . K e n n e d y token token token Scanning Input stream Token Stream

  41. marker 0 Algorithm J o h n F . K e n n e d y Output: J

  42. Algorithm String inputLine J o h n F . K e n n e d y marker 1 Output: J

  43. Algorithm J o h n F . K e n n e d y marker 2 Output: J

  44. Algorithm J o h n F . K e n n e d y marker 5 Output: JF

  45. Algorithm J o h n F . K e n n e d y marker 6 Output: JF

  46. Algorithm J o h n F . K e n n e d y marker 8 Output: JFK

  47. Algorithm J o h n F . K e n n e d y marker 9 Output: JFK

  48. Algorithm J o h n F . K e n n e d y marker 14 Output: JFK

  49. Solution (edit in class) package warmup; publicclass AnUpperCasePrinter { publicstaticvoid main(String[] args){ } }

  50. 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'); } }

More Related