CSC 15: Problems to prepare for Quiz 2: SOLUTIONS Quiz 2 will cover: How to call functions given a class interface (like on first quiz) Boolean operators &&, ||, and ! Type casting - when to use (double), (int), and what effect they have How to write simple functions, including boolean functions. 1. Given an object yourobject that has the following interface: void f(int x) int g() int h(int x, String y) Write statements showing how each method can be called on object yourobject. int x; x = 3; yourobject.f(x); x = yourobject.g(); x = yourobject.h(3,"hello"); 2. Given three integers x, y, and z, write a (nested) if-else statement prints the largest of the three values. You may use boolean operators to avoid nesting. int answer; if (x>=y && x>=z) answer = x; else if (y>=x && y>=z) answer = y; else answer = z; System.out.println(answer); or: if (x>=y) { // either x or z is largest: if (x>=z) answer = x; else answer = z; } else { // either y or z is largest if (y>=z) answer = y; else answer = z; } System.out.println(answer); 3. One of the following statements have a problem. Identify it and explain. If that statement is eliminated, what will be the resulting values of x and y? int x; double y; x = 1; y = 3.5; y = x; y = (double) x; x = y; ******* can't assign without typecast: x = (double)y; x = (int)y; 4. Percentages are sometimes represented with doubles between 0 and 1, e.g., 0.75 represents 75 percent. But percents can also be represented as integers between 0 and 100. Write a function that converts a double representation of percentage to the integer representation. That is, this function will take doubles such as 0.75, and return ints such as 75. public int percent(double x) { int answer; double temp; // temporary value temp = x * 100; answer = (int) (temp + 0.5); return answer; // alternatively, this can be written in one line: // return (int) (x*100 + 0.5); { 5. Write a boolean function "allsame" that determines if all three integers passed to it are the same. For example, allsame(3,3,3) should return true allsame(1,2,2) should return false. public boolean allsame(int a, int b, int c) { boolean answer; if (a == b && b == c) answer = true; else answer = false; return answer; // alternatively: return (a==b && b==c); } Don't worry about combining everything on one line at this point. Make sure you understand the individual steps.