University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

Similar documents
! This week: Chapter 6 all ( ) ! Formal parameter: in declaration of class. ! Actual parameter: passed in when method is called

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Midterms Save the Dates!

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

Midterms Save the Dates!

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

Midterms Save the Dates!

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

COMP-202: Foundations of Programming. Lecture 6: Conditionals Jackie Cheung, Winter 2016

Midterms Save the Dates!

! Midterm 2: Thu Mar 16, 6:30pm (TODAY!) ! Woodward 2. ! hour-long exam, reserve 6:30-8 time slot. ! no labs/tutorials this week. ! Bunny.

Control Structures in Java if-else and switch

! Rest of Chap 2 ! 2.3-4, ! Rest of Chap 4 ! ! Things that do not vary. ! unlike variables. ! will never change. !

Survey #2. Variable Scope. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings. Scope Static.

Lab 8: IF statement. Conditionals and Loops. Copyright 2012 Pearson Education, Inc.

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

COMP-202: Foundations of Programming. Lecture 5: Arrays, Reference Type, and Methods Sandeep Manjanna, Summer 2015

Introduction to Computer Science Unit 2. Notes

Key Concept: all programs can be broken down to a combination of one of the six instructions Assignment Statements can create variables to represent

Object-Oriented Programming

Basics of Java Programming

Following is the general form of a typical decision making structure found in most of the programming languages:

Midterms Save the Dates!

Midterms Save the Dates!

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

SELECTION. (Chapter 2)

Programming with Java

APCS Semester #1 Final Exam Practice Problems

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

McGill University School of Computer Science COMP-202A Introduction to Computing 1

Midterms Save the Dates!

! labs last week. ! still time to work through lab 7 (midterm correction) ! can earn back up to 5 out of 70 points

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Final Exam. Programming Assignment 3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

CT 229 Java Syntax Continued

Programming in Java

Primitive Data, Variables, and Expressions; Simple Conditional Execution

CS 106 Introduction to Computer Science I

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

CS 102 / CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2010

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

Decision Making in C

Chapter 3. Selections

Decision Structures. Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Administration. Conditional Statements. Agenda. Syntax. Flow of control. Lab 2 due now on floppy Lab 3 due tomorrow via FTP

Using Classes and Objects. Lecture 7. Midterms Save the Dates! Extra Credit Survey

Lecture 6: While Loops and the Math Class

Survey #2. Teen Talk Barbie TM Reloaded. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu Partially Filled Arrays ArrayLists

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

! labs this week. ! midterms returned. ! work through what you got wrong on midterm. ! can earn back up to 5 out of 70 points

CONDITIONAL EXECUTION

Building Java Programs

Module 3 SELECTION STRUCTURES 2/15/19 CSE 1321 MODULE 3 1

CS110: PROGRAMMING LANGUAGE I

Important Java terminology

Midterms Save the Dates!

AP CS Unit 3: Control Structures Notes

CS11 Java. Fall Lecture 1

CSCI 131, Midterm Exam 1 Review Questions This sheet is intended to help you prepare for the first exam in this course. The following topics have

Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2

Control Structures in Java if-else and switch

Midterms Save the Dates!

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

In this chapter, you will:

PeerWise Study. Midterms Save the Dates! University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Constants Using Classes and Objects

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

CMPT 125: Lecture 3 Data and Expressions

COMP-202: Foundations of Programming. Lecture 5: More About Methods and Data Types Jackie Cheung, Winter 2016

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

COMP-202: Foundations of Programming. Lecture 4: Methods Jackie Cheung, Winter 2016

Decision Structures. Lesson 03 MIT 11053, Fundamentals of Programming

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

Lecture Set 4: More About Methods and More About Operators

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

CMPT 125: Lecture 4 Conditionals and Loops

CS 231 Data Structures and Algorithms, Fall 2016

M e t h o d s a n d P a r a m e t e r s

What we will do today Explain and look at examples of. Programs that examine data. Data types. Topic 4. variables. expressions. assignment statements

CS 112 Introduction to Programming

CS 112 Introduction to Programming

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

CS 161: Object Oriented Problem Solving

Chapter 3: Operators, Expressions and Type Conversion

MIDTERM REVIEW. midterminformation.htm

Inf1-OOP. Textbooks. Who and What. Organizational Issues. Why Java? Course Overview. Hello, World! in Java. Ewan Klein, Perdita Stevens

