nextWord

Category: Programming
Author: Benson Limketkai
Book Chapter: 5.4
Problem: nextWord
(a) (8 points) Write a static method named nextWord that takes a String parameter (one of "rock", "paper" or "scissors") and returns the next word in the sequence. If the word is "scissors", then nextWord returns "rock". The table below shows all possible calls to nextWord. You may assume that no other String will be passed to nextWord.

Call                  | Returns
nextWord("rock");     | "paper"
nextWord("paper");    | "scissors"
nextWord("scissors"); | "rock"

(b) (10 points) Using nextWord, write a static method named rockPaperScissors that takes a number as parameter. The given number represents the maximum number of Strings to print. The method will alternate printing out the Strings "rock", "paper" and "scissors" on successive lines with the first line having one word, the second line having two words, the third line having three words, and so on. The total number of words printed should be no more than the given parameter. No partial lines will be printed, i.e. if printing out the line will cause the number of words to exceed the given number, then the line will not be printed. The sample calls below should make this clear.

rockPaperScissors(5) | rockPaperScissors(6) | rockPaperScissors(17)
rock                 | rock                 | rock
paper scissors       | paper scissors       | paper scissors
                     | rock paper scissors  | rock paper scissors
                     |                      | rock paper scissors rock
                     |                      | paper scissors rock paper scissors

Notice that rockPaperScissors(5) does not print out the third line. Doing so would require printing 3 more words, which would mean 6 words would have been printed--that would exceed the given number 5. Passing 6 prints three lines (as would passing 7, 8, or 9). Passing 10 would add the fourth line.