c omp 401 i ntroduction instructor: prasun dewan

39
COMP 401 INTRODUCTION Instructor: Prasun Dewan

Post on 19-Dec-2015

223 views

Category:

Documents


2 download

TRANSCRIPT

  • Slide 1
  • Slide 2
  • C OMP 401 I NTRODUCTION Instructor: Prasun Dewan
  • Slide 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,
  • Slide 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
  • Slide 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
  • Slide 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;
  • Slide 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("**************"); }
  • Slide 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);
  • Slide 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
  • 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.
  • Slide 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
  • Slide 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.
  • Slide 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
  • Slide 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]); }
  • Slide 16
  • 15 S AFE A RG P RINTER
  • Slide 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]); }
  • Slide 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
  • Slide 19
  • 18 R UNNING P ROGRAM IN E CLIPSE
  • Slide 20
  • 19 S PECIFYING M AIN C LASS
  • Slide 21
  • 20 S PECIFYING A RGUMENT
  • Slide 22
  • 21 E XECUTING D EBUG
  • Slide 23
  • 22 S CANNING P ROBLEM
  • Slide 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
  • Slide 25
  • 24 S CANNING JohnF.Kenndye token Input stream Token Stream token
  • Slide 26
  • 25 A LGORITHM JohnF.Kenndye marker 0 Output: J
  • Slide 27
  • 26 A LGORITHM JohnF.Kenndye marker 1 Output: J String inputLine
  • Slide 28
  • 27 A LGORITHM JohnF.Kenndye marker 2 Output: J
  • Slide 29
  • 28 A LGORITHM JohnF.Kenndye marker 5 Output: JF
  • Slide 30
  • 29 A LGORITHM JohnF.Kenndye marker 6 Output: JF
  • Slide 31
  • 30 A LGORITHM JohnF.Kenndye marker 8 Output: JFK
  • Slide 32
  • 31 A LGORITHM JohnF.Kenndye marker 9 Output: JFK
  • Slide 33
  • 32 A LGORITHM JohnF.Kenndye marker 14 Output: JFK
  • Slide 34
  • 33 S OLUTION ( EDIT IN CLASS ) package warmup; public class AnUpperCasePrinter { public static void main(String[] args){ }
  • Slide 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)); }
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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