CS100J Spring 2001 Prelim 1 Topics Overall Prelim 1 + mostly Java + closed book + about 4 problems + short answer, fill-in-the blank, fill-in-the box + font notation: code elements/statements represented with either boldface, fixed-width font (pretty) or $stuff$ (text) + covers all lectures and lecture notes, readings, exercises, projects Problems + develop algorithm + instructions to someone/something to solve a problem + be detailed and descriptive + break big problems into smaller ones Languages + programming: automating problem solving + building a computer language - characters (ASCII, Unicode) - words (tokens) - sentences (statements) + using grammar (syntax) and meaning (semantics) to build statements + examples of languages: Java, MATLAB (Matrix Laboratory) + Java language elements: characters, white space, comments, identifiers, operators, literals, punctuation, strings keywords, Environments + MATLAB (running an M-File) + Java IDEs (understanding difference between .java and .class) Output + can print directly with $System.out.println(expressionstatement)$ + can add strings and expressions inside $System.out.println$ - strings: "stuff" - adding: literals + Strings (any combination) - leave spaces by adding $" "$ - escape characters: like $\n$, as in $System.out.print("Hello\n");$ prints a new line after printing Hello - avoid word wrap by adding expressions and strings on different lines Input + using TokenReader and/or BufferedReader for input (basically, we give you the code and you use it) + boils down to: SomethingReader in$ = new SomethingReader(Something); int value = in.readInt(); // input integer and store in $value$ double thing = in.readDouble(); // input double and store in $thing$ String stuff = in.readString(); // input String and store in $stuff$ + prompting: - print message to user that tells her/him to enter a number - does NOT include the actual statement for getting input - just the print statement Empty statement + use a sole semicolon ($;$) Block statement + group statements together with ${ s1;s2;...;sn; }$ Expression statements + combine literals and other elements with operators - operators: + (add), - (subtract), * (multiply), / (divide), % (remainder) - literals: integers (whole numbers), doubles (decimal and exponential notation), characters (use ''), booleans ($true$ and $false$) + follow precendence and associativity rules! - add and divide come first for precedence - minus is left associative + avoid confusion using parentheses $()$ + remainder operator (showed up briefly) if (x % 2 == 0) System.out.println("Even!"); else System.out.println("Odd!"); + cast: - syntax: (type) expression - convert a type to another type - usually for doubles and ints System.out.println( (int) 10.2 + 7) outputs 17 Assignment statements + declaring, assigning, using + variables (identifiers) have no default values (yet) + must delcare before use + must put the same type of value inside the variable (Java is STRONGLY TYPED) + use assign operator ($=$) (means "gets") + increment variables: var = var + value + swapping values (something similar shows up with min/max) int a,b,tmp; a = 1; b = 2; tmp = a; a = b; b = tmp; + increment operators ++ and -- - preincrement: a = ++c - postincrement: b = d++ + random numbers with $Math.round$ (well, not actually an operator!) + style: comment major variables Condition/Selection statements + make choices + keywords: $if$, $if$-$else$, $if$-$else if$-$else$ + basic form: $if (c) s$ + $c$ represents a boolean expression - logical values: $true$ and $false$ - relations: <, <=, >, >=, ==, != - logical operators: && (and), || (or), and ! (not) + use sole $if$, as many $else-if$s, and possibly one $else$ + style: indent code underneath the condition - indentation does NOT mean a new statement! - an entire $if$ statement ends at the last $;$ (after the last statement in an $else$ or $else-if$, or last statement in $if$ assuming no $else$ or $else-if$) - avoid $break$ and $System.exit$ - comments for multiple lines of code go above the lines + Java does selection if the $c$ evalutates to $true$ otherwise, Java moves to statement after the $if$ statement + nested selection: use a selection statement within a selection statement + finding min/max values Repetition statements + loops, iteration + keywords: $while$,$for$,$do$-$while$ (for Prelim 1, you may ONLY use $while$) + types: - definite: known stopping point, usually means giving count check in $while$ condition. Examples: echoing, pure repeating, sometimes accumulation - indefinte: unknown when the loop will stop, could go on forever (infinite loop) Examples: accumulation, conditional update + processing input pattern: - initial/preprocess: assign variables look for boundary values/initial conditions use comments to explain what they are "so far" get initial input(s) - looping/processing: start with $while(c)$ $c$ is boolean expression (see selection statement!) in $c$, check for "evil" input and stopping values in loop body, do count and updates, if necessary (updates mean things like swapping variables and checking for min/max values -- they use $if$s) get next input - final/postprocessing: happens after loop use selection statements to report what may have happened in the loop + style - indent underneath condition - avoid $break$ and $System.exit$ - avoid infinite loops: provide stopping conditions - comments for multiple lines of code go above the lines