// Tyler Rigsby, CSE 142 // A variety of practice problems highlighting concepts that will be useful // on the midterm public class MidtermPractice { public static void main(String[] args) { // System.out.println(digitSum(123)); // System.out.println(digitSum(400069)); // System.out.println(digitSum(378)); // // System.out.println(min(2, 4, 6)); // System.out.println(min(3, -3, 8)); // System.out.println(min(42, 6, 0)); // // System.out.println(hasOddDigit(444)); // System.out.println(hasOddDigit(357)); // System.out.println(hasOddDigit(2478)); // // sumRange(1, 5); // sumRange(-2, 2); // sumRange(3, 6); // // System.out.println(stretch("philippines")); // System.out.println(stretch("banana")); // System.out.println(stretch("mississippi")); } // 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) { int digit = n % 10; sum = sum + digit; // sum += digit; n = n / 10; // n /= 10; } return sum; } // Write a method called min that takes three integers as parameters // and that returns the minimum of the three values. public static int min(int a, int b, int c) { if (a <= b && a <= c) { return a; } else if (b <= a && b <= c) { return b; } else { return c; } //int min = Math.min(a, Math.min(b, c)); //return min; } // 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. You may assume that n is greater than or equal to 0. public static boolean hasOddDigit(int n) { while (n > 0) { if (n % 2 == 1) { return true; } n = n / 10; } return false; } // Write a method called sumRange that takes two integers as // parameters, a low and a high, and prints an equation which // desribes the sum of all integers between the two parameters, // inclusive. You may assume the first parameter has a value that // is less than or equal to the second parameter. For example, // the call sumRange(1, 5) would print: // 1 + 2 + 3 + 4 + 5 = 15 public static void sumRange(int low, int high) { int sum = 0; for (int i = low ; i < high; i++) { System.out.print(i + " + "); sum = sum + i; } // sum = sum + high; System.out.println(high + " = " + (sum + high)); } // Write a method called stretch 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, // stretch("hello") should stretch "hheelllloo" public static String stretch(String text) { String result = ""; for (int i = 0; i < text.length(); i++) { char c = text.charAt(i); result += ("" + c + c); } return result; } // DIDN'T DO IN CLASS, BUT HERE'S ONE MORE: // Write a method called firstNotIncluded that accepts two Strings as // parameters. It should return the index of the first character in the // second String that is not found in the first String. It should // return -1 if all of the characters in the second String are found in // the first String. Your method should ignore case. // For example, the call firstNotIncluded("aNt", "tan") should return -1 // because all of the letters in "tan" can be found in "aNt" if we // ignore case. The call firstNotIncluded("section", "tonsils") should // return 5 because 'l' is the first character in "tonsils" that cannot // be found in "section" and 'l' is at index 5 in "tonsils". public static int firstNotIncluded(String a, String b) { for (int i = 0; i < b.length(); i++) { // Need to add the char to a String because String.contains // take a String parameter. if (!a.toLowerCase().contains("" + b.toLowerCase().charAt(i))) { return i; } } return -1; } }