Example: Develop the main() method of an application program that creates two TossableCoin objects and repeatedly tosses both of them until exactly one of them is showing heads. Then report how many tosses were made to reach that situation.
Example 1: Tell what value is returned by the method call mystery(100), where the relevant method is as follows:
public static int mystery(int m) { int low = 0, high = m+1; while (low + 1 != high) { int mid = (low + high) / 2; if (mid * mid <= m) { low = mid; } else { high = mid; } } return low; } |
Example 2: Tell what value is returned by the method call stringSwap("abcdefghi"), where the relevant method is as follows:
public static int stringSwap(String s) { int k = s.length() / 2; return s.substring(k) + s.substring(0,k); } |
Example 3: Show the contents of array a[] upon completion of execution of this code segment:
int[] a = new int[6]; a[0] = 1; for (int i=1; i != a.length; i=i+1) { a[i] = 2 * a[i-1]; } |
Example 1: You are given the WrapAroundCounter class and are asked to add a new method as follows:
/* Increments this counter by the specified amount (k), wrapping ** around if appropriate. For example, if this counter's maximum value ** is 12 and its current value is 8, then incrementing by 7 results in ** its value being 2. */ public void increment(int k) { ... } |
Example 2: Supply a body for the following method:
/* Returns the distance, within the given String (s), between the ** first two occurrences of the given character (ch). If there are ** fewer than two occurrences of ch in s, -1 is returned. */ public static int distanceBetween(String s, char ch) { ... } |