Chap05 EX Solutions

Chap05 EX Solutions - Java Software Solutions, 6th Edition...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Java Software Solutions, 6 th Edition Exercise Solutions, Ch. 5 Chapter 5 Exercise Solutions EX 5.1. What happens in the MinOfThree program if two or more of the values are equal? If exactly two of the values are equal, does it matter whether the equal values are lower or higher than the third? If two or more values are equal, the program still prints the lowest value. Because only less than comparisons are made, the comparison of two equal values produces a false result. If two values are equal, and lower than the third value, then one of the two lower but equal values is printed. If all three values are equal, then this value is printed. Which “version” of the equal value is irrelevant. If only two values are equal, it does not matter whether the third is lower or higher. The correct result is determined in either case. If the two equal values are lower than the third, then one of the two lower but equal values is printed. If the two equal values are higher than the third, then the third value is printed. EX 5.2. What is wrong with the following code fragment? Rewrite it so that it produces correct output. if (total == MAX) if (total < sum) System.out.println ("total == MAX and < sum."); else System.out.println ("total is not equal to MAX"); Despite the indentation, the else clause is associated with the immediately preceding if rather than the first if. The program will produce the correct output if it is rewritten as: if (total == MAX) { if (total < sum) System.out.println ("total == MAX and < sum."); } else System.out.println ("total is not equal to MAX"); EX 5.3. What is wrong with the following code fragment? Will this code compile if it is part of an otherwise valid program? Explain. if (length = MIN_LENGTH) System.out.println ("The length is minimal."); The assignment operator (=) is used erroneously in place of the equality operator (==). Hence, it will not compile in an otherwise valid program. EX 5.4. What output is produced by the following code fragment? int num = 87, max = 25; if (num >= max*2) System.out.println ("apple"); System.out.println ("orange"); System.out.println ("pear"); The second println statement is improperly indented, so the output produced is:
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Java Software Solutions, 6 th Edition Exercise Solutions, Ch. 5 apple orange pear EX 5.5. What output is produced by the following code fragment? int limit = 100, num1 = 15, num2 = 40; if (limit <= limit) { if (num1 == num2) System.out.println ("lemon"); System.out.println ("lime"); } System.out.println ("grape"); The output is: lime grape EX 5.6. Put the following list of strings in lexicographic order as if determined by the compareTo method of the String class. Consult the Unicode chart in Appendix C. "fred"
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 10

Chap05 EX Solutions - Java Software Solutions, 6th Edition...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online