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 three conversions involving casting be allowed?  If so, find the converted result.
        boolean b = true;
        int i = (int) b;
    
        int i = 1;
        boolean b = (boolean) i;
    
        int i = 1;
        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 (assume ch is a char):  (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 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.
  6. If a variable is declared in the for loop control, can it be used after the loop exits?
  7. The for loop on the left is converted into the while loop on the right.  What is wrong?  Correct it.
    int sum = 0;
    for (int i = 1; i <= 4; i++) {
       if (i % 3 == 0)  continue;
       sum += i;
    }
    int i = 1, sum = 0;
    while (i <= 4) {
       if (i % 3 == 0)  continue;
       sum += i;
       i++;
    }
  8. Do the following two loops result in the same value in sum?
    1.      int sum = 0;
           for ( int i = 0; i < 10; ++i ) {
               sum += i;
           }
    2.      int sum = 0;
           for ( int i = 0; i < 10; i++ ) {
               sum += 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);
      }
  9. What is wrong in the following statements?
    1. System.out.printf("%5d %d", 1, 2, 3);
    2. System.out.printf("%5d %f" , 1);
    3. System.out.printf("%5d %f" , 1, 2);
  10. Show the output of the following statements:
    1. System.out.printf("amount is %f %e\n" , 32.32, 32.32);
    2. System.out.printf("amount is %5.4f %5.4e\n", 32.32, 32.32);
    3. System.out.printf("%6b\n", (1>2));
    4. System.out.printf("%6s\n", "Java");
    5. System.out.printf("%-6b%s\n", (1>2), "Java");
    6. System.out.printf("%6b%-s\n", (1>2), "Java");
    7. System.out.printf("%6b%-8s\n", (1>2), "Java");
  11. Write a Java regular expression (“regex”), that matches en_US (standard Americian) formatted whole numbers.  Such numbers are composed of only digits and commas.  For example:
    Input Result From
    Correct Regex
    ,0False
    ,False
    1,20False
    1,234,False
    0True
    12True
    123True
    1,234True
    0,000True
    100,000True

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).