CS110 Java LAB 3

Assignment Statements


Instructor: Trish Cornez





Part 1: Strings



Show the final variable values for the following code segments.

  1. String str1 = "Bob";
    String str2 = "Cat";
    String str3 = str2 + "\n" + str1 + str2;



  2. String str1 = "Bob";
    String str2 = "Cat";
    String str3 = str1 + " three " + str2;
    int count = str3.length();



  3. String str1 = "LionsGate";
    String str2 = "" + str1.charAt(str1.length() - 1) + str1.charAt(0);
    char c2 = str1.charAt(4);



  4. String str1 = "chocolatecakechocolatecake!";
    String str2 = str1.substring(0, 5);





Part 2: Assignment Statements





      // CONSTANTS

      final   double   MIN   = 2;
      final   int   MAX  = 5;
      final   double   REAL  = 1;



      // VARIABLES

      int
           y = 7,
           n = 2;

      double
           x = 20.0,
           fNum = 100.0,
           t = 4;

      char
           c = 'A';
    Show the assignment values, or errors, that occur in the following sequence of instructions.

  1. x = y/n + 4;

  2. x *= 3;

  3. x = 2 / t;

  4. x = REAL * y / n;

  5. y = 2 / t;

  6. x = (double) 5/2;

  7. x = (double) (5/2);

  8. x = y % 3 + MIN / t + MAX;

  9. x = 3 - 4 * x;

  10. c = c + 35;

  11. fNum = MAX / MIN;

  12. fNum = 7.0 % 2;

  13. fNum = 2 / 4 * fNum;

  14. 'A' = c;

  15. y = 'a' % MAX;

  16. n += c;

  17. MIN = MAX + fNum /3;

  18. y = - 10 / 2 % 4;

  19. x = sqrt('b' + MIN) / MAX;

  20. x++;

  21. --y;





Part 3: Increment and Decrement Expressions



Assume the following declarations have been made:

      // VARIABLES

      int
           x = 2,
           y = 3;

           z = 5;

For each of the following, state whether or not the statement is valid. If it is valid, state the values of x, y, and z.
  1. ++x;
  2. x++;
  3. y = x++;
  4. y = ++x;
  5. x = --y;
  6. z = y--;
  7. x = y-- + 3;