3_ob85_meet

.docx

School

Drexel University *

*We aren’t endorsed by this school

Course

540

Subject

Computer Science

Date

May 1, 2024

Type

docx

Pages

4

Uploaded by bbbtobi17 on coursehero.com

Tobi Bakare 04/19/2024 Week3Meet - 10 pts Turn in on BBL as soon as complete, but before end of day Sunday following the lecture. =============================== Reading a program Read both programs. 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 str){ solution = str.toUpperCase(); } //Scrambles the solution String public String scramble(String str){ setSolution(str); 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 str){ //base case if (str.isEmpty()){ return str; } //Recursive call return reverseString(str.substring(1)) + str.charAt(0); } }
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help