// Midterm.java // NAME, ANDREW ID, SECTION import java.util.*; class Midterm { ///////////////////////////////////////////////////// // nthPowerfulNumber ///////////////////////////////////////////////////// // A "powerful" number is a positive integer m that for every // prime number p dividing m, p^2 also divides m. So 12 is not // powerful because 3 divides 12 but 3^2=9 does not divide 12. // We assume 1 is powerful (because no prime numbers divide it). // Here are the first several powerful numbers: // 1, 4, 8, 9, 16, 25, 27, 32, 36, 49, 64, 72, 81, 100, 108, ... // This method takes a number n and returns the nth powerful number, // or -1 if n is non-positive. public static int nthPowerfulNumber(int n) { return 42; } public static void testNthPowerfulNumber() { System.out.print("Testing nthPowerfulNumber... "); Assert(nthPowerfulNumber(-1) == -1); Assert(nthPowerfulNumber(0) == -1); Assert(nthPowerfulNumber(1) == 1); Assert(nthPowerfulNumber(2) == 4); Assert(nthPowerfulNumber(3) == 8); Assert(nthPowerfulNumber(12) == 72); Assert(nthPowerfulNumber(50) == 900); System.out.println("Passed all tests!"); } ///////////////////////////////////////////////////// // nthOddishNumber ///////////////////////////////////////////////////// // We will say that a number is "oddish" (our term) if it is // positive and it has more odd digits than even digits. So, // 91234 is oddish because it has 3 odd digits and 2 even digits, // and 76 is not oddish because it does not have more odd digits // than even digits. // Here are the first several oddish numbers: // 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 31, 33, 35, 37, 39, 51, ... // This method takes a number n and returns the nth oddish number, // or -1 if n is non-positive. public static int nthOddishNumber(int n) { return 42; } public static void testNthOddishNumber() { System.out.print("Testing nthOddishNumber... "); Assert(nthOddishNumber(-1) == -1); Assert(nthOddishNumber(0) == -1); Assert(nthOddishNumber(1) == 1); Assert(nthOddishNumber(2) == 3); Assert(nthOddishNumber(3) == 5); Assert(nthOddishNumber(11) == 31); Assert(nthOddishNumber(1234) == 3217); System.out.println("Passed all tests!"); } ///////////////////////////////////////////////////// // medianLength ///////////////////////////////////////////////////// // This method returns the median length of the Strings in the // given array, or -1 if the array is either empty or null. // Note that the median of an odd list is the middle element, // and the median of an even list is the average of the middle // two elements. public static int medianLength(String[] a) { return 42; } public static void testMedianLength() { System.out.print("Testing medianLength... "); Assert(medianLength(null) == -1); String[] a0 = { }; Assert(medianLength(a0) == -1); String[] a1 = { "this", "is", "a", "test" }; Assert(medianLength(a1) == 3); String[] a2 = { "this", "is", "another", "test" }; Assert(medianLength(a2) == 4); String[] a3 = { "12345", "1234", "1234567"}; Assert(medianLength(a3) == 5); System.out.println("Passed all tests!"); } ///////////////////////////////////////////////////// // hasDuplicate ///////////////////////////////////////////////////// // This method returns true if at least one string occurs at least // twice in the given array, and false otherwise (including if the // array is null). public static boolean hasDuplicate(String[] a) { return false; } public static void testHasDuplicate() { System.out.print("Testing hasDuplicate... "); Assert(hasDuplicate(null) == false); String[] a1 = { "no", "No", "NO!!!" }; Assert(hasDuplicate(a1) == false); String[] a2 = { "aaaa" }; Assert(hasDuplicate(a2) == false); String[] a3 = { "this", "is", "a", "test", "it", "is" }; Assert(hasDuplicate(a3) == true); System.out.println("Passed all tests!"); } ///////////////////////////////////////////////////// // isHappy ///////////////////////////////////////////////////// // A happy number is defined by the following process. // Starting with any positive integer, replace the number // by the sum of the squares of its digits, and repeat the // process until the number equals 1 (where it will stay), // or it loops endlessly in a cycle which does not include 1. // Those numbers for which this process ends in 1 are happy numbers, // while those that do not end in 1 are unhappy numbers. // This method takes an integer and returns true if it is a // happy number and false otherwise. Note that non-positive numbers // are not happy. // HINT: Interestingly, every number that ends in a cycle ends in the // SAME cycle. You will probably need this hint to solve this problem. public static boolean isHappy(int n) { return false; } public static void testIsHappy() { System.out.print("Testing isHappy... "); Assert(isHappy(-1) == false); Assert(isHappy(0) == false); Assert(isHappy(1) == true); Assert(isHappy(10) == true); Assert(isHappy(12) == false); Assert(isHappy(13) == true); Assert(isHappy(495) == false); Assert(isHappy(496) == true); System.out.println("Passed all tests!"); } ///////////////////////////////////////////////////// // main and checkAssertsAreEnabled ///////////////////////////////////////////////////// // Use "Assert" rather than "Assert" so we work in Java 1.4 // as well as in Java 1.5. public static void Assert(boolean b) { if (!b) throw new RuntimeException("Assertion Error!"); } public static void main(String[] args) { // 1. Do EITHER ONE (your choice) but NOT BOTH of these testNthPowerfulNumber(); testNthOddishNumber(); // 2. Do EITHER ONE (your choice) but NOT BOTH of these testHasDuplicate(); testMedianLength(); // 3. BONUS/OPTIONAL: testIsHappy(); } }