// CSE 142 Lecture 13 // Midterm Review // Some problems demonstrating the concepts we've learned that will be // applied on the midterm. public class MidtermReview { // -------------------------------------------------------------------------- // Write a static method isPowerOfTwo that takes an integer n as an argument, // and that returns true if n is a power of two, and otherwise false. // If n is zero or negative, return false. Note that isPowerOfTwo(1) // should return true, since 2^0 = 1. // -------------------------------------------------------------------------- public static boolean isPowerOfTwo1(int n) { if (n <= 0) { return false; } int cur = (int)Math.pow(2, 0); int i = 0; while (cur < n) { cur = (int)Math.pow(2, i); i++; } return cur == n; } public static boolean isPowerOfTwo2(int n) { if (n < 1) { return false; } // In class it was suggested that we use i < n / 2, // but that doesn't work for n == 1. for (int i = 0; i < n; i++) { if (Math.pow(2, i) == n) { return true; } } return false; } public static boolean isPowerOfTwo3(int n) { if (n <= 0) { return false; } int cur = 1; while (cur < n) { cur *= 2; } return cur == n; } public static boolean isPowerOfTwo4(int n) { if (n < 1) { return false; } // Check to see if log2(n) is a whole number. // This is hacky...probably shouldn't do stuff like this. double exp = Math.log(n) / Math.log(2); return Math.abs(exp - (int)exp) < 0.00000000000001; } public static boolean isPowerOfTwo5(int n) { if (n < 1) { return false; } while (n % 2 == 0) { n /= 2; } return n == 1; } // --------------------------------------------------------------------- // 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 firstNotIncluded1(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; } public static int firstNotIncluded2(String a, String b) { a = a.toLowerCase(); b = b.toLowerCase(); for (int i = 0; i < b.length(); i++) { if (!a.contains(b.substring(i, i + 1))) { return i; } } return -1; } public static int firstNotIncluded3(String a, String b) { a = a.toLowerCase(); b = b.toLowerCase(); int i = 0; while (b.length() > 0) { if (!a.contains(b.substring(0, 1))) { return i; } i++; b = b.substring(1); } return -1; } // --------------------------------------------------------------------- // Write a method speedingTicket that decides whether a given driver // should be given a speeding ticket from a police officer. The method // accepts three parameters: the driver's car speed in miles per hour, // as an integer; the current speed limit, as an integer; and whether // or not the police officer has eaten a donut today, as a true/false // value. (A police officer that has eaten a donut is happy, so he/she // is less likely to give the driver a ticket). Your method should return // true if the driver should receive a speeding ticket, and false if not. // A driver should be given a speeding ticket if any of the following // conditions are met: // // The officer has eaten a donut (true) and the driver's speed is at least // 10 mph over the speeding limit. // // The officer has not eaten a donut (false) and the driver's speed is // at least 5 mph over or under the limit. // // The driver is going 100 mph or faster, regardless of the speed limit // or donut status. // // You may assume that the integers passed as parameters will be non-negative. // public static boolean speedingTicket1(int speed, int limit, boolean donut) { if (speed >= 100) { return true; } else if (donut && speed >= limit + 10) { return true; } else if (!donut && (speed >= limit + 5 || speed <= limit - 5)) { return true; } return false; } public static boolean speedingTicket2(int speed, int limit, boolean donut) { if (speed >= 100) { return true; } if (donut) { if (speed >= limit + 10) { return true; } } else { if (speed >= limit + 5 || speed <= limit - 5) { return true; } } return false; } public static boolean speedingTicket3(int speed, int limit, boolean donut) { return speed >= 100 || donut && speed >= limit + 10 || !donut && Math.abs(speed - limit) >= 5; } }