// CSE 142, Autumn 2009, Marty Stepp // Determines whether two words rhyme and/or alliterate. // This second version uses methods with boolean return types. // (Look at how much cleaner the main method is now!) import java.util.*; public class Rhyme2 { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.print("Type two words: "); String word1 = console.next(); String word2 = console.next(); if (rhyme(word1, word2)) { System.out.println("They rhyme!"); } if (alliterate(word1, word2)) { System.out.println("They alliterate!"); } } // Returns true if the two given words "rhyme" // (end w/ same last 2 letters), and false otherwise. public static boolean rhyme(String word1, String word2) { return word1.length() >= 2 && word2.length() >= 2 && word1.substring(word1.length() - 2).equalsIgnoreCase( word2.substring(word2.length() - 2)); } // Returns true if the two given words "alliterate" // (start w/ same letter), and false otherwise. public static boolean alliterate(String word1, String word2) { String firstLetter2 = word2.substring(0, 1).toLowerCase(); return word1.toLowerCase().startsWith(firstLetter2); } }