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

Please let with the following request. There are three parts to this request and

ID: 3913272 • Letter: P

Question

Please let with the following request. There are three parts to this request and I will Bold the start of each section to diviide each section. The first is the prompt of the request, the second is the code I am working on, and the last section is the code that I am using to fill in the needed requirements.

Section 1 Prompt

Goal
• Learn how to use classes and object in Java to simplify implementation and facilitate reuse.
Problem
News of the power of the Scramble program has gotten out and many teams would like to make use of it.
We are being asked to prepare scramble(), removeCharAt(), and isPalindrome() so that they can be used in
other Java programs.
User Story
The “user” in this case is a developer wanting to access the scramble() functionality. The reuse capability in
Java is the class. In a project, prepare a class so that the scramble() and other “word” related functionality so
it can be reused.
Tasks
You are to place this in an executable Java project which is why it is only “prepared” for reuse. To facilitate
its reuse, you will need to put it in a NetBeans project by itself. We won’t do that here.
1. Create a new project called Scramble3.
2. Copy and paste this into Scramble3.java file in the project.

/*
* Put your header here.
*/

package scramble3;

import java.util.Scanner;

public class Scramble3 {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String input_text;
while (true) {
System.out.print("Enter word: ");
input_text = input.next();
if (input_text.equals("0")) {
break;
}
Word in_word = new Word(input_text);
System.out.println(in_word.text() + " scrambled is " + in_word.scramble());
if (in_word.isPalindrome()) {
System.out.println(in_word.text() + " is a palindrome");
} else {
System.out.println(in_word.text() + " is not a palindrome");
}
}
}
}
}

3. Create a file in the project called Word.java. To create this file use File > New File. Make sure you

create the file in the Scramble3 project and make sure Category is “Java” and File Type is “Java
Class”.
4. Copy and paste the following into the Word.java file you just created in the Scramble3 project.

/*
* Put your header here.
*/
package scramble3;
public class Word {
private String _word;
// Put removeCharAt() here. It should be a “class” method.
// Copy the very method from your last project.
// Put what the name of what this particular method is called here.
public Word(String word) {
this._word = word;
}
// Implement text() method here. It simply returns _word.
// Put scramble() here.
// Put isPalindrome() here. Get this from Palin4.java in Examples, but
// note that you will need to wrap it in a function and return a value.
}

5. Go through each of the bolded comments above and implement the methods correctly for the
functions that you have implemented in previous projects.
Checklist
Use the following as guidance for getting an 8 on this project:
• Correctly added removeCharAt() to Word class. (2 points)
• Correctly named the identified method. (1 point)
• Correctly implemented text() method on Word class. (1 point)
• Correctly added scramble() to Word class. (2 points)
• Correctly added isPalindrome to Word class. (2 points)

Section 2 Word.java

package scramble3;

public class Word {

private String _word;
// Put removeCharAt() here. It should be a “class” method.
// Copy the very method from your last project.
public static String removeCharAt(String word, int i) {
if (i == 0) {
return word.substring(1);
}
if (i == word.length() - 1) {
return word.substring(0, word.length() - 1);
}
String s = word.substring(0, i) + word.substring(i + 1);
return s;
}
// Constructur

public Word(String word) {
this._word = word;
}
// Implement text() method here. It simply returns _word.
// Put scramble() here.
// Put isPalindrome() here. Get this from Palin4.java in Examples, but
// note that you will need to wrap it in a function and return a value.
boolean is_palin = true;
int j = word.length() - 1;
for (int i = 0; i < word.length() / 2; i++) {
if (word.charAt(i) != word.charAt(j)) {
is_palin = false;
}
--j;
}
}

Section 3 Code Used to fill in whats needed for Word.java

package scramble.pkg2;
import java.util.Scanner;

public class Scramble2 {
static boolean TESTIT = false;

public static String removeCharAt(String word, int i) {
if (i == 0) {
return word.substring(1);
}
if (i == word.length() - 1) {
return word.substring(0, word.length() - 1);
}
String s = word.substring(0, i) + word.substring(i + 1);
return s;
}
public static String scramble(String word) {
String scram_word = "";
int i;
while (word.length() > 0) {
i = (int) (Math.random() * word.length());
scram_word = scram_word + word.charAt(i);
word = removeCharAt(word, i);
}
scram_word = scram_word.toUpperCase();
return scram_word;
}
public static int getConsonant(String scram_word) {
/*
* This method returns index of first consonant in the given string
* Returns -1 if no consonant is present in the word
* */
for (int j = 0; j < scram_word.length(); j++) {
if (scram_word.charAt(j) != 'A' && scram_word.charAt(j) != 'E' && scram_word.charAt(j) != 'I'
&& scram_word.charAt(j) != 'O' && scram_word.charAt(j) != 'U') {
return j;
}
}
return -1;
}
public static int getVowel(String scram_word) {
/*
* This method returns index of first vowel in the given string
* Returns -1 if no vowel is present in the word
* */
for (int j = 0; j < scram_word.length(); j++) {
if (scram_word.charAt(j) == 'A' || scram_word.charAt(j) == 'E' || scram_word.charAt(j) == 'I'
|| scram_word.charAt(j) == 'O' || scram_word.charAt(j) == 'U') {
return j;
}
}
return -1;
}
public static String getFake(String scram_word) {
String fake_word = ""; //fake word

//get consonants and vowels
int j;
while (scram_word.length() > 0) {
int index = getConsonant(scram_word);
//add consonant to fake word
if (index != -1) {
fake_word = fake_word + scram_word.charAt(index);
scram_word = removeCharAt(scram_word, index);
}
index = getVowel(scram_word);
//add vowel to fake word
if (index != -1) {
fake_word = fake_word + scram_word.charAt(index);
scram_word = removeCharAt(scram_word, index);
}
}

return fake_word;
}
public static int test() {
int errors = 0;
String word = "break";
String scram_word = scramble(word);
System.out.println(word + " scrambled is '" + scram_word + "'");

return errors;
}

public static void main(String[] args) {

if (TESTIT) {
int errors = test();
System.exit(errors);
}
Scanner input = new Scanner(System.in);
System.out.print("Enter a word: ");
String word = input.next();

while (!word.equals("0")) {
if (word.length() == 5) {
String scram_word = scramble(word);
System.out.println("Scramble : " + scram_word);

System.out.println("Fake Word : " + getFake(scram_word));
} else {
System.out.println("Enter a 5 letter word ");
}
System.out.print("Enter a word: or 0 to exit ");

word = input.next();

}
}
}

Thank you for assisting me with this project.

Explanation / Answer

hmm i didn't understood why you added additional methods to word class.. according to the criteria of your assignment grading:(apart from 'identified function' which i guess from prev assignment)

please notify if any correction is needed

//Word.java

import java.lang.Math;

import java.lang.String;

import java.util.ArrayList;

import java.util.List;

import java.lang.System;

public class Word {

private String _word;

void removeCharAt(int pos) //removing character at position

{

this._word=_word.substring(0, pos) + _word.substring(pos + 1);

}

public Word(String word) {

this._word = word;

}

String text()

{

return this._word;

}

String scramble() //returning scrambled string

{

List<Character> characters = new ArrayList<Character>();

for(char c:_word.toCharArray()){

characters.add(c);

}

StringBuilder output = new StringBuilder(_word.length());

while(characters.size()!=0){

int randPicker = (int)(Math.random()*characters.size());

output.append(characters.remove(randPicker));

}

return output.toString();

}

boolean isPalindrome()

{

String s=this._word;

StringBuffer sb=new StringBuffer(s);

if(sb.reverse().equals(sb))

return true;

return false;

}

}