CSE 201 - Homework 4


Read Section 3.10 in Chapter 3 and Sections 4.1-4.3 in Chapter 4 in the textbook, and then answer the following questions.

  1. For each of the following program segments, write the output produced.
    1. int i = 0, j = 0;
      while (i < 6) {
        j = j + i;
        i = i + 1;
      }
      System.out.println(j);
      
    2. 
                   
    3. int i = 0, j = 10;
      while (i <= j) {
        if (i % 2 == 0) {
          System.out.println(i);
        }
        i = i + 1;
      }
      
    4. 
                   
    5. int i = 11;
      while (i > 0) {
        int j = 2;
        while (j <= i) {
          System.out.print(j + " ");
          j = j + 2;
        }
        i = i - 3;
        System.out.println();
      }
      
    6. 
                   
    7. int n = 4;
      while (n != 1) {
        System.out.print(n + " ");
        if (n % 3 != 0) {
          n = 2 * n + 1;
        }
        else {
          n = n / 3;
        }
      }
      
    8. 
                   
    9. int pos = 0;
      String a = "camera", b = "computer";
      while (pos < a.length()) {
        if (a.charAt(pos) == b.charAt(pos)) {
          System.out.print(b.charAt(pos));
        }
        pos = pos + 1;
      }
      
    
           
  2. For each of the following questions, write a code segment that solves the given problem. Note that some questions provide separate sample input and output, instead of giving an example of the interaction of the code with the user (as is given in the lab assignments). The inputs and outputs may or may not be interleaved in an actual run of the code, depending on how the code is implemented. By definition, input is whatever the user types in and the program reads, and output is whatever the program writes to the screen. If you are confused by this, make sure you ask your instructor about it as soon as possible.
  3. 
              
    1. Write a program segment that reads a sequence of integers until a 0 is encountered, and outputs the number of non-zero numbers read. For example, if the input is:
          2
          13
          -7
          21
          -11
          0
      
      The code will read in all the integers, and produce the following output:
          Number of non-zero integers: 5
      
    2. 
                   
    3. Write a program segment that reads a sequence of integers until a 0 is encountered, and outputs the number of even integers and the number of odd integers read (not counting the final 0). For example, if the input is:
          2
          13
          -7
          21
          -11
          0
      
      The code will read in all the integers, and produce the following output:
          Number of even integers: 1
          Number of odd integers: 4
      
    4. 
                   
    5. Write a program segment that reads a sequence of integers until a 0 is encountered. For each number read, output the number of times the number can be divided by 2 before you get 0. For example, if the input is:
          2
          9
          -7
          4
          -3
          0
      
      The code will read in all the integers, and produce the following output:
          2
          4
          3
          3
          2
      
    6. 
                   
    7. Given a String variable s, write a code segment that outputs each letter in the string twice. For example, if s = "Hello", the code should output "HHeelllloo".
    8. 
                   
    9. Given an int variable size, write a program segment that outputs a triangle of asterisks '*', which is size rows high, and size columns wide, starting with a row of size asterisks, and ending with a row of a single asterisk. For example, if size = 5, the output should be:
      *****
      ****
      ***
      **
      *