urce | | History | .\' * Plays random triplets of consecutive words with only a
ID: 3881469 • Letter: U
Question
urce | | History | .' * Plays random triplets of consecutive words with only a 100 millisecond * pause between the three words, with a four hundred millisecond pause * between triplets Ex: for ,this is a test: a triplet would only be ·this is a. * or is a test 0 1 32 * eparam numTriplets the number of triplets to play * @throws InterruptedException 84 public void playTriplets int numTriplets) throws InterruptedException 85| | String s ="this is a random string" ; 86 String arrayI1-s.split")splits string into words 87 Random randnew Random)//create random generator 88 int count 1 while(countcnumDoublets) 190 191 192 int startIndexarand.nextInt(array. length-1) System.out.print(arraylstartIndex]+") Thread .current Thread().sleep (100); 100 | 194 //pause ms 195 System.out.print ln(array IstartIndex+1): 196 Thread.currentThread).sleep(400)://pause 400 ms 198 199 count++Explanation / Answer
public class Triplet {
public static void main(String[] args) throws InterruptedException {
playTriplets(3);
}
public static void playTriplets(int numTriplets) throws InterruptedException {
String s = "This is a test";
String array[] = s.split(" ");
Random rand = new Random();
int count =1;
while(count <= numTriplets) {
int start = rand.nextInt(array.length-2);
System.out.print(array[start]+" ");
Thread.currentThread().sleep(400);
System.out.print(array[start+1]+" ");
Thread.currentThread().sleep(400);
System.out.print(array[start+2]+" ");
Thread.currentThread().sleep(400);
count++;
System.out.println();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.