// Helene Martin, CSE 142 // Several methods that I think have interesting characteristics. import java.util.*; public class Practice { public static void main(String[] args) { System.out.println(isAllVowels("aeiouAEIOU")); // true System.out.println(isAllVowels("ooooOOOooOOOh")); // false System.out.println(); System.out.println(hasNoVowels("tqqqaqr")); // false System.out.println(hasNoVowels("qwrrrtpppp")); // true System.out.println(); System.out.println(digitSum(29107)); // 19 System.out.println(digitSum(-23)); // 5 System.out.println(); System.out.println(hasAnOddDigit(34442)); // true System.out.println(hasAnOddDigit(222)); // false System.out.println(); System.out.println(stutter("hello")); System.out.println(stutter("CSE 142")); } // isAllVowels returns true if all characters are vowels // aaaaaahiuuuuuuu (first, we solved the problem by hand public static boolean isAllVowels(String str) { for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); if (isNonVowel(c + "")) { return false; } } return true; } // returns true if phrase has no vowels, false otherwise // NOTE: we can't use isAllVowels! public static boolean hasNoVowels(String phrase) { for (int i = 0; i < phrase.length(); i++) { String s = phrase.substring(i, i + 1); // String s = phrase.charAt(i) + "" if (isVowel(s)) { return false; } } return true; } // digitSum accepts an integer parameter and returns the sum of its digits public static int digitSum(int value) { // 4523 % 10 -> 3 // 4523 / 10 -> 452 // 452 % 10 --> 2 value = Math.abs(value); // for negatives int sum = 0; while (value > 0) { int digit = value % 10; value = value / 10; sum += digit; } return sum; } // 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) { 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 = ""; // cummulative sum pattern for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); response = response + ch + ch; } return response; } // isVowel returns whether a String is a vowel (a, e, i, o, or u), // case-insensitively. public static boolean isVowel(String s) { s = s.toLowerCase(); // or use equalsIgnoreCase return s.equals("a") || s.equals("e") || s.equals("i") || s.equals("o") || s.equals("u"); } // isNonVowel returns whether a String is any character except a vowel. public static boolean isNonVowel(String s) { //s = s.toLowerCase(); //return !s.equals("a") && !s.equals("e" ... De Morgan's law return !isVowel(s); } }