Chapter 5 Review Exercise Solutions
R5.1
a. n = 1, k = 2, r = 1
b. n = 1, k = 2, r = 2
c. n = 1, k = 1, r = 2
d. n = 1, k = 6, r = 3
R5.2
a. if (1 + x > Math.pow(x, Math.sqrt(2)) y = y + x;
Unbalanced parentheses around condition, use:
   if (1 + x > Math.pow(x, Math.sqrt(2))) y = y + x;

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

(x = 1) and (x = 2) are assignments, not comparisons, use (x == 1) 
and (x == 2) instead. 

c. int x = Integer.parseInt(input);
   if (x != null) y = y + x;
The integer value x can never be null. Use
   if (input != null) y = y + Integer.parseInt(input);

R5.3
language = "English";
if (country.equals("Canada"))
   if (stateOrProvince.equals("Quebec")) language = "Frnech";
else if (country.equals("China"))
   language = "Chinese";
The indentation levels give the wrong impression: the else actually pairs with the closest if. Use braces:
language = "English"
if (country.equals("Canada"))
{
   if (stateOrProvince.equals("Quebec")) language = "Frnech";
}
else if (country.equals("China"))
   language = "Chinese";

R5.4

a. if (x && y == 0) { x = 1; y = 1; }
Incompatible variable types: If x, y are integer variables, logical operations like && cannot be performed on them. If x, y are Boolean variables, they can't be compared to 0. Use the following instead:
   if (x == 0 && y == 0) . . .;

b. if (1 <= x <= 10)
      System.out.println(x);

Invalid condition, use if (1 <= x && x <= 10) instead. 

c. if (!s.equals("nickels") || !s.equals("pennies")
      || !s.equals("dimes") || !s.equals("quarters"))
      System.out.print("Input error!");
The condition is always true. If s must be one of the choices, use the following instead:
   if (!s.equals("nickels") && !s.equals("pennies")
      && !s.equals("dimes") && !s.equals("quarters"))
      System.out.print("Input error!");

d. if (input.equalsIgnoreCase("N") || "NO") 
      return;
Invalid compound condition, use the following instead:
if (input.equalsIgnoreCase("N") || input.equalsIgnoreCase("NO")) 
   . . .

R5.5
•	An expression is made up of constants, variables, method calls and operators, and evaluates to a single value. For example: 
   x + Math.sin(Math.PI / 2)
•	A condition is an expression that evaluates to a Boolean value 
   x == 0
•	A statement is a simple statement, a compound statement or a block statement.
•	A simple statement is a unit that can be executed. In Java, every simple statement must end with a semicolon (;)
   System.out.println();
•	A compound statement is a statement that starts with a keyword such as if or while, then is followed by other syntactical constructs depending on that keyword
   if (x == 0)
      y = z;
   else
      z = y;
•	A block statement groups several statements together. Braces are used to group statements into a block.
   {
      double root = Math.sqrt(r);
      p1 = new Point2D.Double(root, b);
      p2 = new Point2D.Double(-root, b);   
   }   
R5.6
In Java if statements with multiple else branches are used to express a choice from multiple alternatives in which exactly one from a mutually exclusive set of choices is selected to be executed. 
/* value of integer choice used to select one of four possible values for x */
if (choice == 1)  x = n;
else if (choice == 2) x = n * n;
else if (choice == 3) x = n * n * n;
else x = -2;  
Nested if statements are used to express a selection from multiple alternatives when there are multiple conditions for each selection.
/* value of integer choice and value of boolean available
used to select one of four possible values for x */
if (choice == 1)
{
   if (x > 0)
      x = n;
   else
      x = n * n;
}      
else  
{
   if (x > 0)
      x = n * n * n;
   else
      x = -2;
}
R5.7
/* Order doesn't matter */
if (choice == 1)  x = n;
else if (choice == 2) x = n * n;
else if (choice == 3) x = n * n * n;
else x = 0;  

/* Order matters */
if (choice < 1)  x = n;
else if (choice < 2) x = n * n;
else if (choice < 3) x = n * n * n;
else x = 0;
R5.8
First	Second	
Jerry	Tom	
Tom	Tomato	
Churchill	church	
car manufacturer	carburetor	
Harry	hairy	
C++	car	
Tom	Tom	
Car	Carl	
bar	car	
101	11	
1.01	10.1	
R5.9
p	q	r	(p && q) || !r	!(p && (q || !r))	
false	false	false	true	true	
false	false	true	false	true	
false	true	false	true	true	
false	true	true	false	true	
true	false	false	true	false	
true	false	true	false	true	
true	true	false	true	false	
true	true	true	true	false	
R5.10
letter	number	color	code statement	result	
g	5	?	if letter is an a, c, e or g	true	
		?	if the number is odd	true	
		“black”	color = “black”		

R5.11
Test case #1: e5 (result should be black)
Test case #2: a2 (result should be white)
Test case #3: f8 (result should be black)
Test case #4: b7 (result should be white)
R5.12
start1	end1	start2	end2	s	e	code statement	result	
10	12	11	13	?	?	if start1 > start2	false	
				11		s = start2		
						if end1 < end2	true	
					12	e = end1		
						if s < e	true	
							the appointments overlap	

start1	end1	start2	end2	s	e	code statement	result	
10	11	12	13	?	?	if start1 > start2	false	
				12		s = start2		
						if end1 < end2	true	
					11	e = end1		
						if s < e	false	
							the appointments don’t overlap	
R5.13
print “Please enter month”
month = user input
print “Please enter day”
day = user input
if month is “January” and day is 1
	 print “It’s new Year’s Day”
else if month is “July” and day is 4
	print “It’s Independence Day”
else if month is “November” and day is 11
	print “It’s Veteran’s Day”
else if month is “December” and day is 25
	 print “It’s Christmas Day”

R5.14
If either condition has a side effect, then you cannot swap A and B. If neither condition has a side effect, you can swap them. In other words, if evaluating A or B affects the expected result of evaluating the other (B or A), then you cannot swap A and B without changing the expected result. For example, these two conditions would lead to different results: 
if (n > 5 || (n = Integer.parseInt(input)) > 0) . . . 
   // will use n with its old value first 

if ((n = Integer.parseInt(input)) > 0 || n > 5) . . . 
   // will first assign n a new value and then use it 
The statements above are confusing and you should avoid this type of programming.
Another case in which you cannot swap the order is when you are depending on a lazy evaluation (expressions are evaluated from left to right, and evaluation stops as soon as the truth value is determined) to obtain a particular behavior. For example, 
if (rectangle != null && rectangle.equals(box)) . . . 
   // would work fine 

if (rectangle.equals(box) && rectangle != null) . . . 
   // yields an error if rectangle is null (equals needs an object to work on)
R5.15
In the first block, the conditions are evaluated sequentially, so s could be incremented twice. In the second block, the conditional statements are mutually exclusive, so, s cannot be incremented more than once.
R5.16
Original	De Morgan	
!(x > 0 && y > 0)	x <= 0 || y <= 0	
!(x != 0 || y != 0)	x == 0 && y == 0	
!(country.equals("US") && !state.equals("HI") && !state.equals("AK"))	!country.equals("US") || state.equals("HI") || state.equals("AK"))	
!(x % 4 != 0 || !(x % 100 == 0 && x % 400 == 0))	x % 4 == 0 && x % 100 == 0 && x % 400 == 0
Of course, this condition can now be simplified to x % 400 == 0	
R5.17
status = "GOOD";
if (GPA >= 1.5)
   if (GPA < 2.0)
      status = "PROBATION";
