Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

It has been said that a monkey pushing keys at random on a typewriter (keyboard)

ID: 672081 • Letter: I

Question

It has been said that a monkey pushing keys at random on a typewriter (keyboard) could produce the works of Shakespeare, given enough time. One can simulate this by having a Java program select and display letters at random. Count the number of letters typed until the program produces one of these two-letter words: at, is, he, we, up, or on. When one of these words is produced, stop the program and display the total number of letters typed. Use Math. Random, and format the output 40 characters per line. Output should look similar to below. Sample Run: It took 201 letters to produce a word The letters entered are:

Explanation / Answer

import java.util.*;

class RandomNumbers {
public static void main(String[] args) {


// random integers in [0, 100]
int n= produceRand();
System.out.println(" It took "+ n +" letters to produce a word");
}
public static int produceRand()
{
int c=0;
char ch,prev;
Random t = new Random();
prev = (char)(t.nextInt(26)+'a');
c++;
System.out.print(prev+" ");
while(true)
{
ch=(char)(t.nextInt(26)+'a');
c++;
System.out.print(ch+" ");
if(c%40 == 0)
System.out.println();
if(prev=='a'&&ch=='t')
return c;
if(prev=='h' && ch=='e')
return c;
if(prev=='w'&& ch =='e')
return c;
if(prev=='u' && ch=='p')
return c;
if(prev=='o' && ch=='n')
return c;
prev = ch;
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote