Download presentation
Presentation is loading. Please wait.
1
Comp 114 Foundations 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
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 Keyword Predefined Programmer- defined
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'); } Function call Actual parameter Function definition Formal parameter
51
Creating JBuilder Project
55
Adding a Class
58
Editing the Class
59
Saving the Class
60
Running the Class
61
Viewing the Results
62
Running with arguments
68
Setting a break point
69
Running in debugging mode
70
Stopping at breakpoint
71
Examining the call stack
72
Navigating main parameters
73
Stepping to next statement (F8)
74
Stepping to next statement
75
Variable values
76
Stepping Over
77
Stepping Into (F9)
78
Stepping Out
79
Resuming normal execution
80
Program termination
81
Starting Eclipse
82
Specifying Projects Directory
83
No Projects Screen
84
Adding a New Project
85
Selecting Java Project
86
Entering Project Name
87
Switching to Java Perspective
88
Empty Project
89
Adding a Package (Optional Step)
90
Entering Package Name
91
Workspace, Project, Package Folder Worskspace Project Package
92
Adding a New Class File
93
Naming the File.java suffix needed!
94
Empty File
95
Getting Rid of Welcome Pane
96
Editing the File
97
Saving the File
98
Running the Main Method
99
Output in Console Window Console
100
Running Main with Parameters
101
Setting Run Configuration
102
Run Configuration
103
Arguments Tab
104
Executing Debug
105
Setting a Break Point Double click where you want program to stop
106
Executing Debug
107
Starting the Debugger
108
Stopping at Breakpoint Debug Perspective Next statement to be executed
109
Examining Main Parameters Main Parameters
110
Stepping to next statement
111
Next Statement
112
New Output and Local Variable Index gets initialized println() executed
113
Step Over
114
Step Into Call stack Called method parameters
115
Step Return
116
Resume
117
Returning to Java Perspective
118
AReverseUpperCasePrinter
119
No bold face! No Outline!
120
Deleting File Will also delete from file system
121
Renaming
122
Adding.java suffix
123
Renamed File No bold face! No Outline!
124
Downloading ObjectEditor Software
127
Installing ObjectEditor Software
128
Must do this for each project!
129
Installing ObjectEditor Software
136
Testing ObjectEditor Software Any object instance!
137
Testing ObjectEditor Graphics
139
Adding a library in JBuilder
146
Location of All Libraries http://www.cs.unc.edu/~dewan/oe Use Internet explorer (not Netscape) to download files Latest library names –oe8.jar –shapes3.jar oe8.jar version 8 of oe.jar Sotware may have lots of bugs! Send me mail for workarounds bugs.
147
The end The rest are extra slides
154
Previous Main.class shown
158
Running Another Program with Parameters
166
Resume
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.