// This file contains some sample methods that are similar to questions that might be // asked on the midterm exam. import java.util.*; public class MidtermPractice { public static void main(String[] args) { System.out.println(doubleLetters("abcde")); System.out.println(doubleLetters("Mississippi")); System.out.println(doubleLetters("abracadabra")); System.out.println(); System.out.println(containsE("abcdefg")); System.out.println(containsE("cheese")); System.out.println(containsE("banana")); System.out.println(containsE("CSE")); System.out.println(containsE("")); System.out.println(); Random rand = new Random(); rollSeven(rand); System.out.println(); rollSeven(rand); System.out.println(); rollSeven(rand); System.out.println(); System.out.println(); } // Write a static method called doubleLetters that takes a String as a parameter and // returns a new String where each letter of the original string has been replaced with // two of that letter. For example, doubleLetters("hello") should return "hheelllloo". public static String doubleLetters(String str) { String result = ""; for (int i = 0; i < str.length(); i++) { // result = result + str.substring(i, i + 1) + str.substring(i, i + 1); result = result + str.charAt(i) + str.charAt(i); } return result; } // Write a static method called containsE that takes a single string parameter // and returns a true if the value passed contains an 'e' as one of its characters, // and false otherwise. Your method should return true if an 'e' appears in either // lowercase or uppercase. public static boolean containsE(String str) { for (int i = 0; i < str.length(); i++) { char ch = str.toLowerCase().charAt(i); if (ch == 'e') { return true; } } return false; } // Write a static method called rollSeven that takes a Random object as a parameter // and simulates the rolling of two dice until their sum is equal to 7. The method // should print each roll and its sum and print the number of rolls it took to get // to 7. Consider this sample execution: // Roll #1: 3 + 5 = 8 // Roll #2: 2 + 1 = 3 // Roll #3: 1 + 4 = 5 // Roll #4: 3 + 4 = 7 // Found 7 in 4 rolls. // You must exactly reproduce the format of this sample execution. public static void rollSeven(Random rand) { int total = 0; int count = 0; while (total != 7) { int roll1 = rand.nextInt(6) + 1; int roll2 = rand.nextInt(6) + 1; total = roll1 + roll2; count++; System.out.println("Roll #" + count + ": " + roll1 + " + " + roll2 + " = " + total); } System.out.println("Found 7 in " + count + " rolls."); } }