Question

Today you are to write a Java program that will prompt for and read 2 words of equal length entered by the user, and create a new word which contains the last letter of the 1st word, the last letter of the 2nd word, followed by the second to last character of the 1st word, followed by the second to last character of the 2nd word and so on. Be sure to use the same format and wording as in the sample run in the table below. You can use the loop of your choice to solve the problem. The box below illustrates how your program should behave and appear. REMEMBER in the output: - is a space and is a new line. Text in green is user input Enter 2. words (same length): 12345 vwxyz The new word is 5z4y3x2w1vd What an odd word! Note 1: You are to expect a perfect user who will always enter 2 words of the same length; that is, do not verify the validity of user input. Note 2: The use of libraries other than java.util.Scanner is prohibited. Your program must work for any strings entered, not just the ones in the samples above. The words can be of any length not just 5 as in the sample above. Note 3: Final thought, remember that your solution is case-sensitive and space-sensitive and fulfill the above instructions carefully and precisely. Reminder: When submitting your solution to the lab system, make sure that if you have a package statement at the top of your .java file it is commented out (has // in front of it) as failing to do so will result in a Compilation Error hence a grade of O (restriction of the PC system

For this lab you are required to fulfill all requirements exactly as described in this provided document, no less, no more. Q

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

package string;

import java.util.Scanner;

// Defines a class StringTest

public class StringTest

{

// main method definition

public static void main(String s[])

{

// Creates a Scanner class object

Scanner input = new Scanner(System.in);

// Accepts two words

System.out.println("Enter 2 words (same length): ");

String firstWord = input.next();

String secondWord = input.next();

// To store the concatenated string

String newWord = "";

// Checks if first word length is equals to second word length

if(firstWord.length() == secondWord.length())

{

// Loops from last character of the first word to beginning

for(int c = firstWord.length()-1; c >= 0; c--)

{

// Adds the current character of the first word to newWord

newWord += (char)firstWord.charAt(c);

// Adds the current character of the second word to newWord

newWord += (char)secondWord.charAt(c);

}// End of for loop

// Displays the new word after concatenation

System.out.print("\n\n The new word is: " + newWord);

}// End of for loop

// Otherwise two strings are not equal length

else

System.out.print("\n ERROR: Two words are not same length");

}// End of main method

}// End of class

Sample Output 1:

Enter 2 words (same length):
this 1234


The new word is: s4i3h2t1

Sample Output 2:

Enter 2 words (same length):
check 785

ERROR: Two words are not same length

Add a comment
Know the answer?
Add Answer to:
Today you are to write a Java program that will prompt for and read 2 words...
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
  • JAVA LANGUAGE For this lab you are required to fulfill all requirements exactly as described in...

    JAVA LANGUAGE For this lab you are required to fulfill all requirements exactly as described in this provided document, no less, no more. Question: Today you are to write a Java program that will prompt for and read 2 words of equal length entered by the user, and create a new word which contains the last letter of the 1st word, the last letter of the 2nd word, followed by the second to last character of the 1st word, followed...

  • Capitalization JAVA In this program, you will read a file line-by-line. For each line of data...

    Capitalization JAVA In this program, you will read a file line-by-line. For each line of data (a string), you will process the words (or tokens) of that line one at a time. Your program will capitalize each word and print them to the screen separated by a single space. You will then print a single linefeed (i.e., newline character) after processing each line – thus your program will maintain the same line breaks as the input file. Your program should...

  • ***LOOPS NOT ALLOWED*** String variables and use of String methods: Write a complete Java program which...

    ***LOOPS NOT ALLOWED*** String variables and use of String methods: Write a complete Java program which prompts the user for a string with 3 words each separated by a single space and reads the line into one String variable using nextline(). Extract each word and add them to a new String variable in reverse order with a space between the words. Have the 1st letter of the new string in upper case and all other strings in lower case letter....

  • Write a Java Program that performs the following functionalities: and you can use if statements, cases,...

    Write a Java Program that performs the following functionalities: and you can use if statements, cases, and string methods Enter a new main sentence Find a String Find all incidents of a String Find and Replace a String Replace all the incidents of a String Count the number of words Count a letter’s occurrences Count the total number of letters Delete all the occurrences of a word Exit The program will display a menu with each option above, and then...

  • For this lab you will write a Java program that plays a simple Guess The Word...

    For this lab you will write a Java program that plays a simple Guess The Word game. The program will prompt the user to enter the name of a file containing a list of words. These words mustbe stored in an ArrayList, and the program will not know how many words are in the file before it starts putting them in the list. When all of the words have been read from the file, the program randomly chooses one word...

  • Could you guys write an efficient java program to implement BST. Your java program should read...

    Could you guys write an efficient java program to implement BST. Your java program should read words and its corresponding French and store it in a BST. Then read every English word and print its corresponding French by searching in BST. Assume your java program is in xxxxx5.java file (5th java project), where xxxxx is the first 5 characters of your last name. To compile: javac xxxxx5.java To execute: java xxxxx5 < any data file name Your main method should...

  • I need to make this into a Java Code: Write a Java program that asks the...

    I need to make this into a Java Code: Write a Java program that asks the user for four words: word1 word2 word3 word4. Your prompts to the user must be: Enter 4 words: Output word1 and the length of word1 and the position of the character 'h' in word1 Output word2 and the length of word2 and the position of the character 'i' in word2 Output word3 and the length of word3 and the position of the substring "hi"...

  • Write an efficient java program to implement BST. Your java program should read n words and...

    Write an efficient java program to implement BST. Your java program should read n words and its corresponding French and store it in a BST. Then read every English word and print its corresponding French by searching in BST. Assume your java program is in engtofren.java file To compile: javac engtofren.java To execute: java engtofren< any data file name Your main method should be as follow: public static void main(String args[]) { engtofren bst = new engtofren (); // try{...

  • Write a program in java to read a string object consisting 300 characters or more using...

    Write a program in java to read a string object consisting 300 characters or more using index input Stream reader. The program should perform following operations. The String must have proper words and all kind of characters.(Use String class methods). a, Determine the length of the string count the number of letters in the strings , count the number of numeric object d. Calculate the number of special character e. Compute the ratio of the numeric to the total f....

  • Program In Assembly For this part, your MAL program must be in a file named p5b.mal....

    Program In Assembly For this part, your MAL program must be in a file named p5b.mal. It must have at least one function in addition to the main program. For the purposes of Part (b), you may assume the following 1. Any line of text typed by a user has at most 80 characters including the newline character. 2. A whitespace character refers to a space, a tab or the new line character. 3. A word is any sequence of...

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