Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Review COMP 102 #
© Peter Andreae COMP :2 Menu Running Java programs without BlueJ main, and other static methods. Designing conditionals: Rock Paper Scissors Designing loops: Temperature Monitor Administrivia: Mandatory requirements and Makeup assignment Class Reps meeting. Using the web page resources
© Peter Andreae COMP :3 Where have we been: Elements of Java classes and methods statements expressions, arithmetic, variables and assignments calling methods, passing arguments, using returned values defining methods, parameters, return values types int, double, String, long, boolean CartoonFigure, Flower, HouseDrawer, constructing new objects. control structures: if ( … ) … else … while ( … ) … boolean expressions, with &&, ||, ! files, Scanners, Printstreams, try … catch (…)…, exceptions
© Peter Andreae COMP :4 Where have we been Programming and Design techniques specification, design/algorithm, code, debugging design using precise English in comments coding using syntax rules (railway diagrams) using documentation debugging compiler identifies syntax errors testing identifies logic errors. indenting for readability Wishful programming – top-down design wishing: make up a method name, use it, work out arguments magic wand: define the method (name, parameters, return type)
© Peter Andreae COMP :5 Rock Paper Scissors PlayRound work out computer’s choice use Math.random() < 0.33, <.667 < 1.0 ask for player’s choice work out win/draw/lose display/report Design questions: How do you represent the choices? How do you work out win/draw/lose?
© Peter Andreae COMP :6 Rock Paper Scissors Computer as number, player as string: int comp = (int) (Math.random()*3); // 0 (rock), 1 (paper), 2 (scissors) String player = UI.askToken("Your choice: ").toLowerCase(); Win/draw/lose: if ( player.equals("rock") ) { if ( comp==0 ) { UI.drawString(x, y, "draw"); return 0; } else if ( comp==1){ UI.drawString(x, y, "lose"); return -1; } else { UI.drawString(x, y, "win"); return 1; } } else if ( player.equals("paper") ) { if ( comp==0 ) { UI.drawString(x, y, "win"); return 1; } else if ( comp==1){ UI.drawString(x, y, "draw"); return 0; } else { UI.drawString(x, y, "lose"); return -1; } } else { if ( comp==0 ) { UI.drawString(x, y, "lose"); return -1; } else if ( comp==1){ UI.drawString(x, y, "win"); return 1; } else { UI.drawString(x, y, "draw"); return 0; } }
© Peter Andreae COMP :7 Rock Paper Scissors Both choices as strings: String comp = "rock"; double rand = Math.random(); if ( rand < 1/3.0 ) { comp = "paper"; } else if ( rand > 2/3.0) { comp = "scissors"; } String player = UI.askToken("Your choice: ").toLowerCase();
© Peter Andreae COMP :8 Rock Paper Scissors Win/draw/lose: (strings) if ( player.equals(comp) ) { UI.drawString(x, y, "draw"); return 0; } else if ( (player.equals("paper") && comp.equals("rock") ) || (player.equals("rock") && comp.equals("scissors") ) || (player.equals("scissors") && comp.equals("paper") ) { UI.drawString(x, y, "win"); return 1; } else { UI.drawString(x, y, "lose"); return -1; } Easier to compare, Easier to read.
© Peter Andreae COMP :9 Rock Paper Scissors Both choices as numbers: int comp = (int) (Math.random()*3); // 0 (rock), 1 (paper), 2 (scissors) String ans = UI.askToken("Your choice: ").toLowerCase(); int player = 0; if ( ans.equals("paper") ) { player = 1; } else if ( ans.equals("scissors") { player = 2; }
© Peter Andreae COMP :10 Rock Paper Scissors Win/draw/lose: (numbers) if ( player == comp ) { UI.drawString(x, y, "draw"); return 0; } else if ( player == (comp+1)%3) { UI.drawString(x, y, "win"); return 1; } else { UI.drawString(x, y, "lose"); return -1; } Easiest to compare, Not so easy to read and understand Thinking carefully about representation helps
© Peter Andreae COMP :11 Temperature Monitor. Read a sequence of numbers from user and analyse them double max = -Double.MAX_VALUE; or Double.NEGATIVE_INFINITY double min = Double.MAX_VALUE; or Double.POSITIVE_INFINITY double sum = 0.0; int count = 0; UI.print("Enter temperatures; end with 'done': "); while (UI.hasNextDouble()) { double value = UI.nextDouble(); count = count + 1; sum = sum + value; if (value > max) {max = value;} if (value < min) {min = value;} } double mean = sum / count;
© Peter Andreae COMP :12 Temperature Monitor. Keeping the first and last? int count = 0; UI.print("Enter temperatures; end with 'done': "); while (UI.hasNextDouble()) { double value = UI.nextDouble(); count = count + 1; if (count == 1) {double first = value;} } UI.println(“first is ” + first); UI.println(“last is ” + value); first and last are not visible (“out of scope”)
© Peter Andreae COMP :13 Temperature Monitor. Keeping the first and last? double first = ??; double value = ??; int count = 0; UI.print("Enter temperatures; end with 'done': "); while (UI.hasNextDouble()) { value = UI.nextDouble(); count = count + 1; if (count == 1) {first = value;} } UI.println(“first is ” + first); UI.println(“last is ” + value); first and last are not visible (“out of scope”)
© Peter Andreae COMP :14 Temperature Monitor Finding the largest difference Need to see it how the computer sees it - one number at a time Need to remember the previous number as we go.
© Peter Andreae COMP :15 Temperature Monitor. Finding the largest difference? double maxDiff = 0; double previous = 0; UI.print("Enter temperatures; end with 'done': "); while (UI.hasNextDouble()) { value = UI.nextDouble(); if (count > 1 && Math.abs(value-previous) > maxDiff) { maxDiff = Math.abs(value-previous); } previous = value; }