Question
I have this Java code, but it fails this JUnit test: _testSameCounts(new int[] {}, new int[] {1}, false); package edu.wit.cs.comp1050; import java.util.Scanner; /** * Solution
I have this Java code, but it fails this JUnit test: _testSameCounts(new int[] {}, new int[] {1}, false);
package edu.wit.cs.comp1050;
import java.util.Scanner;
/** * Solution to Lab Assignment 1 when it runs it asks the user for two phrases * and says if they're anagrams or not * * @author Rose Levine * */ public class LA1a {
/** * Returns an array with counts for each letter from a-z (case-insensitive), * ignoring all other characters: * * [0]: number of a/A's [1]: number of b/B's ... [25]: number of z/Z's * * @param word input word * @return count of case-insensitive letters */ public static int[] countLetters(String word) { int[] counts = new int[26]; char ch; for (int i = 0; i < word.length(); i++) { ch = Character.toLowerCase(word.charAt(i)); if (Character.isAlphabetic(ch)) { counts[ch - 'a']++; } } return counts; }
/** * Compares two arrays and returns true if they have the same contents * * @param c1 array 1 * @param c2 array 2 * @return true if c1 and c2 have the same contents */ public static boolean sameCounts(int[] c1, int[] c2) { for (int i = 0; i < c1.length; i++) { if (c1[i] != c2[i]) { return false; } } return true; }
/** * Inputs two phrases and outputs if they are anagrams (ignoring case and any * non-letter characters) * * @param args command-line arguments, ignored */ public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter phrase 1: "); String s1 = input.nextLine(); System.out.print("Enter phrase 2: "); String s2 = input.nextLine(); int[] count1 = countLetters(s1), count2 = countLetters(s2); if (sameCounts(count1, count2)) { System.out.println("These phrases are anagrams."); } else { System.out.println("These phrases are not anagrams."); }
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started