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

Info icon This 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
Image of page 1

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

View Full Document Right 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"
Image of page 2
Java Software Solutions, 4e Lewis/Loftus Chapter 5 "hephalump" "ricky" "{([])}"
Image of page 3

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

View Full Document Right Arrow Icon
Java Software Solutions, 4e Lewis/Loftus Chapter 5 5.7. What output is produced by the following code fragment?
Image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern