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

Slides:



Advertisements
Similar presentations
Lecture 10 Flow of Control: Loops (Part 2) COMP1681 / SE15 Introduction to Programming.
Advertisements

MiniDraw Testing COMP 102 # T1
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Creating and using Objects.
Building Java Programs
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Methods COMP.
© Peter Andreae What is Programming About COMP 102 # T1 Peter Andreae Computer Science Victoria University of Wellington.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
Wednesday, 10/9/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/9/02  QUESTIONS ??  Today:  Discuss HW #02  Discuss test question types  Review 
XP 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial 10.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Conditionals.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Create Objects,
©Xiaoying Gao, Peter Andreae First Java Program COMP 102 #2 2014T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods with Parameters COMP.
First Java Program COMP 102 #2 2015T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
School of Computer Science & Information Technology G6DICP - Lecture 9 Software Development Techniques.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
2011-T1 Lecture 13 School of Engineering and Computer Science, Victoria University of Wellington  Rashina Hoda and Peter Andreae COMP 102 Rashina Hoda.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington 2D arrays COMP 102 # T1.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Files COMP 102.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Conditionals.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington More Interfaces.
CSC 212 – Data Structures Prof. Matthew Hertz WTC 207D /
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Methods COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Classes, Objects, Fields,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 103 #
ReviewNum1 { Hardware, Binary, Java, BlueJ, Variables, Classes, Arithmetic, Print Statements, Errors, Boolean Expressions, Conditionals, Loops, Random.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Loops and Input COMP 102 #
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Classes and.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More on Files COMP 102 # 14.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Fields, Constructors.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More on Files COMP 102 # 14.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Loops and Input COMP 102 #
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods Calling Methods Return.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington 2D arrays COMP 102 # T1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Classes and.
2011-T1 Lecture 10 School of Engineering and Computer Science, Victoria University of Wellington  Rashina Hoda and Peter Andreae COMP 102 Rashina Hoda.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists: varying size arrays.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists: varying size arrays.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Programs with Choice Booleans,
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
2011-T1 Lecture 12 School of Engineering and Computer Science, Victoria University of Wellington  Rashina Hoda and Peter Andreae COMP 102 Rashina Hoda.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Creating and using Objects.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with While loops.
© Peter Andreae Java Programs COMP 102 # T1 Peter Andreae Computer Science Victoria University of Wellington.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Arrays COMP 102 # T1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Dealing with Files COMP 102.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More on ArrayLists COMP 102.
CS305J Introduction to Computing
Review for Final Exam.
CS139 October 11, 2004.
Review for Final Exam.
Fundamental Programming
Conditionals and Loops
Corresponds with Chapter 5
Day 11 The Last Week!.
Presentation transcript:

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; }