Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update DefiningMethods.ptx #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion source/Introduction/DefiningMethods.ptx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public class MySquareRoot {

<exercises>
<title>Self Check</title>
<p>Here's a self check that really covers everything so far. You may have heard of the infinite monkey theorem? The theorem states that a monkey hitting keys at random on a typewriter keyboard for an infinite amount of time will almost surely type a given text, such as the complete works of William Shakespeare. Well, suppose we replace a monkey with a Java metho. How long do you think it would take for a Java method to generate just one sentence of Shakespeare? The sentence we'll shoot for is: <q>methinks it is like a weasel</q></p>
<p>Here's a self check that really covers everything so far. You may have heard of the infinite monkey theorem? The theorem states that a monkey hitting keys at random on a typewriter keyboard for an infinite amount of time will almost surely type a given text, such as the complete works of William Shakespeare. Well, suppose we replace a monkey with a Java method. How long do you think it would take for a Java method to generate just one sentence of Shakespeare? The sentence we'll shoot for is: <q>methinks it is like a weasel</q></p>
<p>You're not going to want to run this one in jshell, so fire up your favorite Java IDE. The way we'll simulate this is to write a function that generates a string that is 28 characters long by choosing random letters from the 26 letters in the alphabet plus the space. We'll write another function that will score each generated string by comparing the randomly generated string to the goal.</p>
<p>A third function will repeatedly call generate and score, then if 100% of the letters are correct we are done. If the letters are not correct then we will generate a whole new string.To make it easier to follow your program's progress this third function should print out the best string generated so far and its score every 1000 tries.</p>
<p>In order to do this program, you will need to know how to generate random numbers. The following program generates ten random numbers in the range 0 up to (but not including) 100:</p>
Expand Down