solutions - Java Software Solutions 4e Chapter 5...

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

View Full Document Right Arrow Icon
Java Software Solutions, 4e Lewis/Loftus Chapter 5 Chapter 5 Exercise Solutions 5.1. What happens in the MinOfThree program if two or more of the 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 exactly two of the values are equal, does it matter whether the equal values are lower or higher than the third? 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. 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 is < 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 is < sum.”); } else System.out.println (“total is not equal to MAX”); 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. 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 output produced is: apple orange
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, 4e Lewis/Loftus Chapter 5 pear The second println statement is improperly indented. 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 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" "Ethel" "?-?-?-?" "{([])}" "Lucy" "ricky" "book" "******" "12345" " " "HEPHALUMP" "bookkeeper" "6789" ";+<?" "^^^^^^^^^^" "hephalump" The strings in lexicographic order: " " "******" "12345" "6789" ";+<?" "?-?-?-?" "Ethel" "HEPHALUMP" "Lucy" "^^^^^^^^^^" "book" "bookkeeper" "fred"
Background image of page 2
Java Software Solutions, 4e Lewis/Loftus Chapter 5 "hephalump" "ricky" "{([])}"
Background image of page 3

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

View Full DocumentRight Arrow Icon
Java Software Solutions, 4e Lewis/Loftus Chapter 5 5.7.
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/28/2010 for the course COMP COMP taught by Professor Huye during the Spring '10 term at Eastern Oregon.

Page1 / 10

solutions - Java Software Solutions 4e Chapter 5...

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

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