# Variables, Expressions, and Assignments

1. Calculate the value and type of each expression given assuming the following declarations and assignments have been made:

double a = 2.1, b = 27.0;
int c = 27, d = 4;

1. c + d
2. 2*d + c
3. c / d
4. b / d
5. 1.0 * c / d
6. b / 3 - - d
7. b % 5 + c % 5
8. ++a + d++
2. Write Java expressions that will evaluate to the following formulas (You may assume that all of the variables used in the formula have previously been declared and initialized.):

1. $E = mc^2$

2. $\displaystyle{x = \frac{10(-5+x)}{2} + \frac{3}{4}}$

3. $\displaystyle{x = \frac{-b + \sqrt{b^2 - 4ac}}{2a}}$

4. $c = \sin^2 x + \cos^2 x$

5. $\displaystyle{\frac{1}{s \sqrt{2 \pi}} e ^{ - \frac{(y-m)^2}{2s^2}}}$

6. $\displaystyle{\frac{\log(10y)}{\log(2)}}$

3. For each statement below, state whether the statement is correct or has an error. If there is an error, describe it. If there is no error, give the value stored by the assignment statement. Evaluate each statement in accordance with the original values of the variables. That is to say, assume each question is independent -- the results of any one question don’t affect any other questions.

Original values of the variables:

int i1 = 2, i2 = 5, i3 = 7;
long L1 = 2, L2 = 5, L3 = 7;
boolean b = true;
double d1 = 2.0, d2 = 5.0, d3 = 7.0;
char c1 = '2', c2 = '5', c3 = '7';
String s1 = "2", s2 = "5", s3 = "7";

1. i1 = i1++;
2. i1 = L1;
3. L1 = i1 * i2;
4. L3 = d2 / d1;
5. c3 = c1 + c2;
6. c3 = (char)(c1 + c2);
7. d1 = L1 + L2 + L3;
8. i3 = (i2 = i1 + 1);
9. i1 = (int)(d2 / d1);
10. b = c1 < c2 & cd2 <= c3;
11. b = true
12. b = 10 <= i1 * i2 && b;
13. int num; i1 = num * 10;
1. b = s1 >= s2 && i1 <= i2;
2. i3 %= i2;
3. L1 + s1 + s2 + s3;
4. i1 = s1 * s2;
5. i1 = c1 * c2;
6. c1 + i2 / i1;
7. ++c1;
8. s1 = L1 + L2 + L3 + s2;
9. Integer.parseInt(s1 + s2);
10. i3 = i1-- - -++i2 * 3;
11. c1-'0'==i1 && c2-'0'==i2;
12. L1 *= i2;

4. Suppose the symbol "^" is used to denote the binary XOR operation. Let $A$ and $B$ be two integers values of your choosing. Find their binary representations and use them to compute $A$ ^ $(A$ ^ $B)$ and $B$ ^ $(A$ ^ $B)$. Then do the same for some different values of $A$ and $B$. What do you notice? Can you prove what you observe always happens?