Download presentation
Presentation is loading. Please wait.
2
C OMP 401 I NTRODUCTION Instructor: Prasun Dewan
3
2 C OMP 401 VS. 110 Majors vs. Non Majors? Majors usually start with 401 But many 110 students become majors. Object-oriented vs. Conventional? Both 401 and (current) 110 focus on objects. Java vs. Non-Java? 110 and 401 are both in Java Language is not the issue 401110 CS MajorsPsychology, Biology, … Object-Oriented Functional, Imperative, … Java C++, Python, …
4
3 C OMP 401 VS. 110 “Intermediate” vs. “introductory” programming Introductory may be object- oriented Introductory may be conventional “Introductory” material must have few language and course dependencies Assume background in conventional programming and will teach Java syntax for it. Repetition for those who know object-oriented programming. 401110 IntermediateIntroductory
5
4 I NTRODUCTORY C ONVENTIONAL P ROGRAMMING Types, variables, assignment, constants, expression Conditionals, loops. Input and output Arrays Procedures/Functions/Subroutines/Methods Comments Program vs. algorithm
6
5 T YPES, V ARIABLES, A SSIGNMENT, C ONSTANT, E XPRESSIONS double height = 1.77; int HIGH_BMI = 27; String name =“joe”; char firstChar= name.charAt(0); int bmi= ( int ) weight/(height * height); int weight = “seventy”; Type Variable Constant Named constant Assignment Expression Type rules determine legal and illegal assignments boolean overWeight = false;
7
6 C ONDITIONALS AND O UTPUT if (score < PASS_CUTOFF) { System.out.println("**************"); System.out.println("FAILED"); System.out.println("**************"); } else { System.out.println("**************"); System.out.println("PASSED"); System.out.println("Congratulations!"); System.out.println("**************"); }
8
7 W HILE L OOPS AND I NPUT int product = 1; int nextNum = Console.readInt(); while (nextNum >= 0) { product = product* nextNum; nextNum = Console.readInt(); } print (product);
9
8 F OR L OOPS, A RRAYS AND C OMMENTS System.out.println("Number of Strings:"); int numElements = Console.readInt(); // reads the next line as integer System.out.println("Please enter " + numElements + " strings"); String[] strings = new String[numElements]; // dynamic array for ( int elementNum = 0; elementNum < numElements; elementNum++) strings[elementNum] = Console.readString(); /* This loop uses the array input ** in the previous loop*/ for ( int elementNum = 0; elementNum < strings.length; elementNum++) System.out.println(strings[elementNum]); String s = strings[0]; // unsafe for (int i=0; i<s.length(); i++) System.out.println(s.charAt(i)); Difference in syntax: arrays built into language, strings are library
10
9 A CCESSING S UBSTRINGS s.substring(beginIndex, endIndex) StringIndexBounds exception “o w” “” “hello world”.substring(4,7) “hello world”.substring(4,4) “hello world”.substring(7,4)
11
10 M ETHODS /P ROCEDURES /F UNCTIONS static int f ( int n) { int product = 1; while (n > 0) { product *= n; n -= 1; } return product; } public static void main (String[] args) { while ( true ) { // loop condition never false int n = Console.readInt(); if (n < 0) break; System.out.println("factorial = " + f(n)); } 1*2*3*…*n Called function Takes int argument, n, Returns int Called function Takes int argument, n, Returns int Calling procedure. Takes String array argument Returns nothing – void Calling procedure. Takes String array argument Returns nothing – void Static implies non-object oriented programming.
12
11 C ALL C HAINS Q P R main Main method starts the computation, and can call other methods. Can put complete program in main method Like having one big paragraph in an essay Method decomposition important modularization technique even in conventional programming
13
12 M AIN M ETHOD D ETAILS Q P R main Main method has predefined header. All methods must be in some “class” (file, which can be in a “package” (directory) public static void main (String[] args) { …. } package warmup; public class AnArgPrinter { public static void main (String[] args) { System.out.println (args[0]); } The Java interpreter calls main and provides its user-specified argument. Public means interpreter can access main.
14
13 R UNNING M AIN C LASS Interpreter User-Supplied Argument Package Class Output package warmup; public class AnArgPrinter { public static void main (String[] args) { System.out.println (args[0]); } Array of user- supplied strings
15
14 A RRAY S UBSCRIPT E RROR User-Supplies No Argument Subscript Error package warmup; public class AnArgPrinter { public static void main (String[] args) { System.out.println (args[0]); }
16
15 S AFE A RG P RINTER
17
16 S AFE A RG P RINTER ( EDIT IN CLASS ) package warmup; public class AnArgPrinter { public static void main (String[] args) { System.out.println (args[0]); }
18
17 S AFE A RG P RINTER package warmup; public class AnArgPrinter { 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"); } String concatenation
19
18 R UNNING P ROGRAM IN E CLIPSE
20
19 S PECIFYING M AIN C LASS
21
20 S PECIFYING A RGUMENT
22
21 E XECUTING D EBUG
23
22 S CANNING P ROBLEM
24
23 S CANNING P ROBLEM Scanning image for text. Scanning frequencies for radio stations. Finding words in a sentence Finding identifiers, operators, in a program
25
24 S CANNING JohnF.Kenndye token Input stream Token Stream token
26
25 A LGORITHM JohnF.Kenndye marker 0 Output: J
27
26 A LGORITHM JohnF.Kenndye marker 1 Output: J String inputLine
28
27 A LGORITHM JohnF.Kenndye marker 2 Output: J
29
28 A LGORITHM JohnF.Kenndye marker 5 Output: JF
30
29 A LGORITHM JohnF.Kenndye marker 6 Output: JF
31
30 A LGORITHM JohnF.Kenndye marker 8 Output: JFK
32
31 A LGORITHM JohnF.Kenndye marker 9 Output: JFK
33
32 A LGORITHM JohnF.Kenndye marker 14 Output: JFK
34
33 S OLUTION ( EDIT IN CLASS ) package warmup; public class AnUpperCasePrinter { public static void main(String[] args){ }
35
34 S OLUTION ( EDIT IN CLASS ) package warmup; public class AnUpperCasePrinter { public static void main(String[] args){ if (args.length != 1) System.esit(0); for (int index = 0; index < args.length; index++) if (Character.isUpperCase(args[0].charAt(index)) System.out.print(args[0].charAt(index)); }
36
35 S OLUTION 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 (Character.isUpperCase(args[0].charAt(index))) System.out.print(args[0].charAt(index)); index++; } System.out.println(); } Print on new vs previous line
37
36 C OMPUTER W ORLD HardwareMemory Operating SystemMemory Page ProgramMemory Word ProcessorMemory Address Instruction (e.g. add 2 to 5)Running a Program Source CodeInteractive Program Object CodeNon-interactive (Batch) Program Programming LanguageProgram arguments Machine LanguageRuntime ProgrammersEditor Library (of Code)Editing Programs Translator (Compilers/Interpreter)Lexical Error UsersSyntax Error DisksSemantics Error Logic Error Debugging Style Principles
38
37 C OMPUTER VS. T HEATER ComputerTheater HardwareTheater Operating SystemTheater Management ProgramPerformance ProcessorPerformer Instruction (e.g. add 2 to 5)Performance action (e.g. walk 3 steps.) Source CodeOriginal Script Object CodePerformance Script Programming LanguageScript Language (e.g. German) Machine LanguagePerformance Language (e.g. English) ProgrammersScript Writers Library (of Code)Reference Material (from Books) Translator (Compilers/Interpreter)Translator (Before/During Performance) UsersAudience DisksArchival Storage Areas MemoryScript performance notebook accessible to performers HardwareTheater
39
38 C OMPUTER VS. T HEATER ComputerTheater Memory PageA Notebook Page Memory WordA Notebook Line Memory Address (Page Number, Word Number) Line Identification (Page Number, Line Number) Running a ProgramPerforming a Script Interactive ProgramPerformance with audience participation Non-interactive (Batch) ProgramPerformance with no audience participation Program argumentsSpecial instructions at start of performance RuntimeStage-Hands EditorTypewriter/Wordprocessor Editing ProgramsWriting Scripts Lexical ErrorSpelling Error Syntax ErrorGrammar Error Semantics ErrorInconsistencies in Script Logic ErrorExecution Error DebuggingStaging trial performances Style Principles
40
39 B EYOND I NTRODUCTORY P ROGRAMMING Comp 110: Creating small simple programs Main and a few classes Comp 401: Creating large programs reusability and understandability individual pieces simple project helps Comp 410: Programming complex code complex popular data structures non-trivial efficiency analysis
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.