Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In Java, the symbol + can be used to add numbers or to concatenate strings. This exercise illustrates both uses.When using a string literal (a

In Java, the symbol + can be used to add numbers or to concatenate strings. This exercise illustrates both uses.When using a string literal (a sequence of characters enclosed in double quotation marks) in Java the complete string must fit on one line. The following is NOT legal (it would result in a compile-time error). System.out.println ("It is NOT okay to go to the next line in a LONG string!!!");

The solution is to break the long string up into two shorter strings that are joined using the concatenation operator (which is the + symbol). This is discussed in Section 2. 1 in the text. So the following would be legal System.out.println ("It is OKAY to break a long string into " + "parts and join them with a + symbol."); So, when working with strings the + symbol means to concatenate the strings (join them). BUT, when working with numbers the + means what it has always meant—add!

1.

Observing the Behavior of

+ To see the behavior of + in different settings do the following:

a. Study the program below, which is in file

PlusTest.java.

// ************************************************************

//

PlusTest.java

//

// Demonstrate the different behaviors of the + operator

// ************************************************************

public class PlusTest

{

// -------------------------------------------------

// main prints some expressions using the + operator

// -------------------------------------------------

public static void main (String[] args)

{

System.out.println ("This is a long string that is the " + "concatenation of two shorter strings.");

System.out.println ("The first computer was invented about" + 55 + "years ago.");

System.out.println ("8 plus 5 is " + 8 + 5);

System.out.println ("8 plus 5 is " + (8 + 5)) ;

System.out.println (8 + 5 + " equals 8 plus 5.");

}

}

b. Save PlusTest.java to your directory.

c. Compile and run the program. For each of the last three output statements (the ones dealing with 8 plus 5) write down what was printed. Now for each explain why the computer printed what it did given that the following rules are used for +. Write out complete explanations.


Step by Step Solution

3.29 Rating (146 Votes )

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Document Format ( 2 attachments)

PDF file Icon
635e38c24472c_182487.pdf

180 KBs PDF File

Word file Icon
635e38c24472c_182487.docx

120 KBs Word File

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Engineering Economy

Authors: Leland T. Blank, Anthony Tarquin

8th edition

73523439, 73523437, 978-0073523439

More Books

Students also viewed these Accounting questions

Question

What is the most common problem to avoid during interviewing?

Answered: 1 week ago