Second Summer 2002

Lab Notes Two: The warm-ups.

Questions:

1. Write the following mathematical expressions in Java.

2. Write the following Java expressions in mathematical notation.
```dm = m * ((Math.sqrt(1 + v / c) / Math.sqrt(1 - v / c)) - 1);

volume = Math.PI * r * r * h;

volume = 4 * Math.PI * Math.pow(r, 3) / 3; ```

3. What is wrong with this version of the quadratic formula?
```x1 = (-b - Math.sqrt(b * b - 4 * a * c)) / 2 * a;

x2 = (-b + Math.sqrt(b * b - 4 * a * c)) / 2 * a;```

4. Give an example of integer overflow. Would the same example work correctly if you used floating-point? Give an example of a floating-point roundoff error. Would the same example work correctly if you used integers? When using integers, you would of course need to switch to a smaller unit, such as cents instead of dollars or milliliters instead of liters.

5. Let `n` be an integer and `x` a floating-point number. Explain the difference between
`n = (int)x;`
and
`n = (int)Math.round(x);`
For what values of `x` do they give the same result? For what values of `x` do they give different results? What happens if `x` is negative?

6. Find at least five syntax errors in the following program.
```public class WarmUpSix {
public static void main(String[] args) {
x = 5;
int y = 3.5;
System.out.print("The sum of " + x + " and " + y " is: ");
System.out.println(x + y)
}
}```

7. Find at least three logic errors in the following program.
```public class WarmUpSeven {
public static void main(String[] args) {
int total = 1;
total = total + x1;
total = total + x1;
double average = total / 2;
System.out.println("The average of two numbers is "
+ average);
}
}```

8. Explain the difference between `2`, `2.0`, `"2"`, and `"2.0"`.

9. Explain what each of the following two program segments computes:
```x = 2;

y = x + x;```
and
```s = "2";

t = s + s; ```

10. Uninitialized variables can be a serious problem. Should you always initialize every variable with zero? Explain the advantages and disadvantages of such a strategy.

11. True or false? (`x` is an `int` and `s` is a `String`)

1. `Integer.parseInt("" + x)` is the same as `x`

2. `"" + Integer.parseInt(s)` is the same as `s`

3. `s.substring(0, s.length())` is the same as `s`

12. Give two ways for converting a number to a string. What is the advantage of each of these ways?

13. How do you get the first character of a string? The last character? How do you remove the first character? The last character?

14. How do you get the last digit of a number? The first digit? That is, if `n` is `23456`, how do you find out `2` and `6`? Do not convert the number to a string. Hint: `%`, `Math.log`

15. What is a `final` variable? Can you define a `final` variable without supplying its value?

16. For the purpose of this exercise, for each of the following cases assume:
```double x = 2.5;

double y = -1.5;

int m = 18;

int n = 4;

String s = "Hello";

String t = "World"; ```
What are the values of the following expressions?

1. `x + n * y - (x + n) * y`

2. `m / n + m % n`

3. `5 * x - n / 5`

4. `Math.sqrt(Math.sqrt(n))`

5. `(int)Math.round(x)`

6. `(int)Math.round(x) + (int)Math.round(y)`

7. `s + t`

8. `s + n`

9. `1 - (1 - (1 - (1 - (1 - n))))`

10. `s.substring(1, 3)`

11. `s.length() + t.length()`

Some advice, at the end of this document:

• You have all the answers, so there's no pressure.

• The emphasis is on learning.

• So, try to solve them on your own first.

• Then, please, make sure you check the solutions.

• Next lab you might be given questions randomly from this set of warmups.

And, as always, please let me know if you have any questions or need any help.

Last updated: Jun 14, 2002 by Adrian German for A201