COP 2800 (Java Programming I) Homework Assignment #2

 

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. Can the following conversions involving casting be allowed?  If so, find the converted result.
           boolean b = true;
           i = (int)b;
    
           int i = 1;
           boolean b = (boolean)i;
    
    	   String s = (String) i;
  2. What is the printout of the code in (a) and (b): if number is 30?  If number is 35?
    1.      if (number % 2 == 0)
                 System.out.println(number + " is even.");
                 System.out.println(number + " is odd.");
    2.      if (number % 2 == 0)
                 System.out.println(number + " is even.");
              else
                 System.out.println(number + " is odd.");
  3. How do you generate a random integer i such that
    1. 0 ≤ i < 20?
    2. 10 ≤ i < 20?
    3. 10 ≤ i ≤ 50?
  4. What is the value of the expression:  (ch >= 'A' && ch <= 'Z')
    1. if ch is 'A'?
    2. if ch is 'p'?
    3. if ch is 'E'?
    4. if ch is '5'?
  5. Write a Boolean expression that evaluates true if weight is greater than 50 pounds or height is greater than 60 inches.
  6. Write a switch statement that assigns to a String variable dayName with Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday, if the int variable day is 0, 1, 2, 3, 4, 5, and 6, accordingly.
  7. Evaluate the following two expressions:
          2 * 2 - 3 > 2 && 4 - 2 > 5
    
          2 * 2 - 3 > 2 || 4 - 2 > 5
  8. Do the following two loops result in the same value in sum?
    1.      for ( int i = 0; i < 10; ++i ) {
               sum += i;
           }
    2.      for ( int i = 0; i < 10; i++ ) {
               sum += i;
           }
  9. If a variable is declared in the for loop control, can it be used after the loop exits?
  10. The for loop on the left is converted into the while loop on the right.  What is wrong?  Correct it.
    for (int i = 0; i < 4; i++) {
       if (i % 3 == 0)  continue;
       sum += i;
    }
    int i = 0;
    while (i < 4) {
       if (i % 3 == 0)  continue;
       sum += i;
       i++;
    }
    1. After the break statement is executed in the following loop, which statement is executed?  Show the output.
      for (int i = 1; i < 4; i++) {
         for (int j = 1; j < 4; j++) {
            if (i * j > 2)
               break;
               System.out.println(i * j);
            }
            System.out.println(i);
      }
    2. After the continue statement is executed in the following loop, which statement is executed?  Show the output.
      for (int i = 1; i < 4; i++) {
         for (int j = 1; j < 4; j++) {
            if (i * j > 2)
               continue;
               System.out.println(i * j);
            }
         System.out.println(i);
      }

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 #2 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 #2 Questions” so I can tell which emails are questions about assignment quiz (and not submissions).