else
   status = "FAILING";           
Should be: 
status = "GOOD";
if (GPA >= 1.5)
{
   if (GPA < 2.0)
      status = "PROBATION";
}
else
   status = "FAILING";           
or better:
if (GPA >= 2.0)
   status = "GOOD";           
else if (GPA >= 1.5)
   status = "PROBATION";
else
   status = "FAILING";
R5.18
s == t
tests whether s and t are references to the identical String object.
s.equals(t)
tests whether s and t are strings with the same contents.
R5.19
r == s
tests whether r and s are references to the identical Rectangle object.
r.equals(s)
tests whether r and s are rectangles with the same coordinates.
R5.20
To test whether a reference is the null reference, use ==:
if (r == null)
You cannot call equals on a null reference because it is not referencing any object. If the code is run, and r is null, the program will end with an exception:
Exception in thread "main" java.lang.NullPointerException
        at Test.main(Test.java:32)
R5.21
For the lexicographic comparison, upper- and lowercase are distinguished. Also, "filler characters" such as . - and space are used in the comparison, whereas they would be ignored in the phonebook.
Lexicographic ordering is like a dictionary alphabetic ordering, but some of the characters have different values which results in their being sorted into a different position. Specifically, all white space (space, horizontal tab, vertical tab, carriage return and line feed) precede digits (0-9) which precede uppercase letters (A-Z) which precede lowercase letters(a-z). The sort order of punctuation and other printing characters is less intuitive. 
R5.22
   if ((a.getP1().equals(b.getP1()) && a.getP2().equals(b.getP2())) 
      || (a.getP1().equals(b.getP2()) && a.getP2().equals(b.getP1())))
R5.23
You have to be careful when comparing floating-point numbers, in order to cope with roundoff errors. When comparing floating-point numbers, donï¿½t test for equality. Instead, check whether they are close enough. 
To test if an integer equals ten use:
   if (n == 10) . . .
To test if a floating point number equals ten use:
   final double EPSILON = 1E-14;
   if (Math.abs(d - 10) <= EPSILON)
      // d is approximately equal to 10
R5.24
boolean xInside = r.getX() <= p.getX() && p.getX() <= r.getX() 
   + r.getWidth());
boolean yInside = r.getY() <= p.getY() && p.getY() <= r.getY() 
   + r.getHeight()); 
if (xInside && yInside) 
   . . . 
R5.25
One possible set is 8.5, 7.5, 6.5, 5, 4, 2, –1.
R5.26
If the richter is 0, a value of 0 is expected.