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

TASK Your task is to build a palindrome from an input string. A palindrome is a

ID: 3882113 • Letter: T

Question

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 8. When your code is working, upload your .java file to the Module 3 lab dropbox.

Explanation / Answer


Given below is the code for the question. Please rename the class and file according to your name as mentioned in the question.
Do rate the answer if it was helpful. Thank you
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i

Lab3.java
==========
import java.util.Scanner;
public class Lab3 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String str, palindrome;
String base;
String letter1, letter2, letter3, letter4;
System.out.println("Enter a word with atleast 5 characters:");
str = keyboard.nextLine();
base = str.substring(0, 5); //get the first 5 chars
letter1 = base.substring(0, 1); //extract the 1st char
letter2 = base.substring(1, 2); //extract the 2nd char
letter3 = base.substring(2, 3); //extract the 3rd char
letter4 = base.substring(3, 4); //extract the 4th char
palindrome = base.concat(letter4);
palindrome = palindrome.concat(letter3);
palindrome = palindrome.concat(letter2);
palindrome = palindrome.concat(letter1);
System.out.println("Your input was:" + str);
System.out.println("Length of your input is:" + str.length());
System.out.println("Palindrome is:" + palindrome);
}
}

output
=====
Enter a word with atleast 5 characters:
sixty
Your input was:sixty
Length of your input is:5
Palindrome is:sixtytxis



Enter a word with atleast 5 characters:
january
Your input was:january
Length of your input is:7
Palindrome is:januaunaj