4/03 lecture sketch for next lecture + refamiliarize yourself with project solutions + we will convert some to java in lecture + you will convert others for P6 optional reading -- read/skim ahead of time or as we cover the material + JV/LL: skim 1 + JV/LL: read 2, 3.1, 3.2, 3.4, 3.5, 3.6, 3.9 + PL/GG: strongly recommended -- see annotated table of contents topics: matlab versus java + $;$ is statement terminator + $//$ and $/*$-$*/$ for comments + blocks, conditionals, loops + variable declaration + scalar datatypes and values + brief intro/motivation to references (more next time) _______________________________________________________________________________ java differences from matlab + display/print not as convenient: $;$ is statement terminator + compiled, not interactive + $//$ and $/*$-$*/$ for comments + $%$ for remainder + control flow (conditionals, loops): + condition must be parenthesized + bodies are *statement blocks*: sequence of code within braces ${$ and $}$ + no $elseif$ -- use $else if$ + $for$ is rather different -- seen next week or later + more types and more type-checking, e.g. + $int$ and $double$ are different types -- careful about mixing them + loop guard must be $boolean$ + explicit variable declaration, which can be combined with initialization: initialization ___________/\___________ / \ $ variable = $ \______ ______/ V declaration =============================================================================== below, comparisons are done side-by-side in two columns. matlab in left column java in right column _______________________________________________________________________________ comments + $%$ means "ignore rest of line" + $//$ means "ignore rest of line" note: $%$ is an operator in java! + no equivalent + can put a comment within code if surrounded with $/*$ and $*/$ note: cannot nest example: x = x + 1; % increment x x = x + /* this is a multi-line comment :) */ 1 ; // increment x _______________________________________________________________________________ statements + optional semicolon: suppress output + semicolon is statement *terminator* not needed for statement blocks (see below), so not needed for an entire conditional or loop sequencing + one line after the other + one "line" after the other x = 2; x = 2; y = 3; y = 3; z = 4; z = 4; block with new scope + no equivalent + enclose 0 or more statements within braces ${$ and $}$ { x = 3; y = x; } + variable declaration\ covered in + shadowing / later lecture conditionals and loops + conditions do not require parens + conditions must be parenthesized + bodies are not blocks + bodies are blocks -- braces needed + $end$ terminates $else$, loop body + no $end$ if _______________ if ( _______________ ) { +-------------+ +-------------+ | | | | +-------------+ +-------------+ elseif ___________ } else if ( ___________ ) { +-------------+ +-------------+ | | | | +-------------+ +-------------+ elseif ___________ } else if ( ___________ ) { +-------------+ +-------------+ | | | | +-------------+ +-------------+ else } else { +-------------+ +-------------+ | | | | +-------------+ +-------------+ end } while ________ while ( ________ ) { +-------------+ +-------------+ | | | | +-------------+ +-------------+ end } $if$ example: if x > 3 if (x > 3) { y = 2; y = 2; elseif y > 7 & z < 2 } else if (y > 2 & z < 2) { w = 3; w = 3; z = 4; z = 4; else } else { x = x + w; x = x + w; z = w; z = w; end } $while$ example: while x > y while (x > y) { x = x-1; x = x - 1; y = 1.1 * y; y = 1.1 * y; end } $if$-and-$while$ example: given integer variables $n, d, x$, with $n$ positive, set $x$ to the sum of all proper divisors of $n$. matlab: d = 1; x = 0; % sum of proper divisors of $n$ up to, but excluding $d$ while d < n if rem(n, d) == 0 x = x+ d; end d = d + 1; end java: d = 1; x = 0; // sum of proper divisors of $n$ up to, but excluding $d$ while (d < n) { if (n % d == 0) { x = x+ d; } d = d + 1; } _______________________________________________________________________________ scalar variables, datatypes, values + identifiers + pretty much the same + $=$ for assignment + the same + implicit declaration & initialization + explicit declaration & initialization $nan$ no equivalent $d = -2.3e7;$ % $double$ $double d = -2.3e7;$ $i = 7;$ % integer $int i = 7;$ $b = logical(1); % $logical$ $boolean b = true;$ $bb = logical(0); % $logical$ $boolean bb = false;$ $c = 'h';$ % length 1 string $char c = 'h';$ _______________________________________________________________________________ references -- no matlab equivalent + *value* = data that can be stored in a variable + values are "small", "can be written on a scrap of paper" + arrays and objects are "too big" to be values + a *reference* (aka *address* or *pointer*) is "small enough" to be a value + box scope diagram: drawn as an arrow + copying the reference does not copy what it points to! + analogy: + the room $Upson 322$ is too big to fit on a piece of paper + the address $Upson 322$ is small enough to fit on a piece of paper + writing/copying down address $Upson 322$ does not copy/duplicate the room! + operator $new$: create "big" object and return reference to it + value $null$: no reference (kind of like $nan$ in matlab) + two references are *aliases* if they point to the same object + *aliasing* is when two or more references point to the same object example: suppose $x$ is or points to an array and $y = x$ is executed: matlab java +---+---+---+ +---+ +---+---+---+ x | 3 | 7 | 9 | x | *-+---->| 3 | 7 | 9 | +---+---+---+ +---+ +---+---+---+ ^ +---+---+---+ +---+ | y | 3 | 7 | 9 | y | *-+------+ +---+---+---+ +---+ y$ gets a copy of the value in $x$: $y$ gets a copy of the value in $x$: $y$ gets a copy of the array $y$ gets a copy of the *reference* modifying $y(2)$ has no effect on $x$ modifying $y(2)$ also modifies $x(2)$ since $x$ and $y$ point to the same array _______________________________________________________________________________ questions asked in lecture. Q: can you put semicolons in comments? A: yes. Q: how is indentation done for statement blocks? A: the code inside the block should be indented. A: i generally recommend putting the opening brace on the previous line. Q: why isn't there a semicolon for the entire $if$ statement Q: or after each statement block? A: the statement terminator $;$ is needed only to tell java where a statement A: ends. since a statement block clearly indicates its end with the close A: brace $}$, it does not need a semicolon. an $if$ statement is done either A: if there is no $else$ part or when the $else$ part is done: the $else$ A: part is either a statement block or another $if$, and thus does not need A: a semicolon. Q: where is $n$ declared and/or initialized in the $if$-and-$while$ example? A: the assumption is that it is *given* earlier in some code that is not shown. Q: in the $if$ example above, the $else$ is on the same line as closing and Q: opening braces ($} else {$), but in the $if$-and-$while$ example, the Q: statement $d = d+1;$ isn't on the same line as the closing brace -- why not? A: the statement blocks are part of the conditional, so it is fine for the A: opening and closing braces to be on the same line as the $if$ and $else$ A: keywords. however, $d = d+1;$ is not part of the conditional, and thus A: should not be on the same line as any part of the conditional. Q: in the example of a reference, is the pointer to just one element, Q: part of the array, or the entire array? A: in java, a reference always points to an entire array or object A: (or is $null$). it is not possible to point to part of an array or object, A: e.g. just one element within an array.