// Helene Martin, CSE 142 // Short problems with interesting twists. // Always start by asking yourself how you would solve the problem by hand. // What programming tools would allow you to automate that? public class ReviewProblems { public static void main(String[] args) { showTwos(128); System.out.println(stutter("hello")); } /* Write a method named showTwos that shows the factors of 2 in an integer. For example: Call Output showTwos(7); 7 = 7 showTwos(18); 18 = 2 * 9 showTwos(68); 68 = 2 * 2 * 17 showTwos(120); 120 = 2 * 2 * 2 * 15 The idea is to express the number as a product of factors of 2 and an odd number. The number 120 has 3 factors of 2 multiplied by the odd number 15. For odd numbers (e.g. 7), there are no factors of 2, so you just show the number itself. Assume that your method is passed a number greater than 0. */ public static void showTwos(int num) { System.out.print(num + " = "); while (num % 2 == 0) { System.out.print("2 * "); num = num / 2; } System.out.println(num); } // Write a method called digitSum that takes an integer n as a parameter // and that returns the sum of the digits of n. Assume that n is >= 0. // Write a method called stutter that takes a string as a parameter and // that returns a new string where each letter of the original string has // been replaced with two of that letter. For example, stutter("hello") // should return "hheelllloo" public static String stutter(String s) { String result = ""; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); result = result + c + c; } return result; } // On your own for practice: // Write a method called noSpaces that takes a String as a parameter and // that returns a new string that contains the nonspace characters of the // original. For example, noSpaces("how are you?") should return // "howareyou?" }