Question

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

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1


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

Add a comment
Know the answer?
Add Answer to:
TASK Your task is to build a palindrome from an input string. A palindrome is a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In the first task, you will write a Java program that accepts a string of the...

    In the first task, you will write a Java program that accepts a string of the format specified next and calculate the answer based on the user input. The input string should be of the format dddxxdddxx*, where d represents a digit and x represents any character and asterisk at the end represents that the string can have any number of characters at the end. 1. Prompt the user to enter a string with the specific format (dddxxdddxx*) 2. Read...

  • Write a C++ program to check whether a string is a palindrome. Input a string (word)...

    Write a C++ program to check whether a string is a palindrome. Input a string (word) from the keyboard. Assume the letters are case-insenstive. Use the C++ string class. Refer to the sample output below. Sample Runs: Enter a string: otto; otto is a palindrome Enter a string: A A is a palindrome Enter a string: heooh heooh is not a palindrome

  • Palindrome Detector C++ A palindrome is any word, phrase, or sentence that reads the same forward...

    Palindrome Detector C++ A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here are some well-known palindromes: Able was I, ere I saw Elba A man, a plan, a canal, Panama Desserts, I stressed Kayak Write a program that determine whether an input string is a palindrome or not. Input: The program should prompt the user "Please enter a string to test for palindrome or type QUIT to exit: " and then wait for...

  • A palindrome is a word, phrase, or sequence that reads the same backward as forward, e.g.,...

    A palindrome is a word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run. In this program, ask the user to input some text and print out whether or not that text is a palindrome. Create the Boolean method isPalindrome which determines if a String is a palindrome, which means it is the same forwards and backwards. It should return a boolean of whether or not it was a palindrome. Create the method reverse...

  • Prompt the user and read in a string. Make your string large enough to hold 100...

    Prompt the user and read in a string. Make your string large enough to hold 100 characters. 2. Count the number of words in the string. A word is one or more non-blank characters separated by one or more blanks. My suggestion is to use a flag (a boolean variable) to indicate whether or not you are in a word. Then you know you have found a word when the flag indicates that you are in a word and the...

  • A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here...

    A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here are some well-known palindromes: Able was I, ere I saw Elba A man, a plan, a canal, Panama Desserts, I stressed Kayak Write a program that determine whether an input string is a palindrome or not. Input: The program should prompt the user "Please enter a string to test for palindrome or type QUIT to exit: " and then wait for the user input....

  • Your program must prompt the user to enter a string. The program must then test the...

    Your program must prompt the user to enter a string. The program must then test the string entered by the user to determine whether it is a palindrome. A palindrome is a string that reads the same backwards and forwards, such as "radar", "racecar", and "able was I ere I saw elba". It is customary to ignore spaces, punctuation, and capitalization when looking for palindromes. For example, "A man, a plan, a canal. Panama!" is considered to be a palindrome....

  • package _solution; /** This program demonstrates how numeric types and operators behave in Java Do Task...

    package _solution; /** This program demonstrates how numeric types and operators behave in Java Do Task #1 before adding Task#2 where indicated. */ public class NumericTypesOriginal { public static void main (String [] args) { //TASK #2 Create a Scanner object here //identifier declarations final int NUMBER = 2 ; // number of scores int score1 = 100; // first test score int score2 = 95; // second test score final int BOILING_IN_F = 212; // boiling temperature double fToC;...

  • Write a C program to “de-vowel” an input string. Assume that the user provides input containing...

    Write a C program to “de-vowel” an input string. Assume that the user provides input containing only the characters a through z (i.e., all lowercase letters). Your program should create an output string that deletes all vowels from the input string, pushing the letters together to fill any gaps. For example, given the input “theturtleandthehare” your code should print out “thtrtlndthhr”. Your program should create an output string from the input string, before printing its output. Sample Input: Enter a...

  • Please Code in Java and follow the directions given Thanks Program required Directions .A palindrome is a str...

    Please Code in Java and follow the directions given Thanks Program required Directions .A palindrome is a string that reads the same forward and backward, i.e., the letters are the same whether you read them from right to left or from left to right. Examples: 3 a) radar à is a palindrome b)Able was I ere I saw Elba à is a palindrome e good à not a palindrome Write java program to read a line of text and tell...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT