One Armed Bandit source: http://dogbeforewicket.blogspot.ca EECS - - PowerPoint PPT Presentation

one armed bandit
SMART_READER_LITE
LIVE PREVIEW

One Armed Bandit source: http://dogbeforewicket.blogspot.ca EECS - - PowerPoint PPT Presentation

One Armed Bandit source: http://dogbeforewicket.blogspot.ca EECS 1030 moodle.yorku.ca One Armed Bandit Utility /** * Returns the winnings from one pull of the one armed * bandit. * * @param coin the coin deposited in the one armed bandit.


slide-1
SLIDE 1

One Armed Bandit

source: http://dogbeforewicket.blogspot.ca moodle.yorku.ca EECS 1030

slide-2
SLIDE 2

One Armed Bandit Utility

/** * Returns the winnings from one pull of the one armed * bandit. * * @param coin the coin deposited in the one armed bandit. * @return the payoff from one pull of the lever. */ public static List<Coin> pull(Coin coin)

moodle.yorku.ca EECS 1030

slide-3
SLIDE 3

Casino App

List<Coin> wallet = new ArrayList<Coin>(); wallet.add(new Coin()); final int TRIES = 10; int tries = 0; while (tries < TRIES && !wallet.isEmpty()) { Coin coin = wallet.remove(0); List<Coin> winnings = OneArmedBandit.pull(coin); wallet.addAll(winnings); tries++; } System.out.printf("After %d tries, %d coins left%n", tries, wallet.size());

moodle.yorku.ca EECS 1030

slide-4
SLIDE 4

The Coin Class

Problem Implement the Coin class. We only need to be able to create Coin

  • bjects, using the default constructor. We do not need any

methods of the Coin class.

moodle.yorku.ca EECS 1030

slide-5
SLIDE 5

The Coin Class

Problem Implement the Coin class. We only need to be able to create Coin

  • bjects, using the default constructor. We do not need any

methods of the Coin class. Now that we have the Coin class, we can run the Casino app.

moodle.yorku.ca EECS 1030

slide-6
SLIDE 6

How Many Coins?

Question You win 37 coins in 10 tries. How many Coin objects are there stored in memory?

moodle.yorku.ca EECS 1030

slide-7
SLIDE 7

How Many Coins?

Question You win 37 coins in 10 tries. How many Coin objects are there stored in memory? Answer At least 37.

moodle.yorku.ca EECS 1030

slide-8
SLIDE 8

How Many Coins?

Question You win 37 coins in 10 tries. How many Coin objects are there stored in memory? Answer At least 37. Question Instead of storing Coin objects in your wallet, could you store aliases of a single Coin object in your wallet?

moodle.yorku.ca EECS 1030

slide-9
SLIDE 9

How Many Coins?

Question You win 37 coins in 10 tries. How many Coin objects are there stored in memory? Answer At least 37. Question Instead of storing Coin objects in your wallet, could you store aliases of a single Coin object in your wallet? Answer Yes.

moodle.yorku.ca EECS 1030

slide-10
SLIDE 10

A Single Coin

Problem Modify the Coin class so that the client can create at most one Coin object.

moodle.yorku.ca EECS 1030

slide-11
SLIDE 11

A Single Coin

Question Can the client use a constructor to create the Coin object?

moodle.yorku.ca EECS 1030

slide-12
SLIDE 12

A Single Coin

Question Can the client use a constructor to create the Coin object? Answer No.

moodle.yorku.ca EECS 1030

slide-13
SLIDE 13

A Single Coin

Question Can the client use a constructor to create the Coin object? Answer No. Question Why not?

moodle.yorku.ca EECS 1030

slide-14
SLIDE 14

A Single Coin

Question Can the client use a constructor to create the Coin object? Answer No. Question Why not? Answer If we provide a public constructor, clients can invoke it as many times as they want and, hence, create as many Coin objects as they want.

moodle.yorku.ca EECS 1030

slide-15
SLIDE 15

A Single Coin

Question Since the client cannot use a constructor, what other options does the client have?

moodle.yorku.ca EECS 1030

slide-16
SLIDE 16

A Single Coin

Question Since the client cannot use a constructor, what other options does the client have? Answer Methods.

moodle.yorku.ca EECS 1030

slide-17
SLIDE 17

A Single Coin

Question Since the client cannot use a constructor, what other options does the client have? Answer Methods. Question Can the method be non-static?

moodle.yorku.ca EECS 1030

slide-18
SLIDE 18

A Single Coin

Question Since the client cannot use a constructor, what other options does the client have? Answer Methods. Question Can the method be non-static? Answer No, because you would need a Coin object to invoke it on (and we are trying to create a Coin object). So the method is static.

moodle.yorku.ca EECS 1030

slide-19
SLIDE 19

A Single Coin

Question What is the return type of this static method?

moodle.yorku.ca EECS 1030

slide-20
SLIDE 20

A Single Coin

Question What is the return type of this static method? Answer Coin.

moodle.yorku.ca EECS 1030

slide-21
SLIDE 21

A Single Coin

Question What is the return type of this static method? Answer Coin. Question Does it have any parameters?

moodle.yorku.ca EECS 1030

slide-22
SLIDE 22

A Single Coin

Question What is the return type of this static method? Answer Coin. Question Does it have any parameters? Answer No.

moodle.yorku.ca EECS 1030

slide-23
SLIDE 23

A Single Coin

public static Coin getInstance() Question The method name suggests that the Coin class has a static

  • attribute. What is its name and type?

moodle.yorku.ca EECS 1030

slide-24
SLIDE 24

A Single Coin

public static Coin getInstance() Question The method name suggests that the Coin class has a static

  • attribute. What is its name and type?

Answer instance and Coin.

moodle.yorku.ca EECS 1030

slide-25
SLIDE 25

A Single Coin

Question Where do we initialize the attribute?

moodle.yorku.ca EECS 1030

slide-26
SLIDE 26

A Single Coin

Question Where do we initialize the attribute? Answer In the declaration.

moodle.yorku.ca EECS 1030

slide-27
SLIDE 27

A Single Coin

Question Where do we initialize the attribute? Answer In the declaration. Question How?

moodle.yorku.ca EECS 1030

slide-28
SLIDE 28

A Single Coin

Question Where do we initialize the attribute? Answer In the declaration. Question How? Answer private static Coin instance = new Coin();

moodle.yorku.ca EECS 1030

slide-29
SLIDE 29

A Single Coin

Question But as we argued earlier, we cannot provide a public constructor. So, how can we use new Coin() in the Coin class?

moodle.yorku.ca EECS 1030

slide-30
SLIDE 30

A Single Coin

Question But as we argued earlier, we cannot provide a public constructor. So, how can we use new Coin() in the Coin class? Answer By adding a private default constructor.

moodle.yorku.ca EECS 1030

slide-31
SLIDE 31

A Single Coin

Question But as we argued earlier, we cannot provide a public constructor. So, how can we use new Coin() in the Coin class? Answer By adding a private default constructor. Question Now that we have declared and initialized the instance attribute, how do we implement the getInstance method?

moodle.yorku.ca EECS 1030

slide-32
SLIDE 32

A Single Coin

Question But as we argued earlier, we cannot provide a public constructor. So, how can we use new Coin() in the Coin class? Answer By adding a private default constructor. Question Now that we have declared and initialized the instance attribute, how do we implement the getInstance method? Answer return Coin.instance;

moodle.yorku.ca EECS 1030

slide-33
SLIDE 33

Singleton Design Pattern

The pattern to ensure that at most one object of a particular class can be created is known as the singleton design pattern. The example we presented is contrived. In case object represent physical entities, such as a connection to a database, the singleton design pattern comes in handy.

moodle.yorku.ca EECS 1030

slide-34
SLIDE 34

Coin with Values

Problem Modify the Coin class some that each coin has a value (of type int). Make the class immutable.

moodle.yorku.ca EECS 1030

slide-35
SLIDE 35

Coin with Values

Problem Modify the Coin class so that the client can create at most one Coin object for each value. That is, the client can create different Coin objects but only if they all have different values.

moodle.yorku.ca EECS 1030

slide-36
SLIDE 36

Coin with Values

Question Can the client use a constructor to create the Coin object?

moodle.yorku.ca EECS 1030

slide-37
SLIDE 37

Coin with Values

Question Can the client use a constructor to create the Coin object? Answer No.

