Question: Open the Java Application project created for WK1-exercise. (Included at the very bottom) This project should already have a main class file WK1.java. Add another
Open the Java Application project created for WK1-exercise. (Included at the very bottom) This project should already have a main class file WK1.java. Add another main class file called WK2.java to the project as shown in class. Add the code below toWK2.java.
The following program converts a given string into an ArrayList. For example, the string java is converted into the ArrayList [J, a, v, a]. Your task is to create another string from this ArrayList where the word Java is jumbled. Remember, an ArrayList size changes as you add/remove items from it. I have provided algorithmic guideline for the code.
There are some errors in the code that you have to fix- only the bolded code needs to be changed or moved. No need to change anything else.Optional: Change this program so that it accepts user input (single word of any length) and jumbles it.
import java.util.*;
public class WK2 {
public static String createJumble(String word){
for (int i=0; i < word.length();i++){
//adding the "" to convert the char into a string
//this is a shortcut for converting char/numbers to string
wordList.add(word.charAt(i)+ ""); }
//this should print an ArrayList [J,a,v,a]
System.out.println(wordList);
//Now create a new string where you jumble the word Java
//A sample jumble: aJav
//the following generates a random number between 0-3
//you may need to use it in your code somewhere
//randomNum = (int)(Math.random()*4);
int randomNum = 0;
String jumble="";
//Your code goes here//Create a for loop that loops 4 times
//In the loop, create a random number
//Using the random number, get a letter from wordList
//Add the chosen letter to a string
//Remove this letter from the wordList
//(Think: What should be the range from which the random
//number is chosen? Remember you are removing items from
//the wordList as the loop progresses.)
return jumble;/**What happens when this line is removed? Why?**/ }
public static void main(String[] args) {
/**should wordList be declared here since this is local to this
method? Where should you place this line of code? **/
ArrayList
String w = "Java";
/**how many arguments/inputs does createJumble take?
How many are here? If they mismatch, which one should you
remove from here?**/
String result = createJumble(w, w.length()); }}
WK1 exercise code:
package wk1and2;
import java.util.Arrays; import java.util.Scanner;
public class WK1and2 {
/** * @param args the command line arguments */ public static void main(String[] args) { Scanner in=new Scanner(System.in); System.out.println("Enter the phrase:"); String input=in.nextLine(); String[] phrase=input.split(" "); System.out.println(Arrays.toString(phrase)); //Declare a string variable for acronym String acronym=""; //Declare array for to store first letter of each word char[] charArray=new char[phrase.length]; //Iterate phrase array for(int i=0;i
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
