1 / 39

Comp 401 Introduction

This course comparison explores the differences between Comp.401 and 110, focusing on the topics covered, programming languages used, and level of difficulty. It highlights the relevance for both CS majors and non-majors.

johnathane
Télécharger la présentation

Comp 401 Introduction

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 401Introduction Instructor: PrasunDewan

  2. Comp 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 401 110 CS Majors Psychology, Biology, … Functional, Imperative, … Object-Oriented Java C++, Python, …

  3. Comp 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. 401 110 Intermediate Introductory

  4. Introductory Conventional Programming • Types, variables, assignment , constants, expression • Conditionals, loops. • Input and output • Arrays • Procedures/Functions/Subroutines/Methods • Comments • Program vs. algorithm

  5. Types, Variables, Assignment, Constant, Expressions • Type • Variable • Constant • Named constant • Assignment • Expression • Type rules determine legal and illegal assignments double height = 1.77; booleanoverWeight = false; intHIGH_BMI = 27; Stringname =“joe”; char firstChar= name.charAt(0); intbmi= (int) weight/(height * height); intweight = “seventy”;

  6. Conditionals and Output 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("**************"); }

  7. While Loops and Input • intproduct = 1; • intnextNum = Console.readInt(); • while (nextNum >= 0) { • product = product* nextNum; • nextNum = Console.readInt(); • } • print (product);

  8. For Loops, Arrays and Comments System.out.println("Number of Strings:"); intnumElements = Console.readInt(); // reads the next line as integer System.out.println("Please enter " + numElements + " strings"); String[] strings = new String[numElements]; // dynamic array for (intelementNum = 0; elementNum < numElements; elementNum++) strings[elementNum] = Console.readString(); /* This loop uses the array input ** in the previous loop*/ for ( intelementNum = 0; elementNum < strings.length; elementNum++) System.out.println(strings[elementNum]); String s = strings[0]; // unsafe for (inti=0; i<s.length(); i++) System.out.println(s.charAt(i)); Difference in syntax: arrays built into language, strings are library

  9. Accessing Substrings s.substring(beginIndex, endIndex) “hello world”.substring(4,7)  “o w” “hello world”.substring(4,4)  “” “hello world”.substring(7,4) StringIndexBounds exception

  10. Methods/Procedures/Functions static intf (int n) { intproduct = 1; while (n > 0) { product *= n; n -= 1; } return product; } • Called function • Takes int argument, n, • Returns int 1*2*3*…*n • Calling procedure. • Takes String array argument • Returns nothing – void 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)); } } Static implies non-object oriented programming.

  11. Call Chains R Main method starts the computation, and can call other methods. Q Can put complete program in main method Like having one big paragraph in an essay P Method decomposition important modularization technique even in conventional programming main

  12. Main Method Details Main method has predefined header. R public static void main (String[] args) { …. } Q All methods must be in some “class” (file, which can be in a “package” (directory) P package warmup; publicclassAnArgPrinter { publicstaticvoid main (String[] args) { System.out.println (args[0]); } } main The Java interpreter calls main and provides its user-specified argument. Public means interpreter can access main.

  13. Running Main Class package warmup; publicclassAnArgPrinter { publicstaticvoid main (String[] args) { System.out.println (args[0]); } } Array of user-supplied strings Interpreter Package Class Output User-Supplied Argument

  14. Array Subscript Error package warmup; publicclassAnArgPrinter { publicstaticvoid main (String[] args) { System.out.println (args[0]); } } Subscript Error User-Supplies No Argument

  15. Safe ArgPrinter

  16. Safe ArgPrinter (edit in class) package warmup; publicclassAnArgPrinter { publicstaticvoid main (String[] args) { System.out.println (args[0]); } }

  17. Safe ArgPrinter package warmup; publicclassAnArgPrinter { 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"); } } String concatenation

  18. Running Program in Eclipse

  19. Specifying Main Class

  20. Specifying Argument

  21. Executing Debug

  22. Scanning Problem

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

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

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

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

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

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

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

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

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

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

  33. Solution (edit in class) packagewarmup; publicclassAnUpperCasePrinter { publicstaticvoid main(String[] args){ } }

  34. Solution (edit in class) packagewarmup; publicclassAnUpperCasePrinter { publicstaticvoid 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)); } }

  35. Solution packagewarmup; publicclassAnUpperCasePrinter { 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 (Character.isUpperCase(args[0].charAt(index))) System.out.print(args[0].charAt(index)); index++; } System.out.println(); } Print on new vs previous line

  36. Computer World

  37. Computer vs. Theater

  38. Computer vs. Theater

  39. Beyond Introductory Programming • 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

More Related