### Homework 3, CS170

• I have made an answer sheet for you to do this homework:

(You can go to the MathCS lab, login to one of the machines and print it out. Pick up your print outs by the lab rep (at the entrance of the MathCS lab). You can also print in the library, but that will cost you some money).

You must use the answer sheet

• Question 1 (20 pts)

• Give the values printed by each print statement below:

 ``` int x, y z; x = 4; y = 4; z = 5; z += y *= x -= 2; System.out.println(x); x = ____________ (3 pts) System.out.println(y); y = ____________ (3 pts) System.out.println(z); z = ____________ (4 pts) x = 3; y = 4; z = x++ * --y; System.out.println(x); x = ____________ (3 pts) System.out.println(y); y = ____________ (3 pts) System.out.println(z); z = ____________ (4 pts) ```

• Question 2 (30 pts)

• State for each assignment statement whether the statement is correct or has an error.

If there is an error, describe the error and if there is no error, give the value stored by the assignment statement.

Answer each question with the original values of the variables !!!

 ``` Original values of the variables: byte b1 = 3, b2 = 5, b3 = 7; int i1 = 3, i2 = 5, i3 = 7; double d1 = 3.0, d2 = 5.0, d3 = 7.0; String s1 = "3", s2 = "5", d3 = "7"; b1 = b2; // Error: Y / N // If Y, reason, if N, assigned value: ____________________________ b1 = -b2; // Error: Y / N // If Y, reason, if N, assigned value: ____________________________ b1 = b2 + b3; // Error: Y / N // If Y, reason, if N, assigned value: ____________________________ i1 = b2 + b3; // Error: Y / N // If Y, reason, if N, assigned value: ____________________________ d1 = b2 + b3; // Error: Y / N // If Y, reason, if N, assigned value: ____________________________ s1 = b2 + b3; // Error: Y / N // If Y, reason, if N, assigned value: ____________________________ s1 = s2 + b3; // Error: Y / N // If Y, reason, if N, assigned value: ____________________________ s1 = b3 + s2; // Error: Y / N // If Y, reason, if N, assigned value: ____________________________ s1 = b3 - b2 + s2; // Error: Y / N // If Y, reason, if N, assigned value: ____________________________ s1 = s2 + b3 - b2; // Error: Y / N // If Y, reason, if N, assigned value: ____________________________ ```

Note: there is no (minus) operation defined for String in Java.

• Question 3 (20 pts)

• State for each Boolean expression whether the expression is correct or has an error.

If there is no error, give the result (true or false) of the Boolean expression.

Answer each question with the original values of the variables !!!

 ``` Original values of the variables: int i = 4; double x = 4; 'a' > 'A' Error: Y / N If no error, result = true / false i > x && i > 3 && x > 3 Error: Y / N If no error, result = true / false 0 < i < 10 Error: Y / N If no error, result = true / false ++i > 4 Error: Y / N If no error, result = true / false i++ > 4 Error: Y / N If no error, result = true / false ```

• Question 4 (30 pts)

• You are given the following variable definition:

 ``` char c; 1111111111222 01234567890123456789012 String s = "How are you doing today"; ```

You can assume that the char typed variable c is initialized with a Unicode of some character.

Questions:

 Write Java ststement(s) that prints the message "it is a digit" if the variable c contains the code for '0', '1', '2', ..., '9' (i.e., a digit) (10 pts) (The statement(s) should not print anything otherwise). Write Java statement(s) that update the Unicode in variable c to the corresponding upper case letter if the variable c contains a code for a lower case letter. (10 pts) (The statement(s) should not update the variable c otherwise). Write a Java statement that return the string "you are today" by extracting substrings from the string variables s and concatenate them together. (10 pts)