Presentation is loading. Please wait.

Presentation is loading. Please wait.

Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Review COMP 102 #15 2013.

Similar presentations


Presentation on theme: "Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Review COMP 102 #15 2013."— Presentation transcript:

1 Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Review COMP 102 #15 2013

2 © Peter Andreae COMP 102 15: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

3 © Peter Andreae COMP 102 15: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

4 © Peter Andreae COMP 102 15: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)

5 © Peter Andreae COMP 102 15: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?

6 © Peter Andreae COMP 102 15: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; } }

7 © Peter Andreae COMP 102 15: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();

8 © Peter Andreae COMP 102 15: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.

9 © Peter Andreae COMP 102 15: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; }

10 © Peter Andreae COMP 102 15: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

11 © Peter Andreae COMP 102 15: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;

12 © Peter Andreae COMP 102 15: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”)

13 © Peter Andreae COMP 102 15: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”)

14 © Peter Andreae COMP 102 15:14 Temperature Monitor Finding the largest difference. 46 23 17 57 100 33 Need to see it how the computer sees it - one number at a time. 46 23 17 57 100 33 Need to remember the previous number as we go.

15 © Peter Andreae COMP 102 15: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; }


Download ppt "Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Review COMP 102 #15 2013."

Similar presentations


Ads by Google