Accelerating Information Technology Innovation

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

Conditionals and Loops

CS 302: INTRODUCTION TO PROGRAMMING. Lectures 7&8

Lecture 14 CSE11 Fall 2013 For loops, Do While, Break, Continue

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner

Transcription:

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Conditionals II Lecture 11, Thu Feb 9 2006 based on slides by Kurt Eiselt http://www.cs.ubc.ca/~tmm/courses/cpsc111-06-spr

Reading This week: Chapter 6 all (6.1-6.4) Next week: Chapter 7 all (7.1-7.4) Reading summary so far: Chap 1, 2, 3, 4, 6 (no Chap 5!)

News Next week is reading week no lectures or labs or tutorials Midterms returned today Grades, statistics already posted on WebCT returned end of class, line up by last name reversed (Z-A) Assignment 1 was returned Tue pick up after class if you don't have it yet

Midterm Results 120 120 100 100 80 80 Percentage 60 40 60 40 20 20 0 1 8 15 22 29 36 43 50 57 64 71 78 85 92 99 106 113 120 127 134 141 0 1 8 15 22 29 36 43 50 57 64 71 78 85 92 99 106 113 120 127 134 141

News Reminder: protocol for regrade requests read solution and marking scheme first, carefully no regrade requests accepted until at least 24 hours after material is handed back exception: arithmetic errors regrade requests must be in writing (paper or email) assignments: to marker (listed on cover sheet) if still have dispute after discussion with TA, can escalate to instructor exams: to instructor

Recap: Static Methods Static methods do not operate in context of particular object cannot reference instance variables because they exist only in an instance of a class compiler will give error if static method attempts to use nonstatic variable Static method can reference static variables because static variables exist independent of specific objects

Recap: Static Methods in java.math Java provides you with many pre-existing static methods Package java.lang.math is part of basic Java environment you can use static methods provided by Math class examples: > Math.sqrt(36) 6.0 > Math.sin(90) 0.8939966636005579 > Math.sin(Math.toRadians(90)) 1.0 > Math.max(54,70) 70 > Math.round(3.14159) 3 > Math.random() 0.7843919693319797 > Math.random() 0.4253202368928023 > Math.pow(2,3) 8.0 > Math.pow(3,2) 9.0 > Math.log(1000) 6.907755278982137 > Math.log10(1000) 3.0

Recap: Conditional Statement Conditional statement: choose which statement will be executed next based on boolean expression changes control flow Example if (age < 20) System.out.println("Really, you look like you are " + (age + 5) + ".");

Recap: Boolean Expressions Boolean expression: test which returns either true or false when evaluated aka conditional Consists of operands and operators, like arithmetic expression but operators only return true or false when applied to operands Two different kinds of operators relational sometime split into relational and equality logical

Recap: Relational Operators Tests two values (operands) Operators == equal returns true if they are equal, false otherwise note: do not confuse this with =!= not equal returns true if they are not equal, false otherwise < less than <= less than or equal to > greater than >= greater than or equal to

Recap: Logical Operators Way to combine results from relational operators into single test AND, OR, and NOT in terms from math or philosophy class Operators && logical AND logical OR! logical NOT

Objectives Understand how to compare objects and primitive data types Understand syntax to use for conditionals and switch statements

Comparing Strings How do we test for equality between Strings? Reminder: Strings are sequences of alphanumeric characters create with constructor String firstname = new String("Donald"); or with shortcut String lastname = "Duck"; Strings are objects, not primitive types!

Comparing Strings Relational operator == is wrong way to compare String name1 = "Bubba"; String name2 = "Bubba"; System.out.println(name1 == name2); // prints false Equals method is right way to compare Strings String name1 = "Bubba"; String name2 = "Bubba"; System.out.println(name1.equals(name2)); // prints true why? diagrams will help

Comparing Strings name1 "Bubba" name2 "Bubba" these values tested for equality with test of name1 == name2 two different pointers (references), so false

Comparing Strings name1 "Bubba" name2 "Bubba" these values tested for equality with name1.equals(name2) contents of objects are same, so true

Short-Circuting Evaluation Consider again expression if ((b > a) && (c == 10)) System.out.println("this should print"); Java evaluates left to right if (b>a) is false, does value of (c == 10) matter? no! result of && must be false since one operand already evaluated to false short-circuiting: Java does not evaluate aka lazy evaluation

