// This file contains several sample problems discussed in lecture. import java.util.*; public class Methods2 { // 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. public static int digitSum(int n) { int sum = 0; while (n > 0) { sum += n % 10; n = n / 10; } return sum; } // Write a method called reverse that takes an integer n as a parameter and // that returns the reverse of the number. For example, reverse(38042) // should return 24083. Assume that n is >= 0. public static int reverse(int n) { int result = 0; while (n > 0) { result = result * 10 + n % 10; n = n / 10; } return result; } // Write a method called roll7 that simulates the rolling of two dice until // their sum is equal to 7. The method should print each roll and its sum // and show a count of how many rolls it took to get to 7, as in: // 3 + 5 = 8 // 2 + 1 = 3 // 1 + 4 = 5 // 3 + 4 = 7 // sum of 7 after 4 rolls // You may assume that an import on java.util is included in the program so // that you can construct a Random object. You must exactly reproduce the // format of this sample execution. public static void roll7() { Random r = new Random(); int sum = 0; int count = 0; while (sum != 7) { count++; int roll1 = r.nextInt(6) + 1; int roll2 = r.nextInt(6) + 1; sum = roll1 + roll2; System.out.println(roll1 + " + " + roll2 + " = " + sum); } System.out.println("sum of 7 after " + count + " rolls"); } // Write a method called bothOdd that takes two integers as parameters and // that returns true if they are both odd and that returns false otherwise public static boolean bothOdd(int n1, int n2) { return n1 % 2 != 0 && n2 % 2 != 0; } // Write a method called hasOddDigit that takes an integer as a parameter // and that returns true if it has an odd digit, false if it does not. public static boolean hasOddDigit(int n) { while (n > 0) { int digit = n % 10; if (digit % 2 != 0) { return true; } n = n / 10; } return false; } // 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 response = ""; for (int i = 0; i < s.length(); i++) { response = response + s.charAt(i) + s.charAt(i); } return response; } // 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?" public static String noSpaces(String s) { String result = ""; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) != ' ') { result += s.charAt(i); } } return result; } // Write a method called reverse that takes a string as a parameter and // that returns a new string that has the same text in reverse order. For // example, reverse("hello") should return "olleh" public static String reverse(String s) { String result = ""; for (int i = 0; i < s.length(); i++) { result = s.charAt(i) + result; } return result; } }