Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.

29
Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.

Transcript of Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.

Page 1: Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.

Random, Collections & Loops

Chapter 5

Copyright © 2012 Pearson Education, Inc.

Page 2: Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.

Conditionals and Loops• So far, we’ve looked at:

– making decisions with if

– how to compare data

– Boolean expressions

• Today we’ll look at:– Generating lists of random numbers

– Storing lists (collections) of objects

– repeat processing steps in a loop

Copyright © 2012 Pearson Education, Inc.

Page 3: Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.

RANDOM

Copyright © 2012 Pearson Education, Inc.

Page 4: Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.

The Random Class

• The Random class is part of the java.util package

• It provides methods that generate pseudorandom numbers

• A Random object performs complicated calculations based on a seed value to produce a stream of seemingly random values

Copyright © 2012 Pearson Education, Inc.

Page 5: Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.
Page 6: Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.

Copyright © 2012 Pearson Education, Inc.

//********************************************************************// RandomNumbers.java Author: Lewis/Loftus//// Demonstrates the creation of pseudo-random numbers using the// Random class.//********************************************************************

import java.util.Random;

public class RandomNumbers{ //----------------------------------------------------------------- // Generates random numbers in various ranges. //----------------------------------------------------------------- public static void main (String[] args) { Random generator = new Random(); int num1; float num2;

num1 = generator.nextInt(); System.out.println ("A random integer: " + num1);

num1 = generator.nextInt(10); System.out.println ("From 0 to 9: " + num1);

continued

Page 7: Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.

Copyright © 2012 Pearson Education, Inc.

continued

num1 = generator.nextInt(10) + 1; System.out.println ("From 1 to 10: " + num1);

num1 = generator.nextInt(15) + 20; System.out.println ("From 20 to 34: " + num1);

num1 = generator.nextInt(20) - 10; System.out.println ("From -10 to 9: " + num1);

num2 = generator.nextFloat(); System.out.println ("A random float (between 0-1): " + num2);

num2 = generator.nextFloat() * 6; // 0.0 to 5.999999 num1 = (int)num2 + 1; System.out.println ("From 1 to 6: " + num1); }}

Sample RunA random integer: 672981683From 0 to 9: 0From 1 to 10: 3From 20 to 34: 30From -10 to 9: -4A random float (between 0-1): 0.18538326From 1 to 6: 3

Page 8: Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.

Examples – code to outcome

Copyright © 2012 Pearson Education, Inc.

Given a Random object named gen, what range of values are produced by the following expressions?

gen.nextInt(25)

gen.nextInt(6) + 1

gen.nextInt(100) + 10

gen.nextInt(50) + 100

gen.nextInt(10) – 5

gen.nextInt(22) + 12

Range?

0 to 24

1 to 6

10 to 109

100 to 149

-5 to 4

12 to 33

Page 9: Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.

Examples – outcome to code

Copyright © 2012 Pearson Education, Inc.

Write an expression that produces a random integer in the following ranges:

gen.nextInt(13)

gen.nextInt(20) + 1

gen.nextInt(6) + 15

gen.nextInt(11) – 10

Range

0 to 12

1 to 20

15 to 20

-10 to 0

Page 10: Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.

COLLECTIONS & LOOPS

Copyright © 2012 Pearson Education, Inc.

Page 11: Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Grouping (i.e., collecting) objects• Many applications involve collections of objects:

– Personal organizers.– Library catalogs.– Student-record system.

• The number of items to be stored varies.– Items added.– Items deleted.

Page 12: Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Example: A personal notebook• Notes may be stored.• Individual notes can be viewed.• There is no limit to the number of notes.• It will tell how many notes are stored.• Consider a Notebook project.

Page 13: Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

import java.util.ArrayList;

/** * ... */public class Notebook{ // Storage for an arbitrary number of notes. private ArrayList<String> notes;  /** * Perform any initialization required for the * notebook. */ public Notebook() { notes = new ArrayList<String>(); }

...}

import java.util.ArrayList;

/** * ... */public class Notebook{ // Storage for an arbitrary number of notes. private ArrayList<String> notes;  /** * Perform any initialization required for the * notebook. */ public Notebook() { notes = new ArrayList<String>(); }

...}

the type of collection

the type of objects in the collection

Page 14: Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Object structures with collections

Page 15: Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Adding a third note

Page 16: Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Features of the collection• It increases its capacity as necessary.• It keeps a private count (size() method).• It keeps the objects in order.• Details of how all this is done are hidden.

– Does that matter? Does not knowing how prevent us from using it?

Page 17: Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Using the collection

public class Notebook{ private ArrayList<String> notes; ...  public void storeNote(String note) { notes.add(note); }  public int numberOfNotes() { return notes.size(); }

...}

public class Notebook{ private ArrayList<String> notes; ...  public void storeNote(String note) { notes.add(note); }  public int numberOfNotes() { return notes.size(); }

...}

Adding a new note

Returning the number of notes (delegation)

Page 18: Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Index numbering

Page 19: Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.

public void showNote(int noteNumber){ if(noteNumber < 0) { // This is not a valid note number. } else if(noteNumber < numberOfNotes()) { System.out.println(notes.get(noteNumber)); } else { // This is not a valid note number. }}

public void showNote(int noteNumber){ if(noteNumber < 0) { // This is not a valid note number. } else if(noteNumber < numberOfNotes()) { System.out.println(notes.get(noteNumber)); } else { // This is not a valid note number. }}

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Retrieving an object

Index validity checks

Retrieve and print the note

Page 20: Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Removal may affect numberingnotes.remove(1);notes.remove(1);

Page 21: Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Removal may affect numberingnotes.remove(1);notes.remove(1);

Page 22: Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.

For-each Loops• Simplifies repetitive processing of items in a collection

• For example, suppose bookList is an ArrayList<Book> object:

ArrayList<Book> bookList = new ArrayList<Book>();

• The following loop will print each book:

for (Book myBook : bookList) System.out.println (myBook);

Copyright © 2012 Pearson Education, Inc.

Page 23: Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.

Logic of a for-each loop

Statements to process each item

truefalse

Are there more items in the collection?

Page 24: Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.

Quick Check

Copyright © 2012 Pearson Education, Inc.

Write a for-each loop that prints all of the Student objects in an ArrayList<Student> object called roster.

for (Student student : roster)

System.out.println (student);

Page 25: Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.

Other Collection Types• List (e.g., java.util.ArrayList)

– Ordered elements– Allows duplicates

• Set (e.g., java.util.HashSet)– Unordered elements– No duplicates

• Map (e.g. java.util.HashMap)– Unordered elements, stored as key-value pairs– No duplicate keys (can be duplicate values)

Copyright © 2012 Pearson Education, Inc.

Page 26: Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.

Using collections example

Copyright © 2012 Pearson Education, Inc.

Page 27: Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.

Using collections example

Copyright © 2012 Pearson Education, Inc.

Page 28: Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.

Using collections example

Copyright © 2012 Pearson Education, Inc.

Page 29: Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.

Homework• Finish creating the Song class to prep for next

week’s lab• Work on Project 1• CodingBat

Copyright © 2012 Pearson Education, Inc.