PlusTestAnswers

PlusTestAnswers - 13 is concatenated with that string. The...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
Solution to Two Meanings of Plus Question 1, part (c) The first print statement: System.out.println ("8 plus 5 is " + 8 + 5); prints 8 plus 5 is 85 Because: There are no parentheses so evaluation is from left to right. The first plus has a string operand on the left so the operation is concatenation - the 8 is concatenated with the string and the result is a string. Now the second plus has a string on the left so it too is evaluated as concatenation - the 5 is concatenated with the string "8 plus 5 is 8" The second print statement: System.out.println ("8 plus 5 is " + (8 + 5)); prints 8 plus 5 is 13 Because: The expression 8 + 5 in parentheses is evaluated first and both operands are numeric so the numbers are added to get 13. Then the other + is concatenation since the operand on the left is a string - the
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 13 is concatenated with that string. The third print statement: System.out.println (8 + 5 + " equals 8 plus 5."); prints 13 equals 8 plus 5. Because: The expression is evaluated left to right since there are no parentheses. The leftmost + has two numeric operands so they are added to get 13. Then the second + has one string operand so the 13 and that string are concatenated. Question 1, part (d): A space should be put inside the quotation marks after the word about and another one before the word years. Click here for a corrected version of the program. Question 2: An example program is in Birds.java...
View Full Document

This note was uploaded on 03/22/2010 for the course CS 202 taught by Professor Fong during the Spring '10 term at SIU Carbondale.

Ask a homework question - tutors are online