Question: Help with Java! Reading a program Read both classes below (PuzzlePage and WordScramble). Be the computer and trace which line of code is executing in

Help with Java!

Reading a program

Read both classes below (PuzzlePage and WordScramble). Be the computer and trace which line of code is executing in order. Keep track of what the current state of each variable is.

import java.util.Scanner;

/**********************************

* PuzzlePage.java

* Creates word puzzles for the Triangle

* @author Tammy Pirmann

* @version 20210407

*********************************/

public class PuzzlePage {

public static void main(String args[]){

Scanner keyboard = new Scanner(System.in);

WordScramble puzzle = new WordScramble();

System.out.print("Enter the solution word: ");

String solution = keyboard.nextLine();

System.out.printf("Your puzzle is: %s", puzzle.scramble(solution));

}

}

/*******************************

* WordScramble.java, Scrambles a given word

* @author Tammy Pirmann

* @version 20210407

*******************************/

public class WordScramble {

private String solution;

//constructor

public WordScramble() {

solution = "NA";

}

//Setter

public void setSolution(String string){

solution = string.toUpperCase();

}

//Scrambles the solution String

public String scramble(String string){

setSolution(string);

String mix;

int a = solution.indexOf("A");

if (a >= 0) {

mix = solution.substring(a).concat(solution.substring(0, a));

}

int e = solution.indexOf("E");

if (e >= 0) {

mix = solution.substring(e).concat(solution.substring(0, e));

}

int i = solution.indexOf("I");

if (i >= 0) {

mix = solution.substring(i).concat(solution.substring(0, i));

}

int oh = solution.indexOf("O");

if (oh >= 0) {

mix = solution.substring(oh).concat(solution.substring(0, oh));

}

int u = solution.indexOf("U");

if (u >= 0) {

mix = solution.substring(u).concat(solution.substring(0, u));

}

//reverse it in case it still looks like the original word

return reverse(mix);

}

//helper method to reverse the scrambled string

private String reverse(String string){

//base case

if (string.isEmpty()){

return string;

}

//Recursive call

return reverseString(string.substring(1)) + string.charAt(0);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!