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

FOCUS TOPICS String manipulation TASK Your task is to build a palindrome from an

ID: 3869769 • Letter: F

Question

FOCUS TOPICS String manipulation TASK Your task is to build a palindrome from an input string. A palindrome is a word that reads the same backward or forward. Your code will take the first 5 characters of the user input, and create a 9- character palindrome from it. Words shorter than 5 characters will result in a runtime error when you run your code. This is acceptable for this exercise – we will cover input validation in a later class. Some examples of input words and the resulting palindromes: Input Palindrome sixty sixtytxis tomorrow tomoromot futurama futurutuf january januaunaj june Runtime Error – word is too short As input, prompt the user enter a word, which should be at least 5 characters long. As output, print the user-entered word, the length of that word, and the palindrome you created based on that word. In your code, use only the following String class methods: concat(stringToAdd) length() substring(startIndex, endIndex) As you work through the instructions below, refer to slides 23, 24, and 25 from today’s class material for help with character indexing and the specifics on how the String methods work. Instructions: 1. Create a new NetBeans project called yourname_lab3 2. Prompt the user to enter a word that is at least 5 characters long. 3. Read the input from the keyboard, and store it in a String variable. o Hint: Use the nextLine() method of the Scanner class. o Hint: Don’t forget the import statement! 4. Create a new String variable to represent the base of the output palindrome. Use the substring method to get the first five characters of the input word, and store this String value in the base variable. 5. Create four new String variables to represent the first four letters in the input word. Use the substring method to get a single-character String from the input word for each of these four letters, and store these Strings in the variables you created. 6. Create a new String variable to store the final palindrome. Concatenate the base and the letters together to form the final palindrome, and store it in your final palindrome variable. 7. Following the sample output below, report the following information, in the order below: o The original input word o The length of the original input word o The new palindrome

Explanation / Answer

GeneratePalindrome.java

import java.util.Scanner;

public class GeneratePalindrome {

public static void main(String[] args) {

// Declaring variables
String word, finalPalindrome, base, first, second, third, four;

/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);

// Getting the input entered by the user

/*
* This while loop continues to execute until the user enters a valid
* string
*/
while (true) {
// Getting the input entered by the user
System.out.print("Enter word :");
word = sc.nextLine();

/*
* If the length of the word is less than 5 characters then display
* error message
*/
if (word.length() < 5) {
System.out.println("** word is too short **");
continue;
} else
break;
}

// finding the base String
base = word.substring(0, 5);

// Getting first,second,third,forth characters
first = word.substring(0, 1);
second = word.substring(1, 2);
third = word.substring(2, 3);
four = word.substring(3, 4);

// Performing concatenation
finalPalindrome = base.concat(four).concat(third).concat(second)
.concat(first);

// Displaying the results
System.out.println("The original input word :" + word);
System.out.println("The length of the original input word :" + word.length());

System.out.println("The New Palindrome :" + finalPalindrome);

}

}

___________________

Output:

Enter word :tomorrow
The original input word :tomorrow
The length of the original input word :8
The New Palindrome :tomoromot

_____________Could you rate me well.Plz .Thank You