CSE 201 - Homework 3


Read Section 2.14 in Chapter 2 and Sections 3.1-3.5 in Chapter 3 in the textbook, and then answer the following questions.

  1. Given the following declarations, evaluate each of the Boolean expressions below.
  2.     boolean x = false, y = true, z = false;
    
    1. x && y
    2. y || z
    3. x || y || !z
    4. (x || y) && (y || z)
    5. (!(y || z)) || x
    6. z && x && y
    7. (!y) || (z || !x)
    8. z || (x && (y || z))
    
           
  3. Given the following declarations, write a Boolean expression for each of the problems listed below.
  4.     boolean x, y, z;
        int i, j, k;
    
    1. Write a Boolean expression that is true if and only if either x or y is true but not both
    2. 
                   
    3. Write a Boolean expression that is true if and only if x is true and y is false
    4. 
                   
    5. Write a Boolean expression that is true if and only if i has a value between -3 and 7 (excluding -3 and 7)
    6. 
                   
    7. Write a Boolean expression that is true if and only if i is an odd number
    8. 
                   
    9. Write a Boolean expression that is true if and only if the sum of i and j is equal to twice the value of k
    10. 
                   
    11. Write a Boolean expression that is true if and only if i, j, and k all have distinct values
    
           
  5. For each of the following program segments, write the output produced.
    1. int x = 12, y = -7, z = 0, a;
      if (x > y) {
        if (x > z) {
          a = x;
        }
        else {
          a = z;
        }
      }
      else {
        if (y > z) {
          a = y;
        }
        else {
          a = z;
        }
      }
      System.out.println(a);
      
    2. int number = 45;
      if (number < 10) {
        System.out.println("what");
      }
      else if (number < 50) {
        System.out.println("will");
      }
      else if (number < 100) {
        System.out.println("this");
      }
      else {
        System.out.println("print");
      }
      
    3. int x = 12, y = -7, z = 0;
      if (x > y) {
        int t = x;
        x = y;
        y = t;
      }
      if (x > z) {
        int t = x;
        x = z;
        z = t;
      }
      if (y > z) {
        int t = y;
        y = z;
        z = t;
      }
      System.out.println(x + "," + y + "," + z);
      
    
           
  6. For each of the following questions, write a code segment that solves the given problem.
  7. 
              
    1. Write a program segment that translates a letter grade into a number grade. Letter grades are A, B, C, D, E. Their numeric values are 4, 3, 2, 1, and 0, respectively. Assume that the letter grade is in a variable of type char called letterGrade. The program segment should assign the corresponding number grade to a variable of type int called numberGrade.
    2. 
                   
    3. Write a program segment that decides whether a given year is a leap year or not. A year is a leap year if it is divisible by 4, except for the case when it is also divisible by 100 and not by 400 hundred. So, for example, 1999 is not a leap year because it is not divisible by 4; 2000 is a leap year because it is divisible by 4 and although it is divisible by 100, it is also divisible by 400; 1900 however, is not a leap year because it is divisible by 100 and not by 400. Finally, 2004 is a leap year because it is divisible by 4 and not by 100.

      Assume that the year is stored in a variable of type int called year. The program segment should print to the screen "leap year" if the year stored in year is a leap year, or print "not leap year" if it is not a leap year.