CS110 Lab # 6

More with if/else statements


Instructor: Trish Cornez



PART I

      Identify the errors in the following program segments.
    1. if (x > 2 * y)
          Sum = x + y;
          Product = x * y;
      else
          Difference = x - y;




    2. if (x = y)
          Sum = x + y;
      else
          Difference = x - y;





PART II

      Re-write the following program segment as efficiently as possible.
    1. if ((x < y) && (x == 0))
          System.out.println("Red");
      if ((x < y) && ( x != 0)))
          System.out.println("White");
      if (x >= y)
          System.out.println("Blue");






PART III

Write a an interactive program which prompts the user to enter a text string from the keyboard. Depending upon the value of the first character, output one of the following:
  1. If the character is between A and Z, determine if it is a vowel or a consonant.
    output:  UPPERCASE VOWEL or UPPERCASE CONSONANT.

  2. If the character is bewteen a and z, determine if it is a vowel or a consonant.
    output:  LOWERCASE VOWEL or LOWERCASE CONSONANT.

  3. If the character is between 0 and 9, determine if it is zero, even, or odd. output:  DIGIT ZERO or DIGIT EVEN or DIGIT ODD.

  4. If the character is not defined in (1), (2) or (3), output:  SPECIAL CHARACTER.