CS 100, Summer 2001 Tuesday, 7/3 Lecture 6 ----------------------------------------------------------------------- Announcements: + Hand in Exercise 4 + Assignment 2 due Friday (7/6) + PRELIM 1 on Monday (7/9) + Change in consultant and office hours this week (Weds to Thurs) + reading: Savitch 5.1, 5.2, skim 5.3 ----------------------------------------------------------------------- - Please submit lecture notes for last Friday's review session. - Learn by doing! Use the computer to experiment and test your programming skills. ----------------------------------------------------------------------- Topics: + do-while loops + for loops + Matlab + methods ----------------------------------------------------------------------- ----------------------------------------------------------------------- + do-while - back to Space Invaders (demo) EXAMPLE: read in Space Invaders scores, output avg, min, max - see SpaceInvadersWhile.java + convert to a do-while loop - see SpaceInvadersDoWhile.java - this is good if you know you're going to do something at least once - BUT note the changes we need to make sure -1 isn't treated as a real score + what if negative values are allowed as well? - no useful sentinel value - ask user how many scores they'll be entering, then count how many you see and stop when you've gotten all of them ----------------------------------------------------------------------- More examples of loops: - MirrorNumberWhile.java ----------------------------------------------------------------------- ----------------------------------------------------------------------- for loops + for (init ; condition ; update) // no semicolon! + same as: init; while (condition) { do stuff ; update; } EXAMPLE: convert MirrorNumberWhile.java to use a for loop: - MirrorNumberFor.java EXAMPLE: Exercise 4 ----------------------------------------------------------------------- ----------------------------------------------------------------------- MATLAB + use 'help' to explore what you can do + a lot of information was also contained in the reading assignment + try 'plot' + use 'bar' for Assignment 2 + to load in a text file: var = load('filename'); + If you omit a semicolon at the end of a line, it shows you the value of whatever was computed; if you put the semicolon, it suppresses the output. Both are useful. ----------------------------------------------------------------------- ----------------------------------------------------------------------- + Methods - think function: methodName(inputs) -> generates output ---------------------------------------------------------------------- Examples in Java: Math.random() System.out.println(_stuff_); System.out.print(_stuff_); main(String[] args) + interesting features: - not all have inputs - some don't actually give a value (just perform an action) + 2 general kinds: - performs action without returning a value - returns value (and maybe also performs action) + more tomorrow! ---------------------------------------------------------------------- PICK UP Exercise 4 (graded) Happy Fourth of July!