Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a method named isAnagram and a program that uses the method to determine i

ID: 3821681 • Letter: W

Question

Write a method named isAnagram and a program that uses the method to determine if two words are anagrams.

If the two words are anagrams, the method will return a true; otherwise, it will return a false.

The method header should look like this:

public static boolean   isAnagram (String w1, String w2)

      You may test your Anagrams   program with the following words:   

     melon   lemon           asleep please         listen silent        teacher    cheater                

     mother in law ->  hitler woman        student stunted     dusty study                                               

    dormitory  -> dirty room                      clint eastwood -> old west action

You will find the following methods useful:

public static void outputAnagram( String w1, String w2)

{

        String word1 = w1.toUpperCase();

        String word2 = w2.toUpperCase();

        String output = "The words " + word1 + " and " + word2 + " are" +

            (isAnagram(word1, word2) ? "" : " NOT") + " anagrams.";

        JOptionPane.showMessageDialog(null, output, "Exercise 8.12 Output", JOptionPane.INFORMATION_MESSAGE);      }

       public static boolean isAnagram (String w1, String w2) {

       char[ ] chars1 = w1.toCharArray();

       char[ ] chars2 = w2.toCharArray();

        java.util.Arrays.sort(chars1);

       java.util.Arrays.sort(chars2);

        String s1 = String.valueOf(chars1);

       String s2 = String.valueOf(chars2);

        boolean result = s1.equals(s2);

       return result;

     }   // End of isAnagram method

Explanation / Answer

// anagram Program:

import java.util.Scanner;

public class AnagramPro {

public static void main(String[] args) {

// TODO code application logic here

Scanner sc=new Scanner(System.in);

String w1,w2;

System.out.println("Enter the word 1:");

w1=sc.nextLine();

System.out.println("Enter the word 2:");

w2=sc.nextLine();

outputAnagram(w1,w2);

}

  

public static void outputAnagram( String w1, String w2)

{

String word1 = w1.toUpperCase();

String word2 = w2.toUpperCase();

String output = "The words " + word1 + " and " + word2 + " are" +

(isAnagram(word1, word2) ? "" : " NOT") + " anagrams.";

//JOptionPane.showMessageDialog(null, output, "Exercise 8.12 Output", JOptionPane.INFORMATION_MESSAGE);   

System.out.println(output);

}

public static boolean isAnagram (String w1, String w2) {

char[ ] chars1 = w1.toCharArray();

char[ ] chars2 = w2.toCharArray();

java.util.Arrays.sort(chars1);

java.util.Arrays.sort(chars2);

String s1 = String.valueOf(chars1);

String s2 = String.valueOf(chars2);

boolean result = s1.equals(s2);

return result;

} // End of isAnagram method

}

Output:

test case1:

run:

Enter the word 1:

lemon

Enter the word 2:

melon

The words LEMON and MELON are anagrams.

BUILD SUCCESSFUL (total time: 13 seconds)

test case2:

run:

Enter the word 1:

motherinlaw

Enter the word 2:

hitlerwoman

The words MOTHERINLAW and HITLERWOMAN are anagrams.

BUILD SUCCESSFUL (total time: 17 seconds)

test case3:

run:

Enter the word 1:

orange

Enter the word 2:

onion

The words ORANGE and ONION are NOT anagrams.

BUILD SUCCESSFUL (total time: 1 minute 17 seconds)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote