Working with multiple objects A variable can refer to a single object: IBehavior cc = new ColorChanging(); IBehavior br = new Breathing(); A variable referring.

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Introduction to Programming Java Lab 5: Boolean Operations 8 February JavaLab5 lecture slides.ppt Ping Brennan
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
Basic Logical Operations (fascinating)
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
Mixing types in expressions Operators such as +, -, * and / are overloaded: the same name has many different values + overloaded as String concatenation.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
Array Lists Chapter 7.2 Pages Array Lists In Java, Arrays are an important structure for storing data. We will learn more about arrays later,
Week 10 Recap CSE 115 Spring For-each loop When we have a collection and want to do something to all elements of that collection we use the for-each.
Primitives in Java Java has eight primitive types –boolean –integral types: signed: long, int, short, byte unsigned: char –floating point types: double,
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
James Tam AND OR NOT NAND NOR XOR Basic logic operations (fascinating)
Primitive Types CSE 115 Spring 2006 April 3 &
CSE 143 Lecture 7 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
Python Control of Flow.
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
Computer Science 101 The Boolean System. George Boole British mathematician ( ) Boolean algebra –Logic –Set theory –Circuits –Conditions in if.
Goals for Today  implement a Deck of Cards  composition  Iterator interface  Iterable interface 1.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
Flow of Control Part 1: Selection
C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",
Computer Science 111 Fundamentals of Programming I Making Choices with if Statements.
ITM 352 Flow-Control: if and switch. ITM © Port, KazmanFlow-Control - 2 What is "Flow of Control"? Flow of Control is the execution order of instructions.
Collection 105 Yola. To store data in RAM Variables (name all the types with their length) Arrays (one, two or more) Collections and Maps.
Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
CompSci Arrays  Aggregate data type  Deal with items of same type  Lists of words  Numbers  Analogies  Mailboxes in post office  CD racks.
Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.
3. Controlling Program Flow Methods, parameters, and return values Boolean expressions Conditional branching Loops.
Primitive variables Android Club types of variables: Primitive single value (starts with uppercase letter) Complex multiple value (starts with.
February ,  2/16: Exam 1 Makeup Papers Available  2/20: Exam 2 Review Sheet Available in Lecture  2/27: Lab 2 due by 11:59:59pm  3/2:
Grouping objects Iterators. Iterator type Third variation to iterate over a collection Uses a while loop and Iterator object But NO integer index variable.
What is a Boolean expression?
Computer Programming Boolean Logic Trade & Industrial Education
A: A: double “4” A: “34” 4.
Operators.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
Language Find the latest version of this document at
1 CS161 Introduction to Computer Science Topic #6.
Decision Statements, Short- Circuit Evaluation, Errors.
5.02B Decision Making Structure (part 2). Compound Boolean Expressions.
TestScore < 80 testScore * 2 >= < w / (h * h) x + y != 2 * (a + b) 2 * Math.PI * radius
LOOPS CHAPTER Topics  Four Types of Loops –while –do…while –for –foreach  Jump Statements in Loops –break –continue 2.
Array: Sequence of values of the same type Construct array: new double[10] Store in variable of type double[] : double[] data = new double[10]; When array.
TOPIC 8 MORE ON WHILE LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
Lazy Evaluation Computer Science 3 Gerb Objective: Understand lazy evaluation in Java.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
Sets Set is an unordered list of items
University of Central Florida COP 3330 Object Oriented Programming
Making Choices with if Statements
University of Central Florida COP 3330 Object Oriented Programming
The University of Texas Rio Grande Valley
The University of Texas – Pan American
The University of Texas – Pan American
Functions As Objects.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises UTPA – Fall 2012 This set of slides is revised from.
Code Refresher Test #1 Topics:
CHAPTER 21 LOOPS 1.
Evaluating Boolean expressions
Midterm Review October 23, 2006 ComS 207: Programming I (in Java)
Topics discussed in this section:
Question 1a) What is printed by the following Java program? int s;
Controlling Program Flow
Presentation transcript:

Working with multiple objects A variable can refer to a single object: IBehavior cc = new ColorChanging(); IBehavior br = new Breathing(); A variable referring to many objects? No, but we saw how to build a composite object. IBehavior cc = new CompositeBehavior(cc,br); But it’s not too easy to add/remove items from a composite. Are there options?

Collections interface: java.util.Collection classes: –java.util.HashSet –java.util.ArrayList E is the type of element contained in the collection

Using a Collection (HashSet ) HashSet names = new HashSet (); names.add(“Amy”);names.remove(“Bob”); names.add(“Bob”); names.add(“Cindy”); names.add(“Dave”); names.add(“Emma”); …

for-each loop (w/HashSet ) for (String name : names) { System.out.println(name); } This prints out the following in the console window: Amy Cindy Dave Emma

Collections.shuffle Collections.shuffle(List ) The shuffle method randomizes the order of elements in the collection it is passed. The collection passed to shuffle must be a List: a collection which maintains items in some order.

Using a Collection (ArrayList ) ArrayList names2 = new ArrayList (); names2.add(“Amy”);names2.remove(“Bob”); names2.add(“Bob”); names2.add(“Cindy”); names2.add(“Dave”); names2.add(“Emma”); …

for-each loop (w/ArrayList ) for (String name : names2) { System.out.println(name); } This prints out the following in the console window: Amy Cindy Dave Emma

shuffling… Collections.shuffle(names2); for (String name : names2) { System.out.println(name); } When I ran it once:and again:and again: EmmaDaveCindy DaveCindyAmy AmyEmmaEmma CindyAmyDave

Returning to the boolean operators &&“and” ||“or” !“not”

(x && y) is true only if x is true and y is true truth tables –both convey same information, but in different ways &&truefalse true false xyx && y true false truefalse

(x || y) is true only if x is true or y is true truth tables –both convey same information, but in different ways ||truefalse true falsetruefalse xyx || y true false true falsetrue false

!x is true only if x is false truth tables –both convey same information, but in different ways !truefalse true x!x truefalse true

Short circuiting && and || are so-called "short circuit" operators They only evaluate as much as is needed –In an expression such as x&&y, if x is false there is no need to evaluate y (since x&&y must be false ) –In an expression such as x||y, if x is true there is no need to evaluate y (since x||y must be true )

Use of short-circuiting short-circuiting can be used to effect a form on conditional evaluation: public void shortCircuitTest(int x) { int y=5; int z=2; if ( x!=0 && (y/x)<z ) { System.out.println("Division was safe"); } else { System.out.println("Avoided divide-by-zero"); } shortCircuitTest(3) prints Division was safe shortCircuitTest(0) prints Avoided divide-by-zero