COP 2800 (Java Programming I) Homework Assignment #3

 

Due: by the start of class on the date shown on the syllabus

Description:

Answer the following questions as briefly (but completely) as possible:

  1. How do you simplify the max method in the following, using the conditional operator?
        /** Return the max of two numbers */
        public static int max ( int num1, int num2 ) {
            int result;
    
            if ( num1 > num2 )
                result = num1;
            else
                result = num2;
            return result;
        }
  2. Write method headers (just the declaration, not the bodies) for the following methods:
    1. Compute a sales commission, given the sales amount and the commission rate.
    2. Display the calendar for a month, given the month and year.
    3. Compute a square root of a number.
    4. Test whether a number is even, and returning true if it is.
    5. Display a message a specified number of times.
    6. Compute the monthly payment, given the loan amount, number of years, and annual interest rate.
    7. Find the corresponding uppercase letter, given a lowercase letter.
  3. Identify and correct the errors in the following program:
    public class Test {
        public static method1(int n, m) {
            n += m;
            method2 (3. 4);
        }
    
        public static int method2(int n) {
            if ( n > 0 ) return 1;
            else if (n == 0) return 0;
            else if (n < 0) return -1;
        }
    }
  4. What is pass-by-value?  Show the results of the following two programs.
    1. public class Test {
          public static void main ( String [] args ) {
              int max = 0;
              max(1, 2, max);
              System.out.println(max);
          }
      
          public static void max ( int value1, int value2, int max ) {
              if ( value1 > value2 )
                  max = value1;
              else
                  max = value2;
          }
      }
    2. public class Test {
          public static void main ( String [] args ) {
              int i = 1;
              while ( i <= 6 ) {
                  method1( i, 2 );
                  i++;
              }
          }
      
          public static void method1 ( int i, int num ) {
              for ( int j = 1; j <= i; j++ ) {
                  System.out.print( num + " " );
                  num *= 2;
              }
              System.out.println();
          }
      }
  5. What is wrong with the following program?
    public class Test {
        public static void method ( int x ) {
        }
        public static int method ( int y ) {
            return y;
        }
    }
    1. Write an expression that obtains a random integer between 34 and 55.
    2. Write an expression that obtains a random integer between 0 and 999.
    3. Write an expression that obtains a random number between 5.5 and 55.5.
    4. Write an expression that obtains a random lowercase (English) letter.
  6. How many times is the factorial method in the following invoked, for the call factorial(6)?
    import java.util.Scanner;
    
    public class ComputeFactorial {
        public static void main ( String [] args ) {
            Scanner input = new Scanner( System.in );
            System.out.print( "Enter a nonnegative integer: " );
            int n = input.nextInt();
    
            // Display factorial
            System.out.println( "Factorial of " + n + " is " + factorial(n) );
        }
    
        /** Return the factorial for the specified number */
        public static long factorial ( int n ) {
            if ( n == 0 ) // Base case
                return 1;
            else
                return n * factorial( n - 1 ); // Recursive call
        }
    }
  7. Which of the following statements are valid?
    1. int i = new int(30);
    2. double d[] = new double[30];
    3. char[] r = new char(1..30);
    4. int i[] = (3, 4, 3, 2);
    5. float f[] = {2.3, 4.5, 6.6};
    6. char[] c = new char();
  8. Write Java statements to do the following:
    1. Create an array to hold 10 double values.
    2. Assign the value 5.5 to the last element in the array.
    3. Display the sum of the first two elements.
    4. Write a loop that computes the sum of all elements in the array.
    5. Write a loop that finds the minimum element in the array.
    6. Randomly generate an index and display the element of this index in the array.
    7. Use an array initializer to create another array with the initial value 3.5, 5.5, 4.52, and 5.6.
  9. Use the following illustration as an example, show how to apply the binary search approach to a search first for key 10 and then key 12, in the list:
    [2, 4, 7, 10, 11, 45, 50, 59, 60, 66, 69, 70, 79].
    key is 11
             0   1   2   3   4   5   6   7   8   9  10  11  12
    11<50  [ 2,  4,  7, 10, 11, 45, 50, 59, 60, 66, 69, 70, 79]
           low=0                   mid=6                   hi=12
    
    11>7   [ 2,  4,  7, 10, 11, 45, 50, 59, 60, 66, 69, 70, 79]
           low=0    mid=2      hi=5
    
    11=11  [ 2,  4,  7, 10, 11, 45, 50, 59, 60, 66, 69, 70, 79]
                       low=3    hi=5
                           mid=4

    (Note how binary search eliminates half of the list from further consideration after each comparison.)

  10. What types of array can be sorted using the Java.util.Arrays.sort method?  Does this sort method create a new array?
  11. Which of the following statements are valid?
    1. int[][] r = new int[2];
    2. int[] x = new int[];
    3. int[][] y = new int [3][];
    4. int[][] z = {{1, 2}};
    5. int[][] m = {{1, 2}, {2, 3}};
    6. int[][] n = {{1, 2}, {2, 3}, };
  12. Which of the following statements will throw an exception?
    System.out.println( 1 / 0 );
    System.out.println( 1.0 / 0 );
  13. Point out the problem in the following code.  Does the code throw any exceptions?
    long value = Long.MAX_VALUE + 1;
    System.out.println( value );
  14. What is a checked exception, and what is an unchecked exception?

To be turned in:

Email your homework assignment, by copy-and-paste (no attachments please), to (homework submission).  If possible use the “text” and not the “HTML” mode of your email program.  Please use the subject similar to “Java Programming I Homework Assignment #3 Submission”, so I can tell which emails are submitted homework assignments.

Homework assignments will not be returned.  Please do not send as attachments.  Refer to the Homework and the Submitting Assignments sections of your syllabus for more information.

Confused?  Send questions about the homework assignment to (homework questions).  Please use a subject similar to “Java Programming I Homework Assignment #3 Questions” so I can tell which emails are questions about assignment quiz (and not submissions).