Can you help me with this JAVA question plz, thank you!! Two words or phrases in
ID: 3806005 • Letter: C
Question
Can you help me with this JAVA question plz, thank you!!
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 as in the second string) flipper p (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 Enter a phrase: Clint Eastwood Enter another phrase: >old west Action! true TEST CASES This file contains test cases to run your code on when you have it working. Most of the code has been written- you can finish implementing it and use it to fully test your SuperAnagram code NOTE: do not submit this class to OWL. This is for your testing purposes only. If your code passes all of these tests your code is likely to get max points SuperAnagram Test Cases PROJECT REQUIREMENTS: 1. Your classes must be called SuperAnTester, and SuperAnagram. 2. Your SuperAnTester class must prompt the user to enter each phrase. It must use a Scanner object to read in the input phrases. 3. Your program must either print true, if the superanagram relationship is satisfied, or false if it isn't. All console Input and Output must be done by the SuperAnTester class 4. 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. 5. 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 6. Your SuperAnagram class must use the default constructor (no parameters) onlyExplanation / Answer
This is SuperAnagram Class:
package com.checkAnagrams;
public class SuperAnagram {
public SuperAnagram() {
// TODO Auto-generated constructor stub
}
public static boolean isSuperAnagram(String a, String b){
//if both string is null, then considering it as anagram.
if(a==b){
return true;
}
//if any one string is null, then they are not anagram.
if(a==null || b==null)
return false;
//If length of both strings are not same then obviously they are not anagrams or may be.
if((a.length()!=b.length()))
return true;
char[] aArr = a.replaceAll("[\s]", "").toLowerCase().toCharArray();
char[] bArr = b.replaceAll("[\s]", "").toLowerCase().toCharArray();
// An array to hold the number of occurrences of each character
int[] counts = new int[26];
for (int i = 0; i < aArr.length; i++){
counts[aArr[i]-97]++; // Increment the count of the character at respective position
counts[bArr[i]-97]--; // Decrement the count of the character at respective position
}
// If the strings are anagrams, then counts array will be full of zeros not otherwise
for (int i = 0; i<26; i++){
if (counts[i] != 0)
return false;
}
return true;
}
}
This is SuperAnTester class:
package com.checkAnagrams;
import java.util.Scanner;
public class SuperAnTester {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
SuperAnagram sa=new SuperAnagram();
while(true){
System.out.println("Enter a Phrase");
String a=sc.next();
System.out.println("Enter another Phrase");
String b=sc.next();
System.out.println(sa.isSuperAnagram(a, b)+" ");
}
}
}
Output:
Enter a Phrase
Client_eastwood
Enter another Phrase
Old_west_Action!
true
Enter a Phrase
abc
Enter another Phrase
bdc
false
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.