Building Java Programs



Similar documents
Topic 16 boolean logic

Building Java Programs

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Building Java Programs

Topic 11 Scanner object, conditional execution

Building Java Programs

J a v a Quiz (Unit 3, Test 0 Practice)

Chapter 5. Selection 5-1

Introduction to Java

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Statements and Control Flow

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

if and if-else: Part 1

Outline. Conditional Statements. Logical Data in C. Logical Expressions. Relational Examples. Relational Operators

Conditional Statements Summer 2010 Margaret Reid-Miller

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Lecture 2 Notes: Flow of Control

Introduction to Computer Programming, Spring Term 2014 Practice Assignment 3 Discussion

Chapter 8 Selection 8-1

Sources: On the Web: Slides will be available on:

Sample CSE8A midterm Multiple Choice (circle one)

Lecture Notes on Linear Search

The C++ Language. Loops. ! Recall that a loop is another of the four basic programming language structures

Example of a Java program

Building Java Programs

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Conditionals (with solutions)

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Unit 6. Loop statements

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Chapter 2: Algorithm Discovery and Design. Invitation to Computer Science, C++ Version, Third Edition

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Computational Mathematics with Python

Introduction to Programming (in C++) Loops. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Example. Introduction to Programming (in C++) Loops. The while statement. Write the numbers 1 N. Assume the following specification:

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

Lecture 9 verifying temporal logic

Java Basics: Data Types, Variables, and Loops

12-6 Write a recursive definition of a valid Java identifier (see chapter 2).

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

Computational Mathematics with Python

Web Programming Step by Step

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

VB.NET Programming Fundamentals

CmpSci 187: Programming with Data Structures Spring 2015

1.00/ Session 2 Fall Basic Java Data Types, Control Structures. Java Data Types. 8 primitive or built-in data types

Homework/Program #5 Solutions

Selection Statements

Moving from CS 61A Scheme to CS 61B Java

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

13 Classes & Objects with Constructors/Destructors

ESCI 386 IDL Programming for Advanced Earth Science Applications Lesson 6 Program Control

Object Oriented Software Design

5.2 Q2 The control variable of a counter-controlled loop should be declared as: a.int. b.float. c.double. d.any of the above. ANS: a. int.

Chapter 2 Introduction to Java programming

CS 111 Classes I 1. Software Organization View to this point:

1.3 Conditionals and Loops

Bash shell programming Part II Control statements

AP Computer Science Java Subset

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

Basics of I/O Streams and File I/O

CSC 221: Computer Programming I. Fall 2011

It has a parameter list Account(String n, double b) in the creation of an instance of this class.

Programming Database lectures for mathema

Stacks. Linear data structures

Part I. Multiple Choice Questions (2 points each):

13 File Output and Input

6. Control Structures

by Mario Fusco Monadic

Recursion. Definition: o A procedure or function that calls itself, directly or indirectly, is said to be recursive.

CS 121 Intro to Programming:Java - Lecture 11 Announcements

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

Computational Mathematics with Python

Boolean Expressions 1. In C++, the number 0 (zero) is considered to be false, all other numbers are true.

public static void main(string[] args) { System.out.println("hello, world"); } }

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Iteration CHAPTER 6. Topic Summary

COMP 110 Prasun Dewan 1

Java Programming Fundamentals

Chapter 14: Boolean Expressions Bradley Kjell (Revised 10/08/08)

The following program is aiming to extract from a simple text file an analysis of the content such as:

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

LOOPS CHAPTER CHAPTER GOALS

The if Statement and Practice Problems

Unit Testing & JUnit

Thinking of a (block) cipher as a permutation (depending on the key) on strings of a certain size, we would not want such a permutation to have many

Basic Programming and PC Skills: Basic Programming and PC Skills:

Software Engineering Techniques

Keil C51 Cross Compiler

Calling the Function. Two Function Declarations Here is a function declared as pass by value. Why use Pass By Reference?

PIC 10A. Lecture 7: Graphics II and intro to the if statement

2.3 WINDOW-TO-VIEWPORT COORDINATE TRANSFORMATION

High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)

4.2 Sorting and Searching

Transcription:

Building Java Programs Chapter 5 Lecture 5-3: Boolean Logic and Assertions reading: 5.3 5.5 1

2

Type boolean boolean: A logical type whose values are true and false. A logical test is actually a boolean expression. Like other types, it is legal to: create a boolean variable pass a boolean value as a parameter return a boolean value from methods call a method that returns a boolean and use it as a test boolean minor = age < 21; boolean isprof = name.contains("prof"); boolean lovescse = true; // allow only CSE-loving students over 21 if (minor isprof!lovescse) { System.out.println("Can't enter the club!"); 3

