ITEC 120 LAB 2  

Part I - Two Meanings of Plus

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!

Study this program and write down (or type in a text editor) exactly what you think the output will be:

// *******************************************************************
// 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.");
   }
}

Now Save PlusTest.java to your itec120 folder on your H: drive. Compile and run the program. Did the output match what you thought it would be? If there are any discrepancies, try to figure out why.

Here are some things to consider:

The statement about when the computer was invented is too scrunched up. Fix it.


Part II - Write a Java program that prints a table with a list of at least 3 students together with their scores earned playing a game called Kaboom in the format below.

///////////////////\\\\\\\\\\\\\\\\\\\
==  Points earned playing "Kaboom"  ==     
\\\\\\\\\\\\\\\\\\\///////////////////

Name            Points  Bonus   Total
----            ------  -----   -----
Joe             43434   6792    50226
William         26353   2314    28667
Mary Sue        45789   5436    51225

The requirements for the program are as follows: