// Hw2.java // , , public class Hw2 { //////////////////////////////////////////////////// /// your methods /// (Just print these out!) //////////////////////////////////////////////////// // This method takes a (possibly-null) string s that represents a // non-negative binary number and returns the decimal equivalent of // that number as an int. If the string is null, or empty, or contains // any characters besides '0' or '1', the method should return -1. // As usual, do not worry about overflow. While there are several ways to // solve this problem, you must use an approach that requires a loop in which // you inspect each digit of the binary number in turn as you construct the result. public static int binaryToDecimal(String s) { return 42; } // This method returns true if the given string is a palindrome, // that is, the same forwards as backwards, and false otherwise. // The method returns false if the string is null, but true if // it is empty (why?). // Note that this version checks non-alphabetic characters, too, // so "a ba" is NOT a palindrome. public static boolean isPalindrome(String s) { return false; } // Returns the one-and-only 4-digit integer abcd (where a is non-zero) // where the number abcd = (a^b)*(c^d). So, for example, 2371 is not the // answer because 2^3 * 7^1 = 8 * 7 = 56 != 2371. This method must not just // return the hardcoded answer, but must use one or more "for" loops to // SEARCH for the answer, and then return it. public static int abcd() { return 42; } // Helper method for abcd(); // Returns the integer value of a^b. If b is less than 0, returns 0. public static int pow(int a, int b) { return 42; } // This method takes two strings and returns true if the two strings // are composed of the same characters (though perhaps in different // numbers and in different orders) -- that is, if every character that // is in the first string, is in the second, and vice versa -- and false otherwise. // This test is case-sensitive, so "ABC" and "abc" do not contain the same // characters. The method returns false if either string is null, but // returns true if both strings are empty (why?). public static boolean sameChars(String s1, String s2) { return false; } // Helper method for sameChars: // Same as sameChars, but only checks if all the chars in s1 // also occur in s2, but not vice versa! public static boolean sameChars1(String s1, String s2) { return false; } // Helper method for sameChars1: // Returns true if the given char occurs in the given String // and false otherwise. Returns false if the String is null. public static boolean contains(String s, char c) { return false; } // Returns the most-frequently-occurring character in the given string, // with ties going to the first character alphabetically. Counts // are case-insensitive, so 'a' and 'A' both count as an 'A', and the // result is an uppercase character. If the string is empty or null, // the result should be the char value 0. // Note that to receive full credit, you must write at least one well-chosen // helper method for this method (and this helper method must be distinct // from the helper methods for the "sameChars" problem). public static char mostFrequentChar(String s) { return 'Q'; } // Returns the nth perfect number, where a perfect number is // an integer that is the sum of its proper divisors (that is, // the numbers less than it that evenly divide it). For example, // the proper divisors of 6 are 1, 2, and 3, and 6 = 1 + 2 + 3, // so 6 is a perfect number. If n is less than 1, the method // should return -1. // Note that to receive full credit, you must write at least one well-chosen // helper method for this method. // As usual (for now), do not worry about overflow. public static int nthPerfectNumber(int n) { return 42; } //////////////////////////////////////////////////// /// test methods for your methods /// (Do not print out any code below here!) //////////////////////////////////////////////////// public static void testBinaryToDecimal() { System.out.print("Testing binaryToDecimal... "); assert(binaryToDecimal("0") == 0); assert(binaryToDecimal("1") == 1); assert(binaryToDecimal("10") == 2); assert(binaryToDecimal("11") == 3); assert(binaryToDecimal("01001") == 9); assert(binaryToDecimal("11001") == 25); assert(binaryToDecimal("This is not a binary number!") == -1); assert(binaryToDecimal("") == -1); assert(binaryToDecimal(null) == -1); System.out.println("Passed all tests!"); } public static void testIsPalindrome() { System.out.print("Testing isPalindrome... "); assert(isPalindrome("a") == true); assert(isPalindrome("ab") == false); assert(isPalindrome("a ba") == false); assert(isPalindrome("bab") == true); assert(isPalindrome("b,ab") == false); assert(isPalindrome("b,a,b") == true); assert(isPalindrome("Madam, I'm Adam") == false); assert(isPalindrome("madamimadam") == true); assert(isPalindrome("") == true); assert(isPalindrome(null) == false); System.out.println("Passed all tests!"); } public static void testPow() { System.out.print("Testing pow... "); assert(pow(2, -1) == 0); assert(pow(2, 0) == 1); assert(pow(2, 1) == 2); assert(pow(2, 5) == 32); assert(pow(-2, 5) == -32); assert(pow(-2, 6) == 64); System.out.println("Passed all tests!"); } public static void testAbcd() { System.out.print("Testing abcd... "); int n = abcd(); assert((n >= 1000) && (n < 10000)); assert(pow(n/1000,(n/100)%10)*pow((n/10)%10,n%10) == n); System.out.println("Passed all tests!"); } public static void testContains() { System.out.print("Testing contains... "); assert(contains("abcd",'c') == true); assert(contains("abcdabcd",'d') == true); assert(contains("This is a test!",'!') == true); assert(contains("abcd",'e') == false); assert(contains("",'f') == false); assert(contains(null,'g') == false); System.out.println("Passed all tests!"); } public static void testSameChars1() { System.out.print("Testing sameChars1... "); assert(sameChars1("abc", "abc") == true); assert(sameChars1("abc", "cba") == true); assert(sameChars1("ab", "abc") == true); // false for sameChars assert(sameChars1("abc", "ab") == false); assert(sameChars1("ababab", "ababbbb") == true); assert(sameChars1("ababab", "abcabbbb") == true); // false for sameChars assert(sameChars1("abcabbbb", "ababab") == false); assert(sameChars1(null, null) == false); assert(sameChars1(null, "abc") == false); assert(sameChars1("abc", null) == false); System.out.println("Passed all tests!"); } public static void testSameChars() { System.out.print("Testing sameChars... "); assert(sameChars("abc", "abc") == true); assert(sameChars("abc", "cba") == true); assert(sameChars("ab", "abc") == false); // true for sameChars1 assert(sameChars("abc", "ab") == false); assert(sameChars("ababab", "ababbbb") == true); assert(sameChars("ababab", "abcabbbb") == false); // true for sameChars1 assert(sameChars("abcabbbb", "ababab") == false); assert(sameChars(null, null) == false); assert(sameChars(null, "abc") == false); assert(sameChars("abc", null) == false); System.out.println("Passed all tests!"); } public static void testMostFrequentChar() { System.out.print("Testing mostFrequentChar... "); assert(mostFrequentChar("STUV") == 'S'); assert(mostFrequentChar("tuvw") == 'T'); assert(mostFrequentChar("UTSR") == 'R'); assert(mostFrequentChar("This is a test") == 'S'); assert(mostFrequentChar("") == ((char)0)); assert(mostFrequentChar(null) == ((char)0)); System.out.println("Passed all tests!"); } public static void testNthPerfectNumber() { System.out.print("Testing nthPerfectNumber... "); assert(nthPerfectNumber(-5) == -1); assert(nthPerfectNumber(0) == -1); assert(nthPerfectNumber(1) == 6); assert(nthPerfectNumber(2) == 28); assert(nthPerfectNumber(3) == 496); System.out.println("Passed all tests!"); } //////////////////////////////////////////////////// /// additional test methods //////////////////////////////////////////////////// public static void checkAssertsAreEnabled() { boolean assertsEnabled = false; try { assert(false); } catch (Throwable t) { assertsEnabled = true; } if (!assertsEnabled) throw new RuntimeException("assert statements not enabled!"); } public static void testAll() { testBinaryToDecimal(); testIsPalindrome(); testPow(); testAbcd(); testContains(); testSameChars1(); testSameChars(); testMostFrequentChar(); testNthPerfectNumber(); } //////////////////////////////////////////////////// /// main method //////////////////////////////////////////////////// public static void main(String[] args) { checkAssertsAreEnabled(); testAll(); } }