Unlock access to all the studying documents.
View Full Document
Java Software Solutions, 8e, Global 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?
EX 5.2. What is wrong with the following code fragment? Rewrite it so that it produces correct output.
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.
EX 5.4. What output is produced by the following code fragment?
EX 5.5. What output is produced by the following code fragment?
EX 5.6. Put the following list of strings in lexicographic order as if determined by thecompareTo method
of the String class. Consult the Unicode chart in Appendix C.
“fred”
“Ethel”
“?-?-?-?”
“12345”
” ”
“HEPHALUMP”
The strings in lexicographic order:
” ”
“******”
“12345”
EX 5.7. What output is produced by the following code fragment?
int max = 1000, min = 100;
The output produced is:
900
800
400
EX 5.8. What output is produced by the following code fragment?
EX 5.9. What is wrong with the following code fragment? What are three distinct ways it
could be changed to remove the flaw?
EX 5.10. Write a while loop that verifies that the user enters a positive integer value.
Scanner scan = new Scanner(System.in);
}
EX 5.11. Write a code fragment that reads and prints integer values entered by a user until
a particular sentinel value (stored in SENTINEL) is entered. Do not print the
sentinel value.
}
EX 5.12. Write a method called remainder that accepts two integers, divides them if second integer is not
zero and returns the value.
}
EX 5.13. Write a method called largerSum that accepts three floating point parameters (of type double)
}
EX 5.14. Write a method called complexEquation that accepts three integer parameters and returns
true, if the first parameter is not equal to the second and the product of the first two
}
EX 5.15. Write a method called isAlpha that accepts a character parameter and returns true if that
character is either an uppercase or lowercase alphabetic letter.
EX 5.16. Write a method called floatEquals that accepts three floating-point values as parameters. The
method should return true if the first two parameters are equal within the tolerance of the third
EX 5.17. Write a method called isIsoceles that accepts three integer parameters that represent the
lengths of the sides of a triangle. The method returns true if the triangle is isosceles but not
equilateral (meaning that exactly two of the sides have an equal length), and false otherwise.
}
EX 5.18. Explain what would happen if the radio buttons used in the QuoteOptionsprogram were not
organized into a ButtonGroup object. Modify the program to test your answer.
The three radio buttons used in the QuoteOptions program represent mutually exclusive choices