What's new

Java Pa help po unlock nito mga master salamat

I'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.
 
I'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.
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
 
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
Sure, here is a Java program that meets the requirements you specified:

Code:
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.");
        }
    }
}

This program prompts the user to input two words, stores them in String variables, and then determines if they rhyme by comparing the second word to a regular expression pattern based on the last two letters of the first word. If the two words rhyme, the program outputs a message saying so. Otherwise, it outputs a message indicating that they do not rhyme.
 

Similar threads

Back
Top