Download presentation
Presentation is loading. Please wait.
1
Comp 114 Foundations of Programming Instructor: Prasun Dewan (Pr sün Divän)
2
Hello World
3
Topics Assumed & Reviewed Types –int, double, char, String Variables, constants, expressions Arrays Assignment, conditionals, loops Procedures/Functions/Subroutines/Methods Parameters/arguments
4
Hello World package warmup; public class AHelloWorldGreeter { public static void main (String[] args) { System.out.println ("Hello World"); } Array of user-supplied arguments main header directory/library
5
Main Arguments user-supplied argument
6
Main Arguments package warmup; public class AnArgPrinter { public static void main (String[] args) { System.out.println (args[0]); } First argument args[0] args[1] Second argument...
7
Main Arguments user- supples no argument package warmup; public class AnArgPrinter { public static void main (String[] args) { System.out.println (args[0]); } program refers to argument array element exception!
8
Safe Arg Printer
9
Safe Arg Printer (edit in class) package warmup; public class ASafeArgPrinter { public static void main (String[] args) { //args.length gives number of elements in args array. }
10
Safe Arg Printer package warmup; public class ASafeArgPrinter { public static void 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 ( ) else
12
If-Else Statement true false
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 public static char toLetterGrade (int score) { 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'; }
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 ( ) ;
18
Printing Multiple Arguments
19
Printing Multiple Arguments (edit in class) package warmup; public class AnArgsPrinter { public static void main(String[] args){ }
20
Printing Multiple Arguments package warmup; public class AnArgsPrinter { public static void main(String[] args){ int argNo = 0; while (argNo < args.length) { System.out.println(args[argNo]); argNo++; }
21
If Vs While Statement if ( ) ; while ( ) ;
22
if Statement true false
23
while Statement true false
24
while loop true false
25
Scanning Problem
26
char Constants char {letters, digits, operators...} ‘a’ ‘A‘ ‘1’ ‘< ‘ ‘’ 16 bits ‘ ‘’’ ‘\’’ Escape sequence ‘\n’ newline ‘ ‘\’ ‘\\’
27
Useful Escape Sequences
28
Ordering Characters ‘’‘a’…. position in ordered character list ordinal number (integer code) ….
29
Ordering Characters ‘’‘a’…. ‘b’‘c’‘z’…. ‘’‘A’…. ‘B’‘C’‘Z’…. ‘’‘0’…. ‘1’‘2’‘3’…. ‘a’ > ‘b’ false ‘B’ > ‘A’ true ‘4’ > ‘0’ true ‘0’ > ‘’ true ‘a’ > ‘A’ ??? ‘a’ > ‘0’ ???
30
Converting between Characters and their Ordinal Numbers (int) ‘a’ ordinal number of ’a’ (char) 55 character whose ordinal number is 55 (int) ‘’ 0 0 (char) 0 ‘’ (int) ‘d’ ??? (char) 1 ??? (char) -1 (int) ‘c’ - (int) ‘a’ 2 2 ‘c’ - ‘a’ 2 2 Implicit cast to wider type (char) (‘c’ - 2) ‘a’ (char) (‘A’ + 2) ‘C’ (char) (‘C’ - ‘A’ + ‘a’) ‘c’
31
String constants String{sequences of characters} “hello” “123” “hello 123” “a” variable size ‘a’ “” “hello\n\n123” “\”“\\” Object Type
32
Accessing String Components String s = “hello world”; s[0] s[1]... s.charAt(0) ‘h’ s.charAt(1) ‘e’ s.charAt(-1) s.charAt(11) StringIndexBounds exceptiom index s.length() 11 “ ”.length() 1 1 0 0
33
Accessing SubString “hello world”.substring(4,7) s.charAt(beginIndex).. s.charAt(endIndex-1) s.substring(beginIndex, endIndex) “o w” “hello world”.substring(4,4) “” “hello world”.substring(7,4) StringIndexBounds exceptiom
34
String Processing int i = 0; while (i < s.length()) { System.out.println (s.charAt(i)); i++; } prints each character on separate line
35
Dissecting a Loop int i = 0; while (i < s.length()) { System.out.println (s.charAt(i)); i++; } loop condition loop body
36
Finer-grained Dissection int i = 0; while (i < s.length()) { System.out.println (s.charAt(i)); i++; } loop condition real body Resetting loop variable initalizing loop variables for (int i=0; i<s.length(); i++) System.out.println(s.charAt(i));
37
Meaning of For Loop S1; while ( E) { S3; S2; } for (S1; E; S2) S3 for (; E; S2) S3 while ( E) { S3; S2; } for (; E; ) S3 while ( E) { S3; } for (; ; ) 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
Scanning JohnF.Kenndye token Input stream Token Stream token
41
Algorithm JohnF.Kenndye marker 0 Output: J
42
Algorithm JohnF.Kenndye marker 1 Output: J String inputLine
43
Algorithm JohnF.Kenndye marker 2 Output: J
44
Algorithm JohnF.Kenndye marker 5 Output: JF
45
Algorithm JohnF.Kenndye marker 6 Output: JF
46
Algorithm JohnF.Kenndye marker 8 Output: JFK
47
Algorithm JohnF.Kenndye marker 9 Output: JFK
48
Algorithm JohnF.Kenndye marker 14 Output: JFK
49
Solution (edit in class) package warmup; public class AnUpperCasePrinter { public static void main(String[] args){ }
50
Solution package warmup; public class AnUpperCasePrinter { public static 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(); } public static boolean isUpperCase(char c) { return (c >= 'A') && (c <= 'Z'); }
51
Printing Scanned Characters Backwards
52
Strings = Char Sequences JohnF.Kenndye hello String{sequences of characters} 143l0
53
Other Sequences as Predefined Types 100989910090 604050 {sequences of integers} 80 JFKFDR {sequence of Strings} JCBCRRGB 3.83.1 {sequences of doubles} 3.73.13.63.9 IntSequence DoubleSequence StringSequence
54
Other Sequences as Array Types 100989910090 604050 {sequences of integers} 80 {sequence of strings} 3.83.1 {sequences of doubles} 3.73.13.63.9 int[] double[] String[] JFKFDRJCBCRRGB
55
Initializing Array Declarations 10098991009080 int[] assignmentScores = {100, 98, 99, 100, 90 80}; 3.83.13.73.13.63.9 double[] gpas = {3.8, 3.1, 3.7, 3.1, 3.6, 3.9};String[] initials = {“JFK”, “FDR”, “JC”, “BC”, “GW”, “WW”}; JFKFDRJCBCGWWW assignmentScores Array Type Element Type Array Literal Array Variable
56
Array Operations String[] initials = {“JFK”, “FDR”, “JC”, “BC”, “GW”, “WW”}; JFKFDRJCBCGWWW initials.length 6 initials[0] initials[initials.length - 1] initials[initials.length] ArrayIndexOutOfBoundsException public named constant initials[0] = “HT” HT initials[initials.length] = “HT” ArrayIndexOutOfBoundsException Array Instance Size Fixed
57
Array Types have Variable-Size 100989910090 604050 80 int[] 10098991009080 int[] assignmentScores = {100, 98, 99, 100, 99, 80}; assignmentScores assignmentScores = {60, 40, 50}; assignmentScores 604050
58
Uninitializing Array Declaration int[] assignmentScores; assignmentScores null assignmentScores = {60, 40, 50}; assignmentScores 604050
59
Array Elements Uninitialized int[] assignmentScores = new int[3]; assignmentScores 000
60
Printing Scanned Characters Backwards
61
Variable-Size Collection filled part unfilled part current size maximum size
62
3 J F K sizearray Variable-Size Collection filled part unfilled part current size maximum size
63
Variable-Size Collection static final int MAX_CHARS = 7; static char[] upperCaseLetters = new char[MAX_CHARS]; static int numberOfUpperCaseLetters = 0;
64
Solution package warmup; public class AReverseUpperCasePrinter { static final int MAX_CHARS = 5; static char[] upperCaseLetters = new char[MAX_CHARS]; static int numberOfUpperCaseLetters = 0; public static void main(String[] args){ if (args.length != 1) { System.out.println("Illegal number of arguments:" + args.length + ". Terminating program."); System.exit(-1); } int index = 0; System.out.println("Upper Case Letters:"); while (index < args[0].length()) { if (isUpperCase(args[0].charAt(index))) { System.out.print(args[0].charAt(index)); storeChar(args[0].charAt(index)); } index++; } System.out.println(); printReverse(); }
65
Solution (contd.) public static void storeChar(char c) { if (numberOfUpperCaseLetters == MAX_CHARS) { System.out.println("Too many upper case letters. Terminating program. "); System.exit(-1); } upperCaseLetters[numberOfUpperCaseLetters] = c; numberOfUpperCaseLetters++; } public static void printReverse() { System.out.println("Upper Case Letters in Reverse:"); for (int index =numberOfUpperCaseLetters - 1; index >= 0; index--) { System.out.print(upperCaseLetters[index]); }
66
Creating JBuilder Project
70
Adding a Class
73
Editing the Class
74
Saving the Class
75
Running the Class
76
Viewing the Results
77
Running with arguments
83
Setting a break point
84
Running in debugging mode
85
Stopping at breakpoint
86
Examining the call stack
87
Navigating main parameters
88
Stepping to next statement (F8)
89
Stepping to next statement
90
Variable values
91
Stepping Over
92
Stepping Into (F9)
93
Stepping Out
94
Resuming normal execution
95
Program termination
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.