Solution to
Try It Yourself: Arithmetic Expressions
- Assume you have been given:
int x = 8, y = 7, z = 1;
double a = 2.0, b = 3.5;
Evaluate the following expressions:
y * 2 + x / 3
Ans: 16
(a + 3.0) * -b + 4.5
Ans: -13.0
x % 5 + y * a
Ans: 17.0
y / a + b - x / (-4 + y)
Ans: 5.0
Math.sqrt(x * (y + z))
Ans: 8.0
- What is the value of r after the following code executes:
int p = 3;
int q = p * 7;
p = 4;
int r = q / p;
Ans: 5
- Assume you have been given:
final int QUAD = 4;
Write a Java code fragment to compute:
Answers:
(x + 7) / y + 3 / (x * QUAD)
(4.0 / 7.0) * Math.cos(y +
1 / Math.sqrt(Math.PI + x))