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

Two words or phrases in English are anagrams if their letters (and only their le

ID: 3682159 • Letter: T

Question

Two words or phrases in English are anagrams if their letters (and only their letters), rearranged, are the same. We assume that upper and lower case are indistinguishable, and punctuation and spaces don't count. Some examples and non-examples of regular anagrams:

* The eyes / they see (yes)
* moo / mo (no)
* Clint Eastwood / Old west Action! (yes)
* Dormitory / Dirty Room (yes)

Your job for this assignment is to solve a slight variant of the traditional anagram problem called superanagram. Here you are to write a two class application that reads in two words or phrases from the keyboard, and then outputs true if the first phrase is an anagram of some of the letters in the second phrase, false if it is not. Here are some examples:

* mo / moo (true)
* mo / mOO (true - capitalization doesn't matter)
* moo / mo (false - first phrase is NOT an anagram of some (or all) of letters of second)
* rip / ziPPer (true)
* abc / aabc (true)
* aabc / abcde (false - too few a's in the second string)
* flipper / rip (false)
* Clint Eastwood / Old west Action! (true - the two can have exactly the same letters)
* a stitch in time saves nine / is this meant as an incentive? (true)
* narcissism / one man crisis (false- can you see why?)

Example of a program run:


Some test cases to run your code on. Includes String arrays if you want to write a test harness.
SuperAnagram Test Cases ABOVE


PROJECT REQUIREMENTS:

Your classes must be called SuperAnTester, and SuperAnagram.

Your SuperAnTester class must prompt the user to enter each phrase. It must use a Scanner object to read in the input phrases.

Your program must either print true, if the superanagram relationship is satisfied, or false if it isn't.

Your SuperAnagram class must have an isSuperAnagram method that returns boolean and takes two String parameters- the phrases to be tested. Your isSuperAnagram method must return true if the superanagram relationship is satisfied, false, if it isn't.

Your SuperAnTester class must create an instance of the SuperAnagram class and call its isSuperAnagram method to determine if the input satisfies the superanagram relationship.

Your SuperAnagram class must use the default constructor (no parameters) only.

You may NOT use an ArrayList for this project. Major point deduction for this.


Some tips:

Remember to use the nextLine method, rather than next, since spaces may be present in the two phrases that are submitted.

Use the String methods toLowerCase and, possibly, toCharArray. (Consult the Java API).

Punctuation and any characters other than letters a-z are ignored.

For some useful background on characters and how to work with them, watch the movies in the textbook at the ends of section 4.1 and 7.1.

Very important: suppose you have two arrays of characters such that the first is purported to be an embedded anagram of the second, in the sense we've described above. How can you tell? The single most important thing to do, before you write a single line of code, is to work out a paper and pencil algorithm that distinguishes between superanagrams and non-superanagrams.

Algorithm Idea #1: make a scoreboard for the letters a to z. Every time you encounter a letter in the second String, up its count by 1; Then, every time you encounter a letter in the first String, lower its count by 1. Accept if the scoreboard ends up with all entries >= 0. (of course make sure you understand why this is - use pencil and paper to convince yourself!!).

Algorithm Idea #2: convert strings to arrays. March down first array (representing the first string). When you encounter a letter, look for it in the second array. If you find it, blank out the occurrence in the second array; if you don't find it - it's not a super anagram. When you're all done, you've got a superanagram if your search in the second array never goes bad.

Run all of the test cases provided on your solution. You can write code to use the arrays to test your SuperAnagram class. Follow the pseudocode provided. Note that this is an optional suggestion.


IMPORTANT NOTE: Do NOT put import statements in your classes. They are already included in the evaluation code on our end.

Place your SuperAnTester class in the box below.

import java.util.Scanner;

.......................................................

Explanation / Answer

SuperAnTester.java

import java.util.Scanner;
public class SuperAnTester {
   public static void main(String[] args) {
       Scanner input = new Scanner(System.in);
       System.out.print("Enter a Phrase: >");
       String phraseOne = input.nextLine();
       System.out.print("Enter another Phrase: >");
       String phraseTwo = input.nextLine();
       SuperAnagram anagram = new SuperAnagram();
       System.out.println(anagram.isSuperAnagram(phraseOne, phraseTwo));
       input.close();
   }
}

SuperAnagram.java
public class SuperAnagram {
  
   public SuperAnagram() {}
  
   /**
   * Determines if the first phrase is an anagram of the second
   */
   public boolean isSuperAnagram(String phraseOne, String phraseTwo) {
       phraseOne = phraseOne.replaceAll("[^a-zA-Z]", "");//removes all non alphabetic characters
       String[] firstPhrase = phraseOne.split("");
       String[] secondPhrase = phraseTwo.split("");
       for(String a : firstPhrase) {
           boolean currentChar = true;
           currentChar = checkChar(a, secondPhrase);
           if(currentChar == false) return false;
       }
       return true;
   }
  
   /**
   * Determines if the current character in the first phrase matches any availabile character in the second
   */
   public static boolean checkChar(String charOne, String[] secondPhrase) {
       for(int a = 0; a < secondPhrase.length; a++) {
           if(charOne.equalsIgnoreCase(secondPhrase[a])) {
               secondPhrase[a] = " "; //blanks out the character in the second array so it can't be reused
               return true;
           }
       }
       return false;
   }
}

sample output


Enter a Phrase:                                                                                                                                             
>Clint Eastwood                                                                                                                                             
Enter another Phrase:                                                                                                                                       
>Old west Action!                                                                                                                                           
true