Objectives Explain the algorithms to be implemented in the Elevens L ab Activity 3 Revise and finish implementing the algorithms Background information Activity 3 Shuffling Perfect shuffle ID: 401678
Download Presentation The PPT/PDF document "Elevens Lab" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Elevens LabSlide2
Objectives
Explain the algorithms to be implemented in the Elevens
L
ab – Activity 3
Revise and finish implementing the algorithmsSlide3
Background informationSlide4
Activity 3: Shuffling
Perfect
shuffleSlide5
Algorithm
Initialize shuffled to contain 52 “empty” elements.
Set
k to 0.
For j = 0 to 25,
−
Copy cards[j] to shuffled[k];
−
Set k to k+2.
Set
k to 1.
For j = 26 to 51,
−
Copy cards[j] to shuffled[k];
−
Set k to k+2.
To cover both and even and odd number of elements: The first
loop j must go up to (
cards.length
+ 1) / 2, exclusive, and in the second loop j most begin at (
cards.length
+ 1) / 2.
Note: rounding up is requiredSlide6
Selection Shuffle
First attempt
Initialize
shuffled to contain 52 “empty” elements.
Then
for k = 0 to 51,
−
Repeatedly generate a random integer j between 0 and 51,
inclusive
until cards[j] contains a card (not marked as empty);
−
Copy cards[j] to shuffled[k];
−
Set cards[j] to empty.
“Efficient” selection shuffle
For
k = 51 down to 1,
−
Generate a random integer r between 0 and k, inclusive; −
- Exchange
cards[k] and cards[r]. Slide7
Math.random()
Generates a random double value greater than or equal to 0.0 and less than 1.0
0<=
Math.random
() <1.0
(
int
)(
Math.random
() *10) Returns a random integer between 0 - 9
50 + (
int
)(
Math.random
() *50) Returns a random integer between 50 -99
a+
Math.random
() * b Returns a random integer between a-
a+b
, excluding
a+bSlide8
example
{A, B, C, D}
k
=
r_number
=
{ }
{ }
{ }Slide9
Flip()
Simulated flip of a weighted coin.
Is twice as likely to turn up “heads” as “tails.”
Slide10
arePermutations()
If two, same size arrays, have the same elements, but in different order. Return true if they do or false if they don
’
t.
Look for every element of array1 in array2. If one element is not found, return false otherwise finish searching and return true.Slide11
Overload vs. Override
Both based on polymorphism
Same name, but different signature (note: the return type is not part of the signature
Checks for the correct signature at compile time (uses static binding)
Method exists in the same class, but with different signature
Signature is the same
Resolved at runtime (dynamic binding)
Private, static and final methods cannot be
overriden
Methods with the same signature exists in derived classes.