390 likes | 534 Views
Comp 401 Introduction. Instructor: Prasun Dewan. 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
E N D
Comp 401Introduction Instructor: PrasunDewan
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, …
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
Introductory Conventional Programming • Types, variables, assignment , constants, expression • Conditionals, loops. • Input and output • Arrays • Procedures/Functions/Subroutines/Methods • Comments • Program vs. algorithm
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”;
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("**************"); }
While Loops and Input • intproduct = 1; • intnextNum = Console.readInt(); • while (nextNum >= 0) { • product = product* nextNum; • nextNum = Console.readInt(); • } • print (product);
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
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
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.
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
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.
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
Array Subscript Error package warmup; publicclassAnArgPrinter { publicstaticvoid main (String[] args) { System.out.println (args[0]); } } Subscript Error User-Supplies No Argument
Safe ArgPrinter (edit in class) package warmup; publicclassAnArgPrinter { publicstaticvoid main (String[] args) { System.out.println (args[0]); } }
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
Scanning Problem • Scanning image for text. • Scanning frequencies for radio stations. • Finding words in a sentence • Finding identifiers, operators, in a program
token token J o h n F . K e n n e d y token token token Scanning Input stream Token Stream
marker 0 Algorithm J o h n F . K e n n e d y Output: J
Algorithm String inputLine J o h n F . K e n n e d y marker 1 Output: J
Algorithm J o h n F . K e n n e d y marker 2 Output: J
Algorithm J o h n F . K e n n e d y marker 5 Output: JF
Algorithm J o h n F . K e n n e d y marker 6 Output: JF
Algorithm J o h n F . K e n n e d y marker 8 Output: JFK
Algorithm J o h n F . K e n n e d y marker 9 Output: JFK
Algorithm J o h n F . K e n n e d y marker 14 Output: JFK
Solution (edit in class) packagewarmup; publicclassAnUpperCasePrinter { publicstaticvoid main(String[] args){ } }
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)); } }
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
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