You do not have permission to view the full content of this post.
Log in or register now.
Create a Java program that determines if two (2) words rhyme. 1. Write the import statements to be able to use Scanner, Pattern, and Matcher. 2. Enable user input. 3. Create three (3) String variables. word1 shall store the first word. • word2 shall store the second word. • sub shall store the last two (2) letters of word1. 4. Initialize a Pattern as defined by the regular expression "L _____}" + sub +*" For the first blank, allow uppercase and lowercase letters. For the second blank, allow either 1 or 2 letters before sub (contains the last two (2) letters of word 1). 5. Initialize a Matcher to store a possible match between your pattern and word2. 6. Use the matches() method to check if the two (2) words rhymeI'm sorry, but I cannot access the link you have provided as it is a Chegg link that requires a subscription to view the content. However, if you have any specific questions or concerns regarding the Java program that you are creating to determine if two words rhyme, please feel free to ask and I will do my best to assist you.
Sure, here is a Java program that meets the requirements you specified:Create a Java program that determines if two (2) words rhyme. 1. Write the import statements to be able to use Scanner, Pattern, and Matcher. 2. Enable user input. 3. Create three (3) String variables. word1 shall store the first word. • word2 shall store the second word. • sub shall store the last two (2) letters of word1. 4. Initialize a Pattern as defined by the regular expression "L _____}" + sub +*" For the first blank, allow uppercase and lowercase letters. For the second blank, allow either 1 or 2 letters before sub (contains the last two (2) letters of word 1). 5. Initialize a Matcher to store a possible match between your pattern and word2. 6. Use the matches() method to check if the two (2) words rhyme
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class WordRhyme {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//Step 2: Enable user input
System.out.print("Enter first word: ");
String word1 = input.nextLine();
System.out.print("Enter second word: ");
String word2 = input.nextLine();
//Step 3: Create String variables
String sub = word1.substring(word1.length()-2);
//Step 4: Initialize a Pattern
Pattern pattern = Pattern.compile("[a-zA-Z]*" + sub + "[a-zA-Z]{0,2}$");
//Step 5: Initialize a Matcher
Matcher matcher = pattern.matcher(word2);
//Step 6: Use matches() method to check if two words rhyme
if (matcher.matches()) {
System.out.println(word1 + " and " + word2 + " rhyme!");
}
else {
System.out.println(word1 + " and " + word2 + " do not rhyme.");
}
}
}