Question
Implement the classWordGames. It should have one instance variable, aStringcalledword. It holds the word that will be manipulated with the following methods. The methods should
Implement the classWordGames.
It should have one instance variable, aStringcalledword. It holds the word that will be manipulated with the following methods.
The methods should be
- a constructor that takes a word to setword
- public String scramble(): returns a string that scrambles the word by swapping the first half of the word and the last half of the word. For example, "turtle" becomes "tletur". Find the middle of the word by using thelengthof the word and divide by 2. Remember that when you divide two integers, you get an integer. So you won't have to worry about words with an even number of characters. Then usesubstringto divide the word in two halves.
- public String bananaSplit(int insertIdx, String insertText): Returns aStringthat putsinsertTextin the word such that the first character ofinsertTextis at indexinsertIdx. For example, if you want to put "BOO" and index 2 in the word "ghost", this method would return "ghBOOost". Note the index of "B" is 2. Usesubstringand concatenation to create the newString
- public String bananaSplit(String insertChar, String insertText): Returns aStringthat putsinsertTextin the word such that the first character ofinsertTextis at the index of the first occurrence ofinsertChar. For example, if you want to put "BOO" in the word "ghost" andinsertCharis 'o', this method would return "ghBOOost". Note the index of "B" is the same index as the "o" was. Usesubstring,indexOf, and concatenation to create the newString
- public String toString: Returns a string with square brackets aroundword. For example, ifwordis "fun",toStringshould return "[fun]"
here is my work so far:
public class WordGames
{
private String word;
private int idx;
private String word2;
public WordGames(String text)
{
word = text;
}
public WordGames(int insertIdx,String insertText)
{
int idx = insertIdx;
String word2 = insertText;
}
public String scramble()
{
int length = word.length();
int secondHalf = length/2;
String segundaHalf = word.substring(secondHalf,length);
String primerHalf = word.substring(0,secondHalf);
return segundaHalf + primerHalf;
}
public String bananaSplit()
{
String firstHalf = word2.substring(0,idx);
String secondHalf = word2.substring(idx+1,word.length());
return firstHalf+ word2+ secondHalf;
}
public String bananaSplit(String insertChar, String insertText)
{
// Insert insertText after the first
// occurence of the insertChar
}
public String toString()
{
// Games[word]
}
}
import java.util.Scanner;
public class WordGameTester
{
public static void main(String[] args)
{
System.out.println("Please enter a word:");
Scanner input = new Scanner(System.in);
String word = input.nextLine();
WordGames scram = new WordGames(word);
System.out.println(scram.scramble());
// Ask for a word
// Scramble it
// Print out scrambled wor
System.out.println("Please enter a random integer: ");
int whore= input.nextInt();
System.out.println("Please enter another word: ");
String joeMama = input.nextLine();
WordGames kola = new WordGames(whore,joeMama);
System.out.println(kola.bananaSplit());
// Ask for an index
// Ask for random word
// Add random word at index
// Print out the word
// Ask for a character (store as a String)
// Ask for random word
// Add random word at character
// Print out the word
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started