  Spring Semester 2005

Lab Four: `boolean`s, Conditions, Decisions

First off here are some problems you need to have worked out before you leave the lab.

1. Write a program that asks for a positive number. If the number you enter is positive the program asks for a second number, otherwise it ends.
2. Write a program that reads a positive integer and reports if the number entered is divisible by 19 or not.
3. Write a number that asks the user to enter `1`, `2`, `3`, or `4` (as an integer) and reports "`one`", "`two`", "`three`", or "`four`" depending on the number that was entered. If the number does not match the program prints "sorry".
4. Write a program that generates a random integer between `-50` and `50` every time you call it.
5. See 2 and 4. Write a program that produces a random paper-rock-or-scissors draw for the computer.
6. Write a program that starts from the string "`123`" and adds its digits to produce `6`. (Use `charAt(`...`)` instead of `substring(`...`)`).
7. Write a program that reads a string like "`23:59`" and creates two integers out of it, one for the hours (`23`, in this case) and one for the minutes (`59`, here).
8. Write a program that converts numbers bigger than `3.85` into an `A` (by printing the letter grade `A` to the screen in response), numbers between `3.5` and `3.85` into an `A-`, numbers between `3.15` and `3.5` into a `B+` and all the other numbers into an "`Unknown`" type of message. So before you leave you need to be able to correctly answer or solve all of the problems above. You should get help from the lab instructor if you're having problems putting the right answer together.

And now the Lab Assignment for next time:

Questions to work with for practice:

1. Find the errors in the following `if` statements.
```if quarters > 0 then System.out.println(quarters + "quarters");

if (1 + x > Math.pow(x, Math.sqrt(2)) y = y + x;

if (x = 1) y++; else if (x = 2) y = y + 2;

if (x && y == 0) p = new Point2D.Double(x, y);

if (1 <= x <= 10)
{ System.out.println("Enter y:");
}

if (s != "nickels" || s != "pennies"
|| s != "dimes" || s != "quarters")
System.out.print("Input error!");

if (input.equalsIgnoreCase("N") || "NO")
return;

if (x != null) y = y + x;

language = "English";
if (country.equals("USA"))
if (state.equals("PR")) language = "Spanish";
else if (country.equals("China"))
language = "Chinese"; ```

2. Explain the following terms and give an example for each construct:

• Expression

• Condition

• Statement

• Simple statement

• Compound statement

• Block

3. What are nested `if` statements?
```

```

4. Give an example of nested `if` statement where the order of the tests does not matter. Give an example where the order of the tests matter.
```

```

5. Of the following pairs of strings, which comes first in lexicographic order?
```"Tom", "Dick"
"Tom", "Tomato"
"church", "Churchill"
"car manufacturer", "carburetor"
"Harry" hairy"
"C++", "Car"
"Tom", "Tom"
"Car", "Carl"
"car", "bar"```

6. Complete the following truth table by finding the truth values of the Boolean expressions for all combinations of the Boolean inputs `p`, `q`, and `r`.

`p` `q` `r` `(p && q) || !r` `!(p && (q || !r))`
false false false ? ?
false false true ? ?
false true false ? ?
? ? ? ? ?
? ? ? ? ?
? ? ? ? ?
? ? ? ? ?
? ? ? ? ?

7. True or false: `A && B ` is the same as `B && A ` for any Boolean conditions `A` and `B`?

8. Explain the difference between
```s = 0;
if (x > 0) s++;
if (y > 0) s++;```
and
```s = 0;
if (x > 0) s++;
else if (y > 0) s++;```

9. Use De Morgan's law to simplify the following Boolean expressions.
```!(x > 0 && y > 0)

!(x != 0 || y != 0)

!(country.equals("USA") && !state.equals("HI") && !state.equals("AK"))

!(x % 4 != 0 || !(x % 100 == 0 && x % 400 == 0))

```

10. Make up another Java code example that shows the dangling`-else` problem, using the following statement. A student with a GPA of at least 1.5, but less than 2, is on probation. With less than 1.5, the student is failing.
```

```

11. Explain the difference between the `==` operator and the `equals` method when comparing strings.

12. Explain the difference between the tests
`r == s`
and
`r.equals(s)`
where both `r` and `s` are of type `Rectangle`.

13. What is wrong with this test to see whether `r` is `null`? What happens when this code runs?
```Rectangle r;
...
if (r.equals(null))
r = new Rectangle(5, 10, 20, 30);```

14. Write Java code to test whether two objects of type `Line2D.Double` represent the same line when displayed on the graphics screen. Do not use `a.equals(b)`.
```Line2D.Double a;
Line2D.Double b;

g2.drawString("They look the same!", x, y);```
Hint: If `p` and `q` are points, then `Line2D.Double(p, q)` and `Line2D.Double(q, p)` look the same.

15. Explain why it is more difficult to compare floating-point numbers than integers. Write Java code to test whether an integer `n` equals 10 and whether a floating-point number `x` equals 10.
```

```

16. Give an example for two floating-point numbers `x` and `y` such that `Math.abs(x - y)` is larger than 1000, but `x` and `y` are still identical except for a roundoff error.
```

```

17. Consider the following test to see whether a point falls inside a rectangle.
```Point2D.Double p = ...
boolean xInside = false;
if (x1 <= p.getX() && p.getY() <= x2)
xInside = true;
boolean yInside = false;
if (y1 <= p.getY() && p.getY() <= y2)
yInside = true;
if (xInside && yInside)
g2.drawString("p is inside the rectangle.", x1, y1);```
Rewrite this code to eliminate the explicit `true` and `false` values, by setting `xInside` and `yInside` to the values of Boolean expressions.
```

```

Last updated by Adrian German for A201/A597/I210 on `Thu Feb 3 15:14:08 EST 2005`