CS110 Lab # 5

if/else statements


Instructor: Trish Cornez






      Determine the output for each of the following program segments. Assume that x and y have the following assignments prior to the execution of each if operation:

      int x = 2;
      int y = 3;
      boolean isGreen = true



    1. if (x < y)
      {
          System.out.println("x = " + x);
          System.out.println("y = " + y);
      }
          System.out.println("Apple");




    2. if (x == '2')
          System.out.println("x = " + x);
          System.out.println("y = " + y);




    3. if (!isGreen)
          System.out.println("banana");
      System.out.println("grape");




    4. if (x == y - 1)
      {
          int Temp = y;
          y = x;
          x = Temp;
          System.out.println("x = " + x);
          System.out.println("y = " + y);
      }



    5. if ((x < y) && ( y != 10))
      {
          int sum = x + y;
          System.out.println("x = " + x);
          System.out.println("y = " + y);
          System.out.println("sum = " + sum);
      }
      else {
          System.out.println("x = " + x);
          System.out.println("y = " + y);
      }



    6. if ((x > y) || ( x - y < 0))
      {
          x += 1;
          y -= 1;
          System.out.println("x = " + x);
          System.out.println("y = " + y);
      }



    7. if (x > y)
          x += 1;
      else
          y -= 1;
          System.out.println("x = " + x);
          System.out.println("y = " + y);




    8. if (x < y)
          x += 1;
          System.out.println("x = " + x);
          System.out.println("y = " + y);
      else
          y -= 1;




    9. if (x = 3)
          x += 1;
      else
          y -= 1;
          System.out.println("x = " + x);
          System.out.println("y = " + y);




    10. if (x == 3)
          x += 1;
      else if (x == 4)
          y -= 1;
      else if (x == 5)
          y -= 1;
          System.out.println("x = " + x);
          System.out.println("y = " + y);




    11. if (!(x > y))
          x += 1;
      else
      {
          y -= 1;
          System.out.println("x = " + x);
          System.out.println("y = " + y);
      }



    12. if ((x > y)) || ( x * y < 0))
      {
          x +=1;
          y -= 1;
          System.out.println("x = " + x);
          System.out.println("y = " + y);
      }
          System.out.println("x = " + x);
          System.out.println("y = " + y);




    13. if (x < y)
      {
          x +=1;
          y -= 1;
          System.out.println("x = " + x);
          System.out.println("y = " + y);
      }
          System.out.println("x = " + x);
          System.out.println("y = " + y);




          Assume that in addition to the initial x and y assignments, the following enumeration exists:

          enum Day {MON, TUE, WED, THUR, FRI, SAT, SUN}

    14. if (x > Day.WED && x < Day.THUR)
          cout << "Today is not Sunday." << endl;
      else
          cout << "Today is going to be a hard day." << endl;




    15. if (x % y == 0)
          System.out.println ("x is divisible by y.");
          System.out.println ("x is NOT divisible by y.");
      NOTE: Rewrite this segment of code using proper indentation.




    16. Write a statement that displays "Out of Range" if int number is negative or is greater than 100.




    17. Write an efficient if statement to assign number the value:
          8     if x is less than or equal to 1.5,
          7     if 1.5 < x < 2.5,
          6     if otherwise.




    18. Write Java code to do the following:
      Ask the user for the values of x, y, and code (all ints)
      if code is a 1, calculate and display the sum of x and y. Otherwise output the difference of x and y.




    19. If double a is strictly between 0 and 5, set int b equal to 1/a; otherwise set b equal to a.


    20. Create an program that allows the user to input an age in years and then displays one of the following (or similar) responses:


      Age Range                     Response
      0 < ageinput < 6 You are considered a young minor. But, in approximately ___ year(s) you will be able to attend school!
      6 <= ageinput < 16 In approximately ___ year(s) you will be able to drive!
      16 <= ageinput < 21 In approximately ___ year(s) you will be of legal drinking age.
      21 <= ageinput < 35 You are of legal drinking age. In approximately ___ year(s) you will be able to run for president!
      35 <= ageinput < 120 You are old enough to run for U.S. President.
      ageinput >= 120 You are too enough to run for U.S. President.