Short-Circuting Evaluation Consider different expression if ((b > a) (c == 10)) System.out.println("this should print"); Java evaluates left to right if (b>a) is true, does value of (c == 10) matter? no! result of must be true since one operand already evaluated to true

Syntax reserved word if If Syntax followed by boolean expression enclosed in parentheses followed by statement if (x == y) System.out.println("x equals y! "); Results if boolean evaluates to true, statement is executed otherwise statement is skipped, execution continues with statement immediately following if statement

If-Else Syntax If statement may include optional else clause reserved word else followed by another statement if (x == y) System.out.println("x equals y!"); else System.out.println("x is not equal to y!"); Results if boolean evaluates to true, first statement is executed otherwise (if boolean evalutes to false), statement following else is executed

Block Statements Often want to do many actions, not just one, based on condition Replace single statement with many statements surrounded by curly braces if (x == y) System.out.println("x equals y!"); System.out.println("I'm happy"); else System.out.println("x is not equal to y"); System.out.println("I'm depressed"); System.out.println("How about you?");

Block Statements What if we leave out block in else clause? if (x == y) System.out.println("x equals y!"); System.out.println("I'm happy"); else System.out.println("x is not equal to y"); System.out.println("I'm depressed"); System.out.println("How about you?");

Nested If Syntax Statements within if-else statements can themselves be if-else statements public class NestTest public static void main (String[] args) int x = 1; int y = 3; int z = 2; if (x == y) if (y == z) System.out.println("all three values the same"); else System.out.println("y is not equal to z"); else System.out.println("x is not equal to y");

Nested If Syntax Multiple else statements also legal if( Boolean expression 1 ) // statements else if( Boolean expression 2 ) // statements else if( Boolean expression 3 ) // statements else // statements

Nested If Syntax Rewriting NestTest using multiple else statements public class NestTest2 public static void main (String[] args) int x = 1; int y = 3; int z = 2; if ((x == y) && (y == z)) System.out.println("all three values the same"); else if ((x == y) && (y!= z)) System.out.println("y is not equal to z"); else System.out.println("x is not equal to y");

Comparing Floating Point Numbers Is 0.3 the same thing as 1.0/10.0 + 1.0/10.0 + 1.0/10.0??? Let s try it out... double sum = 1.0/10.0 + 1.0/10.0 + 1.0/10.0; double literal =.3; if (sum == literal) System.out.println ( Yup, they match"); else System.out.println ( Nope, don t match"); System.out.println("Sum is "+sum+" literal +" literal);

Comparing Floating Point Numbers Is 0.3 the same thing as 1.0/10.0 + 1.0/10.0 + 1.0/10.0??? No - very close, but not exactly what you expect 0.30000000000000004 Beware! Write tests for darn near equal like: if (Math.abs(f1 - f2) < TOLERANCE) System.out.println ( Essentially equal. ); where TOLERANCE is small number appropriate to problem like 0.00000001

Comparing Characters You can compare character types with relational operators 'a' < 'b' 'a' == 'b' 'a' < 'A' Remember, cannot compare Strings with relational operators or any other objects! must use methods like equals

Switch Syntax Use switch statement to get program to follow one of several different paths based on single value switch (finalmark) case 4: System.out.println("You get an A"); break; case 3: System.out.println("You get a B"); break; case 2: System.out.println("You get a C"); break; default: System.out.println("See you next year");

Switch Syntax Expression should be int, char (or enumerated type) switch (finalmark) case 4: System.out.println("You get an A"); break; case 3: System.out.println("You get a B"); break; case 2: System.out.println("You get a C"); break; default: System.out.println("See you next year");

Switch Syntax Case values cannot be variables switch (finalmark) case 4: System.out.println("You get an A"); break; case 3: System.out.println("You get a B"); break; case 2: System.out.println("You get a C"); break; default: System.out.println("See you next year");

Switch Syntax Default statement optional, but very good idea switch (finalmark) case 4: System.out.println("You get an A"); break; case 3: System.out.println("You get a B"); break; case 2: System.out.println("You get a C"); break; default: System.out.println("See you next year");

Switch Syntax Break statements really important switch (finalmark) case 4: System.out.println("You get an A"); break; case 3: System.out.println("You get a B"); break; case 2: System.out.println("You get a C"); break; default: System.out.println("See you next year");