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

Line 28 of Figure 11-6 seems a bit awkward. In general, it is clearer to say if

ID: 3567865 • Letter: L

Question

Line 28 of Figure 11-6 seems a bit awkward. In general, it is clearer to say if (x == 1) { // do one thing } else { // do another thing } than if (x != 1) { // do another thing } else { // do one thing } which is equivalent. We might therefore be tempted to change the line to if (spaceIndex == -1) { and to swap lines 29-38 with lines 40-45. Why does this not work?

Figure 11-6

1 /** Play until someone gets five words. */

2 public void play() {

3     while (true) {

4       System.out.println("PLAYER 1 TO PLAY");

5       playTurn(words1, words2);

6       if (words1.size() == 5) {

7          System.out.println("Player 1 wins!");

8          return;

9        }

10     System.out.println("PLAYER 2 TO PLAY");

11     playTurn(words2, words1);

Explanation / Answer

the return of the indexof function is -1 ; which you have written currectly...but that will return only when you give it some input parameters...but you gave an empty string as input so it can't look for an empty string in a whole string for it's first occurence !! so change it accordingly..and it will work

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