Using boolean Why is type boolean useful? Can capture a complex logical test result and use it later Can write a method that does a complex test and returns it Makes code more readable Can pass around the result of a logical test (as param/return) boolean goodage = age >= 12 && age < 29; boolean goodheight = height >= 78 && height < 84; boolean rich = salary >= 100000.0; if ((goodage && goodheight) rich) { System.out.println("Okay, let's go out!"); else { System.out.println("It's not you, it's me..."); 4

Logical operators Tests can be combined using logical operators: Operator Description Example Result && and (2 == 3) && (-1 < 5) false or (2 == 3) (-1 < 5) true! not!(2 == 3) true "Truth tables" for each, used with logical values p and q: p q p && q p q true true true true true false false true false true false true false false false false p!p true fals e false true 5

Evaluating logical expressions Relational operators have lower precedence than math; logical operators have lower precedence than relational operators 5 * 7 >= 3 + 5 * (7 1) && 7 <= 11 5 * 7 >= 3 + 5 * 6 && 7 <= 11 35 >= 3 + 30 && 7 <= 11 35 >= 33 && 7 <= 11 true && true true Relational operators cannot be "chained" as in algebra 2 <= x <= 10 true <= 10 (assume that x is 15) Error! Instead, combine multiple tests with && or 2 <= x && x <= 10 true && false false 6

Returning boolean public static boolean isprime(int n) { int factors = 0; for (int i = 1; i <= n; i++) { if (n % i == 0) { factors++; if (factors == 2) { return true; else { return false; Calls to methods returning boolean can be used as tests: if (isprime(57)) {... 7

"Boolean Zen", part 1 Students new to boolean often test if a result is true: if (isprime(57) == true) {... // bad But this is unnecessary and redundant. Preferred: if (isprime(57)) {... // good A similar pattern can be used for a false test: if (isprime(57) == false) { if (!isprime(57)) { // bad // good 8

"Boolean Zen", part 2 Methods that return boolean often have an if/else that returns true or false: public static boolean bothodd(int n1, int n2) { if (n1 % 2!= 0 && n2 % 2!= 0) { return true; else { return false; But the code above is unnecessarily verbose. 9

Solution w/ boolean variable We could store the result of the logical test. public static boolean bothodd(int n1, int n2) { boolean test = (n1 % 2!= 0 && n2 % 2!= 0); if (test) { // test == true return true; else { // test == false return false; Notice: Whatever test is, we want to return that. If test is true, we want to return true. If test is false, we want to return false. 10

Solution w/ "Boolean Zen" Observation: The if/else is unnecessary. The variable test stores a boolean value; its value is exactly what you want to return. So return that! public static boolean bothodd(int n1, int n2) { boolean test = (n1 % 2!= 0 && n2 % 2!= 0); return test; An even shorter version: We don't even need the variable test. We can just perform the test and return its result in one step. public static boolean bothodd(int n1, int n2) { return (n1 % 2!= 0 && n2 % 2!= 0); 11

Replace "Boolean Zen" template public static boolean name(parameters) { if (test) { return true; else { return false; with public static boolean name(parameters) { return test; 12

Improved isprime method The following version utilizes Boolean Zen: public static boolean isprime(int n) { int factors = 0; for (int i = 1; i <= n; i++) { if (n % i == 0) { factors++; return factors == 2; // if n has 2 factors -> true 13

Boolean practice questions Write a method named isvowel that returns whether a String is a vowel (a, e, i, o, or u), case-insensitively. isvowel("q") returns false isvowel("a") returns true isvowel("e") returns true Change the above method into an isnonvowel that returns whether a String is any character except a vowel. isnonvowel("q") returns true isnonvowel("a") returns false isnonvowel("e") returns false 14

Boolean practice answers // Enlightened version. I have seen the true way (and false way) public static boolean isvowel(string s) { return s.equalsignorecase("a") s.equalsignorecase("e") s.equalsignorecase("i") s.equalsignorecase("o") s.equalsignorecase("u"); // Enlightened "Boolean Zen" version public static boolean isnonvowel(string s) { return!s.equalsignorecase("a") &&!s.equalsignorecase("e") &&!s.equalsignorecase("i") &&!s.equalsignorecase("o") &&!s.equalsignorecase("u"); // or, return!isvowel(s); 15

De Morgan's Law De Morgan's Law: Rules used to negate boolean tests. Useful when you want the opposite of an existing test. Original Expression Negated Expression Alternativ e a && b!a!b!(a && b) a b!a &&!b!(a b) Example: Original Code if (x == 7 && y > 3) {... Negated Code if (x!= 7 y <= 3) {... 16

When to return? Methods with loops and return values can be tricky. When and where should the method return its result? Write a method seven that accepts a Random parameter and uses it to draw up to ten lotto numbers from 1-30. If any of the numbers is a lucky 7, the method should stop and return true. If none of the ten are 7 it should return false. The method should print each number as it is drawn. 15 29 18 29 11 3 30 17 19 22 (first call) 29 5 29 4 7 (second call) 17

Flawed solution // Draws 10 lotto numbers; returns true if one is 7. public static boolean seven(random rand) { for (int i = 1; i <= 10; i++) { int num = rand.nextint(30) + 1; System.out.print(num + " "); if (num == 7) { return true; else { return false; The method always returns immediately after the first draw. This is wrong if that draw isn't a 7; we need to keep drawing. 18

Returning at the right time // Draws 10 lotto numbers; returns true if one is 7. public static boolean seven(random rand) { for (int i = 1; i <= 10; i++) { int num = rand.nextint(30) + 1; System.out.print(num + " "); if (num == 7) { return true; // found lucky 7; can exit now return false; // if we get here, there was no 7 Returns true immediately if 7 is found. If 7 isn't found, the loop continues drawing lotto numbers. If all ten aren't 7, the loop ends and we return false. 19

Boolean return questions hasanodddigit : returns true if any digit of an integer is odd. hasanodddigit(4822116) returns true hasanodddigit(2448) returns false alldigitsodd : returns true if every digit of an integer is odd. alldigitsodd(135319) returns true alldigitsodd(9174529) returns false isallvowels : returns true if every char in a String is a vowel. isallvowels("eieio") returns true isallvowels("oink") returns false These problems are available in our Practice-It! system under 5.x. 20

Boolean return answers public static boolean hasanodddigit(int n) { while (n!= 0) { if (n % 2!= 0) { // check whether last digit is odd return true; n = n / 10; return false; public static boolean alldigitsodd(int n) { while (n!= 0) { if (n % 2 == 0) { return false; n = n / 10; return true; // check whether last digit is even public static boolean isallvowels(string s) { for (int i = 0; i < s.length(); i++) { String letter = s.substring(i, i + 1); if (!isvowel(letter)) { return false; return true; 21