moodle.yorku.ca EECS 1030

slide-38
SLIDE 38

Coin with Values

Question Can the client use a constructor to create the Coin object? Answer No. Question Why not?

moodle.yorku.ca EECS 1030

slide-39
SLIDE 39

Coin with Values

Question Can the client use a constructor to create the Coin object? Answer No. Question Why not? Answer If we provide a public constructor, clients can invoke it as many times as they want and, hence, create as many Coin objects with the same value as they want.

moodle.yorku.ca EECS 1030

slide-40
SLIDE 40

Coin with Values

Question Since the client cannot use a constructor, what other options does the client have?

moodle.yorku.ca EECS 1030

slide-41
SLIDE 41

Coin with Values

Question Since the client cannot use a constructor, what other options does the client have? Answer Methods.

moodle.yorku.ca EECS 1030

slide-42
SLIDE 42

Coin with Values

Question Since the client cannot use a constructor, what other options does the client have? Answer Methods. Question Can the method be non-static?

moodle.yorku.ca EECS 1030

slide-43
SLIDE 43

Coin with Values

Question Since the client cannot use a constructor, what other options does the client have? Answer Methods. Question Can the method be non-static? Answer No, because you would need a Coin object to invoke it on (and we are trying to create a Coin object). So the method is static.

moodle.yorku.ca EECS 1030

slide-44
SLIDE 44

Coin with Values

Question What is the return type of this static method?

moodle.yorku.ca EECS 1030

slide-45
SLIDE 45

Coin with Values

Question What is the return type of this static method? Answer Coin.

moodle.yorku.ca EECS 1030

slide-46
SLIDE 46

Coin with Values

Question What is the return type of this static method? Answer Coin. Question Does it have any parameters?

moodle.yorku.ca EECS 1030

slide-47
SLIDE 47

Coin with Values

Question What is the return type of this static method? Answer Coin. Question Does it have any parameters? Answer Yes, the value of the coin.

moodle.yorku.ca EECS 1030

slide-48
SLIDE 48

Coin with Values

public static Coin getInstance(int value)

moodle.yorku.ca EECS 1030

slide-49
SLIDE 49

Coin with Values

public static Coin getInstance(int value) Question For each value, we have to store a Coin. How can we do that?

moodle.yorku.ca EECS 1030

slide-50
SLIDE 50

Coin with Values

public static Coin getInstance(int value) Question For each value, we have to store a Coin. How can we do that? Answer private static Map<Integer, coin> instance

moodle.yorku.ca EECS 1030

slide-51
SLIDE 51

Coin with Values

Question How do we initialize the attribute?

moodle.yorku.ca EECS 1030

slide-52
SLIDE 52

Coin with Values

Question How do we initialize the attribute? Answer private static Map<Integer, coin> instance = new HashMap<Integer, Coin>();

moodle.yorku.ca EECS 1030

slide-53
SLIDE 53

Coin with Values

Question How do we implement the getInstance method?

moodle.yorku.ca EECS 1030

slide-54
SLIDE 54

Coin with Values

Question How do we implement the getInstance method? Answer if (!instance.containsKey(value)) { Coin.instance.put(value, new Coin(value)); } return Coin.instance.get(value);

moodle.yorku.ca EECS 1030

slide-55
SLIDE 55

Multiton Design Pattern

The pattern to ensure that at most one object with a particular state can be created is known as the multiton design pattern. Immutable classes such as String and Integer implement this design pattern.

moodle.yorku.ca EECS 1030

slide-56
SLIDE 56

Concurrency

“Doing multiple things at the same time”

moodle.yorku.ca EECS 1030

slide-57
SLIDE 57

Concurrency

Question What happens when we execute public static Coin getInstance() { if (Coin.instance == null) { Coin.instance = new Coin(); } return Coin.instance; } twice but at the same time?

moodle.yorku.ca EECS 1030

slide-58
SLIDE 58

Java Bytecode

getstatic Coin.instance // get the value of the static attribute ifnonnull 6 // if the value is not null, go to line 6 new Coin // create a new Coin object ... putstatic Coin.instance // set the value of the static attribute getstatic Coin.instance // get the value of the static attribute areturn

moodle.yorku.ca EECS 1030