Working with multiple objects - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Working with multiple objects

Description:

... to many objects? No, but we saw how to build a composite object. ... The shuffle method randomizes the order of elements in the collection it is passed. ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 15
Provided by: cseBu
Category:

less

Transcript and Presenter's Notes

Title: Working with multiple objects


1
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 its not too easy to add/remove items from a
    composite. Are there options?

2
Collections
  • interface java.util.CollectionltEgt
  • classes
  • java.util.HashSetltEgt
  • java.util.ArrayListltEgt
  • E is the type of element contained in the
    collection

3
Using a Collection (HashSetltEgt)
  • HashSetltStringgt names new HashSetltStringgt()
  • names.add(Amy) names.remove(Bob)
  • names.add(Bob)
  • names.add(Cindy)
  • names.add(Dave)
  • names.add(Emma)

4
for-each loop (w/HashSetltEgt)
  • for (String name names)
  • System.out.println(name)
  • This prints out the following in the console
    window
  • Amy
  • Cindy
  • Dave
  • Emma

5
Collections.shuffle
  • Collections.shuffle(ListltEgt)
  • 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.

6
Using a Collection (ArrayListltEgt)
  • ArrayListltStringgt names2 new ArrayListltStringgt()
  • names2.add(Amy) names2.remove(Bob)
  • names2.add(Bob)
  • names2.add(Cindy)
  • names2.add(Dave)
  • names2.add(Emma)

7
for-each loop (w/ArrayListltEgt)
  • for (String name names2)
  • System.out.println(name)
  • This prints out the following in the console
    window
  • Amy
  • Cindy
  • Dave
  • Emma

8
shuffling
  • Collections.shuffle(names2)
  • for (String name names2)
  • System.out.println(name)
  • When I ran it once and again and again
  • Emma Dave Cindy
  • Dave Cindy Amy
  • Amy Emma Emma
  • Cindy Amy Dave

9
Returning tothe boolean operators
  • and
  • or
  • ! not

10
(x y) is true only ifx is true and y is true
  • truth tables
  • both convey same information, but in different
    ways

11
(x y) is true only ifx is true or y is true
  • truth tables
  • both convey same information, but in different
    ways

12
!x is true only ifx is false
  • truth tables
  • both convey same information, but in different
    ways

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

14
Use of short-circuiting
  • short-circuiting can be used to effect a form on
    conditional evaluation
  • public void shortCircuitTest(int x)
  • int y5
  • int z2
  • if ( x!0 (y/x)ltz )
  • 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
Write a Comment
User Comments (0)
About PowerShow.com