Class: COP 2800 Java Programming Homework #3 Model Solution 1) How do you simplify the max method in the following, using the conditional operator? 1: /** Return the max of two numbers */ 2: public static int max ( int num1, int num2 ) { 3: int result; 4: 5: if ( num1 > num2 ) 6: result = num1; 7: else 8: result = num2; 9: return result; 10: } public static int max ( int num1, int num2 ) { return num1 > num2 ? num1 : num2; } 2) Write method headers (just the declaration, not the bodies) for the following methods: a. Compute a sales commission, given the sales amount and the commission rate. b. Display the calendar for a month, given the month and year. c. Compute a square root of a number. d. Test whether a number is even, and returning true if it is. e. Display a message a specified number of times. f. Compute the monthly payment, given the loan amount, number of years, and annual interest rate. g. Find the corresponding uppercase letter, given a lowercase letter. a. public static int sales_commission ( int salesAmount, int commissionRate ) b. public static void displayCalendar ( int month, int year ) c. public static double sqrt ( double number ) d. public static boolean isNumEven ( int num ) e. public static void displayMessage ( String message, int numTimes ) f. public static double monthlyPayment ( double loanAmount, int numYears, double annualIntrestRate ) g. public static int toUppercase ( int letter ) // Use int for Unicode Use int instead of char will accommodate Unicode. 3) Identify and correct the errors in the following program. 1. public class Test { 2. public static method1(int n, m) { 3. n += m; 4. method2 (3. 4); 5. } 6. 7. public static int method2(int n) { 8. if ( n > 0 ) return 1; 9. else if (n == 0) return 0; 10. else if (n < 0) return -1; 11. } 12. } Without comments, you can see there is a problem but not what was intended, so you must guess (and document in a comment) how to fix. Here's one reasonable way: public class Test { public static void method1 ( int n, int m ) { // missing "void" and "int". n += m; // Does nothing; could be deleted. method2( 3.4 ); // space removed; method2 changed to match (double). } public static int method2 ( double n ) { // change int to double. if ( Double.isNaN( n ) ) throw new ArithmeticException(); // I bet Liang forgot this check. :-) if ( n > 0.0 ) return 1; // for clarity, changed 0 to 0.0. if (n == 0.0 ) return 0; // eliminated needless else, but not an error. else return -1; // eliminated needless if statement. } } 4) What is pass-by-value? Show the results of the following two programs. a. 1: public class Test { 2: public static void main ( String [] args ) { 3: int max = 0; 4: max(1, 2, max); 5: System.out.println(max); 6: } 7: 8: public static void max ( int value1, int value2, int max ) { 9: if ( value1 > value2 ) 10: max = value1; 11: else 12: max = value2; 13: } 14: } b. 1: public class Test { 2: public static void main ( String [] args ) { 3: int i = 1; 4: while ( i <= 6 ) { 5: method1( i, 2 ); 6: i++; 7: } 8: } 9: 10: public static void method1 ( int i, int num ) { 11: for ( int j = 1; j <= i; j++ ) { 12: System.out.print( num + " " ); 13: num *= 2; 14: } 15: System.out.println(); 16: } 17: } "Pass-by-value" is when the "actual" arguments are passed as if by assignment to the "formal" parameters, when calling a method. Output of program (a): (It is legal, but confusing, to use the same for a variable and a method.) 0 Output of program (b): 2 2 4 2 4 8 2 4 8 16 2 4 8 16 32 2 4 8 16 32 64 5) What is wrong with the following class? 1: public class Test { 2: public static void method ( int x ) { 3: } 4: public static int method ( int y ) { 5: return y; 6: } 7: } Overloaded methods must have different signatures; here, both have the same signature of "method(int)". 6) a. Write an expression that obtains a random integer between 34 and 55, inclusive. b. Write an expression that obtains a random integer between 0 and 999, inclusive. c. Write an expression that obtains a random number between 5.5 (inclusive) and 55.5 (exclusive). d. Write an expression that obtains a random lowercase (English) letter. a) 34 + (int) (Math.random() * (55 - 34 + 1) b) 0 + (int) (Math.random() * (999 - 0 + 1) c) 5.5 + Math.random() * (55.5 - 5.5 + 1) d) 'a' + (int) (Math.random() * ('z' - 'a' + 1) 7) How many times is the factorial method in the following invoked, for the call factorial(6)? 1: import java.util.Scanner; 2: 3: public class ComputeFactorial { 4: public static void main ( String [] args ) { 5: Scanner input = new Scanner( System.in ); 6: System.out.print( "Enter a nonnegative integer: " ); 7: int n = input.nextInt(); 8: 9: // Display factorial 10: System.out.println( "Factorial of " + n + " is " + factorial(n) ); 11: } 12: 13: /** Return the factorial for the specified number */ 14: public static long factorial ( int n ) { 15: if ( n == 0 ) // Base case 16: return 1; 17: else 18: return n * factorial( n - 1 ); // Recursive call 19: } 20: } The method is invoked 7 times with input "6": factorial(6), factorial(6) = 6 * factorial(5), factorial(6) = 6 * 5 * factorial(4), factorial(6) = 6 * 5 * 4 * factorial(3), factorial(6) = 6 * 5 * 4 * 3 * factorial(2), factorial(6) = 6 * 5 * 4 * 3 * 2 * factorial(1), factorial(6) = 6 * 5 * 4 * 3 * 2 * 1 * factorial(0) (You can add "static int count;" between lines 4 and 5, and add "++count;" between lines 14 and 15, and print out count between (the original) lines 10 and 11.) 8) Suppose that the class F is defined as shown below. Let f be an instance of F. Which of the following statements are syntactically correct? (a) public class F { int i; static String s; void iMethod() { } static void sMethod() { } } a. System.out.println(f.i); // CORRECT b. System.out.println(f.s); // CORRECT c. f.iMethod(); // CORRECT d. f.sMethod(); // CORRECT e. System.out.println(F.i); // WRONG f. System.out.println(F.s); // CORRECT g. F.iMethod(); // WRONG h. F.sMethod(); // CORRECT 9) Add the static keyword in the place of ?, if appropriate, in the code below. 1: public class Test { 2: private int count; 3: public ? void main ( String [] args ) { 4: ... 5: } 6: public ? int getCount () { 7: return count; 8: } 9: public ? int factorial ( int n ) { 10: int result = 1; 11: for ( int i = 1; i <= n; i++ ) 12: result *= i; 13: return result; 14: } 15: } ANSWER - Line 3 must be static, line 6 must not be static, and line 9 should be static but is correct either way. 10) Can each of the following statements be compiled? a. Integer i = new Integer("23"); ANSWER - yes b. Integer i = new Integer(23); ANSWER - yes c. Integer i = Integer.valueOf("23"); ANSWER - yes d. Integer i = Integer.parseInt("23", 8); ANSWER - yes e. Double d = new Double(); ANSWER - NO f. Double d = Double.valueOf("23.45"); ANSWER - YES g. int i = (Integer.valueOf("23")).intValue(); ANSWER - YES h. double d = (Double.valueOf("23.4")).doubleValue(); ANSWER - YES i. int i = (Double.valueOf("23.4")).intValue(); ANSWER - YES j. String s = (Double.valueOf("23.4")).toString(); ANSWER - YES 11) a. How do you convert an integer into a string? b. How do you convert a numeric string into an integer? c. How do you convert a double number into a string? d. How do you convert a numeric string into a double value? ANSWER - a. Integer.toString( someInt ); b. int i = Integer.parseInt( "123" ); c. Double.toString( someDouble ); d. double d = Double.parseDouble( "123.69" ); end