Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming
Advertisements

DECISIONS Chapter 5. The if Statement  Action based on a conditions  If the condition is true, the body of the statement is executed if (amount
Math 130 Decisions II Wed, Sept 12, 2007 Notes: ref. C. Horstmann, Java Concepts.
Chapter 5 – Decisions Big Java by Cay Horstmann
Chapter 5 Selection Statements. Topics Controlling program flow selection –if –switch Boolean expressions –boolean primitive type –comparison operators.
Chapter 5 Decisions Goals To be able to implement decisions using if statements To be able to implement decisions using if statements To understand how.
Chapter 6 Horstmann Programs that make decisions: the IF command.
Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
Chapter 5  Decisions 1 Chapter 5 Decisions. Chapter 5  Decisions 2 Chapter Goals  To be able to implement decisions using if statements  To understand.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Computer Science A 3: 10/2. Logical Expressions Expressions that has a value which is either true or false. Logical expressions are essential in a program.
If Statement if (amount
Chapter 5 – Decisions Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
BOOLEAN LOGIC CSC 171 FALL 2004 LECTURE 7. ASSIGNMENT Review Quiz # 2 Start reading Chapter 5.
ACSE th Conference The Iconic Programmer Stephen Chen.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Chapter 6 Decisions. Chapter Goals To be able to implement decisions using if statements To understand how to group statements into blocks To learn how.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
The switch Statement, DecimalFormat, and Introduction to Looping
Week 3 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 3 Making Decisions
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 7 Decision Making : selection statements.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. if (amount
Decisions CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (Chapter 5, Horstmann*
Decisions: Handling Infinite Cases CS 21a: Introduction to Computing I First Semester,
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Week 5 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Flow of Control Part 1: Selection
Week 2 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
1 Week 6 Branching. 2 What is “Flow of Control”? l Flow of Control is the execution order of instructions in a program l All programs can be written with.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Review the following: if-else One branch if Conditional operators Logical operators Switch statement Conditional expression operator Nested ifs if –else.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
1 2. Program Construction in Java. 2.4 Selection (decisions)
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
TK 1914 : C++ Programming Control Structures I (Selection)
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Week 14 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Selection Statements. Introduction Today we learn more about learn to make decisions in Turing ▫Nested if statements, ▫case statements.
If Statement if (amount
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Decisions. Three Forms of Decision Making in Java if statements (test a boolean expression) switch statements (test an integer expression) conditional.
Chapter 6 Decisions. Chapter Goals To be able to implement decisions using if statements To understand how to group statements into blocks To learn how.
Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Decisions Bush decision making.
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
Control statements Mostafa Abdallah
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Week 9 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Chapter 4 Repetition Statements (loops)
The switch Statement, and Introduction to Looping
Chapter 5/6 Decisions.
Control Structures – Selection
Control Statement Examples
Expression Review what is the result and type of these expressions?
Chapter 5 – Decisions Big Java by Cay Horstmann
Java Statements B.Ramamurthy CS114A, CS504 4/23/2019 BR.
Chapter 5 – Decisions Big Java by Cay Horstmann
Presentation transcript:

Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

Week 7 Topics The if statement Comparing Values Multiple Alternatives Using Boolean Expressions Code Coverage

7.1.1 The if statement if (condition) statement; Condition returns a boolean true or false If the condition is true, the statement or statements in the body of the if statement will be executed if (amount <= this.balance) this.balance -= amount;

7.1.1 The if statement cont. To implement a choice between alternatives, use the if/else structure if (amount <= this.balance) this.balance -= amount; else this.balance -= OVERDRAFT_PENALTY; Use brackets if there are multiple statements in the body of the if statement if (amount <= balance) { double newBalance = this.balance – amount; this.balance = newBalance; }

7.1.2 Comparing Values Relational Operators OperatorDescription >Greater than >=Greater than or equal <Less than <=Less than or equal ==Equal !=Not equal

7.1.2 Comparing Values Cont. You must be careful when comparing floating-point numbers due to rounding errors. We test to see if close enough. final double EPSILON = 1E-14; if (Math.abs(x – y) <= EPSILON) // x is approximately equal to y assertEquals(x, y, EPSILON); // JUnit method

7.1.2 Comparing Values Cont. To test whether two strings are equal, do NOT use ==, use the equals method. if (s1.equals(s2)) … if (s1.equalsIgnoreCase(s2)) … Use the compareTo method to compare strings in dictionary order if (s1.compareTo(s2) < 0) // s1 is before s2 if (s1.compareTo(s2) > 0) // s1 is after s2 if (s1.compareTo(s2) == 0) // s1 equals s2

7.1.2 Comparing Values Cont. When comparing two object references with the == operator, you test whether the references refer to the same object Rectangle r1 = new Rectangle(5,7,9,9); Rectangle r2 = r1; Rectangle r3 = new Rectangle(5,7,9,9); r1 == r2 is true r1 == r3 is false

7.1.2 Comparing Values Cont. To compare whether the contents of two rectangles are the same, use the equals method r1.equals(r3) is true The Rectangle class like the String class has an equals method. If you write your own class, you will have to implement your own equals method (beyond the scope of this course).

7.1.2 Comparing Values Cont. To test if an object reference has been initialized, you can use null if (middleName == null) System.out.println("No middle name"); else System.out.println(middleName); Some feel it is a good ideal to set an object variable to null if it will not be instantiated until later String middleName = null;

7.1.3 Multiple Alternatives if (age >= 100) s = “What is your secret!”; else if (age >= 65) s = “Enjoying your golden years?”; else if (age >= 45) s = “Feeling some aches and pains?”; else if (age >= 30) s = “Oh to be in your prime again”; else s = “You young whipper snapper!”;

7.1.3 Multiple Alternatives Cont. Note that if the order of conditions for the example in the previous slide were reversed, the logic would not work as intended. Watch out for this trap when coding multiple conditions! if (age >= 0) // This will not work correctly! s = “You young whipper snapper!”; else if (age >= 30) s = “Oh to be in your prime again”; … You will never get past the first condition

7.1.3 Multiple Alternatives Cont. If you have the special case where you are just comparing a single int or char value, the switch statement can be used: switch (digit) // digit is an int for this example { case 1: System.out.println(“one”); break; case 2: System.out.println(“two”); break; case 3: System.out.println(“three”); break; case 4: System.out.println(“four”); break; default: System.out.println(“N/A”); break; }

7.1.3 Multiple Alternatives Cont. Conditional statements can have nested branches: if (vehicle == CAR) // int constant { if (cost <= BUDGET) // double constant // do something else if (cost <= MID_PRICED) // do something else … } else if (vehicle == TRUCK) {

7.1.4 Using Boolean Expressions An expression such as amount < 1000 has a value, either true or false System.out.println(amount < 1000); The above prints true if amount is less than 1000, otherwise prints false A predicate method returns a boolean value if (harrysChecking.isOverdrawn()) …

7.1.4 Using Boolean Expressions Cont. if (Character.isUpperCase(ch)) … if (in.hasNext()) n = in.nextInt(); The isOverdrawn() predicate method from the previous slide might look like this: public boolean isOverdrawn() { return this.balance < 0; }

7.1.4 Using Boolean Expressions Cont. How do I test if amount is between 0 and 1000 inclusive? Use the boolean AND operator (all conditions must be true): if (0 <= amount && amount <= 1000) … If just one condition must be true, use the boolean OR operator: if (in.equals(“S”) || in.equals(“L”)) … To invert a condition, use the boolean NOT operator: if (!in.equals(“S”)) …

7.1.4 Using Boolean Expressions Cont. Logical AND operator ABA && B true false truefalse

7.1.4 Using Boolean Expressions Cont. Logical OR operator ABA || B true falsetrue falsetrue false

7.1.4 Using Boolean Expressions Cont. Logical NOT operator A!A truefalse true

7.1.5 Code Coverage Unit tests are used to test classes in isolation of one another A Unit Testing framework typically allows the programmer to compare a predetermined expected result against the result produced by the program Unit tests are white box tests in that they exploit knowledge of the implementation

7.1.5 Code Coverage Cont. Black-box testing describes a testing method that does not take the structure of the implementation into account, as typical of customer acceptance testing Test coverage is a measure of taking into account how many parts of a program have been tested For example, you should test every branch of all if/else code

7.1.5 Code Coverage Cont. Logging is a way of tracing the execution of a program through all the method calls More than a useful debugging tool, logging can be a business requirement in many situations Consider that all bank transactions should be logged for accounting purposes

7.1.5 Code Coverage Cont. However, the most common use for logging is debugging A logging API allows a programmer to log messages at various levels of severity, capture the log to a file, and even turn logging on and off entirely

Reference: Big Java 4 th Edition by Cay Horstmann The if statement (section 5.1 in Big Java) Comparing Values (section 5.2 in Big Java) Multiple Alternatives (section 5.3 in Big Java) Using Boolean Expressions (section 5.4 in Big Java) Code Coverage (section 5.5 in Big Java)