comp 401 introduction

39
COMP 401 INTRODUCTION Instructor: Prasun Dewan

Upload: virote

Post on 22-Feb-2016

46 views

Category:

Documents


0 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Slide 1

Comp 401IntroductionInstructor: Prasun Dewan#Comp 401 vs. 110Majors 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 JavaLanguage is not the issue

401110CS MajorsPsychology, Biology, Object-OrientedFunctional, Imperative, JavaC++, Python, #2Comp 401 vs. 110Intermediate vs. introductory programmingIntroductory may be object-orientedIntroductory may be conventionalIntroductory 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.

401110IntermediateIntroductory#3Introductory Conventional ProgrammingTypes, variables, assignment , constants, expressionConditionals, loops.Input and outputArraysProcedures/Functions/Subroutines/MethodsCommentsProgram vs. algorithm

#4Types, Variables, Assignment, Constant, Expressionsdouble 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 VariableConstantNamed constantAssignmentExpressionType rules determine legal and illegal assignments

boolean overWeight = false;#5Conditionals and Outputif (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 Inputint product = 1; int nextNum = Console.readInt(); while (nextNum >= 0) { product = product* nextNum; nextNum = Console.readInt(); }print (product);

#For Loops, Arrays and CommentsSystem.out.println("Number of Strings:");int numElements = Console.readInt(); // reads the next line as integerSystem.out.println("Please enter " + numElements + " strings");String[] strings = new String[numElements]; // dynamic arrayfor (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]; // unsafefor (int i=0; i 0) {product *= n;n -= 1;}return product; }public static void main (String[] args) {while (true) { // loop condition never falseint 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 Calling procedure. Takes String array argument Returns nothing voidStatic implies non-object oriented programming.#Call ChainsQ PRmainMain method starts the computation, and can call other methods. Can put complete program in main methodLike having one big paragraph in an essayMethod decomposition important modularization technique even in conventional programming#Main Method DetailsQ PRmainMain 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.#Running Main Class

InterpreterUser-Supplied ArgumentPackageClassOutputpackage warmup;public class AnArgPrinter { public static void main (String[] args) { System.out.println (args[0]); }}

Array of user-supplied strings#13Array Subscript Error

User-Supplies No ArgumentSubscript Errorpackage warmup;public class AnArgPrinter { public static void main (String[] args) { System.out.println (args[0]); }}

#14Safe Arg Printer

#15Safe Arg Printer (edit in class)

package warmup;public class AnArgPrinter { public static void main (String[] args) { System.out.println (args[0]); }}

#16Safe Arg Printer

package warmup;public class AnArgPrinter { public static void main (String[] args) { if (args.length == 1)System.out.println (args[0]); elseSystem.out.println("Illegal no of arguments:" + args.length + ". Terminating program"); }}

String concatenation

#17Running Program in Eclipse

#18Specifying Main Class

#19Specifying Argument

#20Executing Debug

#21Scanning Problem

#22Scanning Problem Scanning image for text.Scanning frequencies for radio stations.Finding words in a sentenceFinding identifiers, operators, in a program

#ScanningJohnF.KenndyetokentokenInputstreamTokenStreamtokentokentoken

#24AlgorithmJohnF.Kenndyemarker0Output: J

#25AlgorithmJohnF.Kenndyemarker1Output: JString inputLine

#26AlgorithmJohnF.Kenndyemarker2Output: J

#27AlgorithmJohnF.Kenndyemarker5Output: JF

#28AlgorithmJohnF.Kenndyemarker6Output: JF

#29AlgorithmJohnF.Kenndyemarker8Output: JFK

#30AlgorithmJohnF.Kenndyemarker9Output: JFK

#31AlgorithmJohnF.Kenndyemarker14Output: JFK

#32Solution (edit in class)package warmup;public class AnUpperCasePrinter { public static void main(String[] args){ } }

#33Solution (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));

} }

#34Solutionpackage 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

#35Computer WorldHardwareMemoryOperating SystemMemory PageProgramMemory WordProcessorMemory Address Instruction (e.g. add 2 to 5)Running a ProgramSource CodeInteractive ProgramObject CodeNon-interactive (Batch) ProgramProgramming LanguageProgram argumentsMachine LanguageRuntime ProgrammersEditorLibrary (of Code)Editing ProgramsTranslator (Compilers/Interpreter)Lexical ErrorUsersSyntax Error DisksSemantics ErrorLogic ErrorDebuggingStyle Principles

#Computer vs. TheaterComputerTheaterHardwareTheaterOperating SystemTheater ManagementProgramPerformanceProcessorPerformerInstruction (e.g. add 2 to 5)Performance action (e.g. walk 3 steps.)Source CodeOriginal ScriptObject CodePerformance ScriptProgramming LanguageScript Language (e.g. German)Machine LanguagePerformance Language (e.g. English)ProgrammersScript WritersLibrary (of Code)Reference Material (from Books)Translator (Compilers/Interpreter)Translator (Before/During Performance)UsersAudienceDisksArchival Storage AreasMemoryScript performance notebook accessible to performersHardwareTheater

#Computer vs. TheaterComputerTheaterMemory PageA Notebook PageMemory WordA Notebook LineMemory Address (Page Number, Word Number)Line Identification (Page Number, Line Number)Running a ProgramPerforming a ScriptInteractive ProgramPerformance with audience participationNon-interactive (Batch) ProgramPerformance with no audience participationProgram argumentsSpecial instructions at start of performanceRuntime Stage-HandsEditorTypewriter/WordprocessorEditing ProgramsWriting ScriptsLexical ErrorSpelling ErrorSyntax Error Grammar ErrorSemantics ErrorInconsistencies in ScriptLogic ErrorExecution Error DebuggingStaging trial performancesStyle PrinciplesStyle Principles

#Beyond Introductory ProgrammingComp 110: Creating small simple programsMain and a few classesComp 401: Creating large programsreusability and understandabilityindividual pieces simpleproject helpsComp 410: Programming complex codecomplex popular data structuresnon-trivial efficiency analysis

#