Java Foundations Introduction to Program Design and Data Structures, 5th edition BY John Lewis
Email: Richard@qwconsultancy.com
Java Foundations, 5th Edtion
Exercise Solutions, Ch. 1
Chapter 1 Exercise Solutions EX 1.1.
Give examples of the two types of Java comments and explain the differences between them. One kind of comment begins with a double slash (//) and continues to the end of the line: // This is a single line comment. A second kind of comment begins following an initiating slash-asterisk (/*) and terminates immediately preceding a terminating asterisk-slash (*/). The second type of comment can span multiple lines: /* This is a comment that spans multiple lines. */
EX 1.2.
Which of the following are not valid Java identifiers? Why?
a. Factorial Valid b. anExtremelyLongIdentifierIfYouAskMe Valid c. 2ndLevel Invalid because it begins with a digit d. level2 Valid e. MAX_SIZE Valid f.
highest$ Valid
g. hook&ladder Invalid because it contains an ampersand (&) EX 1.3.
Why are the following valid Java identifiers not considered good identifiers?
a. q The identifier q is a meaningless name. b. totVal The identifier totalValue would be more meaningful than the abbreviation. c. theNextValueInTheList Unnecessarily lengthy; nextValue would serve as well. EX 1.4.
Java is case sensitive. What does that mean? Uppercase characters are considered to be distinct from lowercase letters. Therefore the identifier HELLO is distinct from Hello which is distinct from hello.
Java Foundations, 5th Edtion EX 1.5.
Exercise Solutions, Ch. 1
What do we mean when we say that the English language is ambiguous? Give two examples of English ambiguity (other than the example used in this chapter) and explain the ambiguity. Why is ambiguity a problem for programming languages? Something is ambiguous if it has two or more possible meanings. For example, the statement, “Mary is the nicest teaching assistant who has helped me all day long” might mean 1) of all the teaching assistants who have helped me today, Mary is the nicest, or 2) of those teaching assistants who have helped me for an entire day, Mary is the nicest. As another example, the statement, “Bananas help those who help themselves” might mean 1) bananas are good for those who attend to their own welfare or 2) bananas are good for those who eat as many bananas as they please. If a programming language statement could be interpreted in two or more ways, it would be impossible to predict with certainty how it would be interpreted and what result would be produced.
EX 1.6.
Categorize each of the following situations as a compile-time error, run-time error, or logical error.
a. multiplying two numbers when you meant to add them A logical error b. dividing by zero A run-time error c. forgetting a semicolon at the end of a programming statement A compile-time error d. spelling a word wrong in the output A logical error e. producing inaccurate results A logical error f.
typing a { when you should have typed ( A compile-time error
Java Foundations, 5th Edtion
Exercise Solutions, Ch. 2
Chapter 2 Exercise Solutions EX 2.1.
Explain the following programming statement in terms of objects and the services they provide. System.out.println("I gotta be me!"); The System.out object has a println method which accepts a character string as a parameter. The service the println method provides is to display the string on the monitor.
EX 2.2.
What output is produced by the following code fragment? Explain. System.out.print("Here we go!"); System.out.println("12345"); System.out.print("Test this if you are not sure."); System.out.print("Another."); System.out.println(); System.out.println("All done."); The output produced is: Here we go!12345 Test this if you are not sure.Another. All done. After printing its data, the println method moves to the next line of output, whereas the print method does not. A println statement with no data has the effect of moving down to the next line.
EX 2.3.
What is wrong with the following program statement? How can it be fixed? System.out.println("To be or not to be, that is the question."); The string to be printed is not all on one line. The problem can be fixed by splitting the long string into two and using the string concatenation operator (+) to reconstruct it, or by using a print statement for part of the string and a println statement for the remainder of the string.
EX 2.4.
What output is produced by the following statement? Explain. System.out.println("50 plus 25 is " + 50 + 25); The output produced is: 50 plus 25 is 5025 First the string “50 plus 25 is ” is concatenated with the integer 50; since one of the operands is a string, the result is a string. Then the string “50 plus 25 is 50” is concatenated with the integer 25, producing the final result.
EX 2.5.
What is the output produced by the following statement? Explain. System.out.println("He thrusts his fists\n\tagainst" + " the post\nand still insists\n\the sees the \"ghost\""); The output produced is: He thrusts his fists against the post and still insists
Java Foundations, 5th Edtion
Exercise Solutions, Ch. 2
he sees the "ghost" Escape characters are used to go to the beginning of new lines (\n), to tab (\t), and to print quotation marks (\"). EX 2.6.
What value is contained in the integer variable size after the following statements are executed? size = 18; size = size + 12; size = size * 2; size = size / 4; The final value stored in size is 15.
EX 2.7.
What value is contained in the floating point variable depth after the following statements are executed? depth = 2.4; depth = 20 – depth * 4; depth = depth / 5; The final value stored in depth is 2.08.
EX 2.8.
What value is contained in the integer variable length after the following statements are executed? length = 5; length *= 2; length *= length; length /= 100; The final value stored in length is 1.
EX 2.9.
Write four different program statements that increment the value of an integer variable total. total = total + 1; total += 1; total++; ++total;
EX 2.10. Given the following declarations, what result is stored in each of the listed assignment statements? int iResult, num1 = 25, num2 = 40, num3 = 17, num4 = 5; double fResult, val1 = 17.0, val2 = 12.78; a. iResult = num1 / num4; iResult is assigned 5 b. fResult = num1 / num4; fResult is assigned 5.0 c. iResult = num3 / num4; iResult is assigned 3