Unformatted text preview:

Slide 1IntroductionComp 401 vs. 110Comp 401 vs. 110Introductory Conventional ProgrammingTypes, Variables, Assignment, Constant, ExpressionsConditionals and OutputSWITCHWhile Loops and InputFor Loops, Arrays and CommentsAccessing SubstringsMethods/Procedures/FunctionsCall ChainsMain Method DetailsRunning Main ClassArray Subscript ErrorSafe Arg PrinterSafe Arg Printer (edit in class)Safe Arg PrinterRunning Program in EclipseSpecifying Main ClassSpecifying ArgumentExecuting DebugScanning ProblemScanning ProblemScanningAlgorithmAlgorithmAlgorithmAlgorithmAlgorithmAlgorithmAlgorithmAlgorithmSolution (edit in class)SolutionDetecting Upper CaseComputer WorldComputer vs. TheaterComputer vs. TheaterBeyond Introductory ProgrammingCOMP 401INTRODUCTIONInstructor: Prasun Dewan2INTRODUCTIONNameYearFrom whereProgramming experienceNon academic titbit3COMP 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 issue401 110CS Majors Psychology, Biology, …Object-OrientedFunctional, Imperative, …JavaC++, Python, …4COMP 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 110Intermediate Introductory5INTRODUCTORY CONVENTIONAL PROGRAMMINGTypes, variables, assignment , constants, expressionConditionals, loops.Input and outputArraysProcedures/Functions/Subroutines/MethodsCommentsProgram vs. algorithm6TYPES, VARIABLES, ASSIGNMENT, CONSTANT, EXPRESSIONSdouble height = 1.77;final 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 assignmentsboolean overWeight = false;7CONDITIONALS 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("**************"); }8SWITCHBreaks out of switch Breaks out of switch switch (c) {case 'a': case 'e': case 'i':case 'o': case 'u': System.out.println("Vowel");break;default: System.out.println("Consonant");}Covers cases not explicitly enumeratedCovers cases not explicitly enumeratedA character is enclosed in single quotesA character is enclosed in single quotes9WHILE LOOPS AND INPUTint product = 1; int nextNum = Console.readInt(); while (nextNum >= 0) { product = product* nextNum; nextNum = Console.readInt(); }print (product);10FOR 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<s.length(); i++)System.out.println(s.charAt(i));Difference in syntax: arrays built into language, strings are libraryDifference in syntax: arrays built into language, strings are library11ACCESSING SUBSTRINGSs.substring(beginIndex, endIndex)StringIndexBounds exceptionStringIndexBounds exception “o w”  “” “hello world”.substring(4,7) “hello world”.substring(4,4) “hello world”.substring(7,4)12METHODS/PROCEDURES/FUNCTIONSstatic 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 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• 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 – voidStatic implies non-object oriented programming.Static implies non-object oriented programming.13CALL CHAINSQ PRmainMain method starts the computation, and can call other methods. Main method starts the computation, and can call other methods. Can put complete program in main methodCan put complete program in main methodLike having one big paragraph in an essayLike having one big paragraph in an essayMethod decomposition important modularization technique even in conventional programmingMethod decomposition important modularization technique even in conventional programming14MAIN METHOD DETAILSQ PRmainMain method has predefined header.Main method has predefined header.All methods must be in some “class” (file, which can be in a “package” (directory)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.The Java interpreter calls main and provides its user-specified argument. Public means interpreter can access main.15RUNNING MAIN CLASSInterpreterInterpreterUser-Supplied ArgumentUser-Supplied ArgumentPackagePackageClassClassOutputOutputpackage warmup;public class AnArgPrinter { public static void main (String[] args) { System.out.println (args[0]); }}Array of user-supplied stringsArray of user-supplied strings16ARRAY SUBSCRIPT ERRORUser-Supplies No ArgumentUser-Supplies No ArgumentSubscript ErrorSubscript Errorpackage warmup;public class AnArgPrinter { public static void main (String[] args) { System.out.println (args[0]); }}17SAFE ARG PRINTER18SAFE ARG PRINTER


View Full Document

UNC-Chapel Hill COMP 114 - Lecture Notes

Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?