ITEC 120
Formatting output

Objectives

After successfully completing this lab you will be able to format output the way you want. You'll also be able to format your code nicely. You'll understand the two meanings of plus in Java, and learn some escape sequences useful for formatting output.

Assignment

Develop a program named Plus in a file named Plus.java. You should put your Plus.java file in a a new directory in your ITEC120 directory (on your H: drive). Write header comments at the top of the file and create the class and the main method. Start with this code in your file:

/**
  * Plus.java - prints stuff formatted just so.
  *
  * @author   <your name>
  * @version  <today's date>
  */
  public class Plus
  {
      public static void main (String[] args)
      {
      }
  }

The first 6 lines of text, delimited by /* and */, are comments, and ignored by the compiler. They are notes to humans who are reading your code, like you and the person who will be grading your code.

The line "public class Plus" is the class header, containing the name of the program (class). The name of this class is Plus, with a capital P. It MUST be saved in a file called Plus.java or it will not compile. The curly braces { } contain all the code that is in the Plus class.

The line of code "public static void main (String[] args)" is the header for the main method. The main method is where a java program begins execution. Every java program you write will have a main method somewhere in the project. Later, we'll learn more about what each word in the main method header means, but for now, you'll need to type this line of code, followed by curly braces { }. You'll type the lines of your program in between the curly braces of the main method.

 

Printing output

You'll be adding lines of code to your main method that will print output to the screen. You'll use two commands to do this: System.out.print() and System.out.println()

Anything you put in between the ( ) following the print or println are parameters to the print or println method, and will get printed to the screen. If you want to print some text, you must put your text inside double quotes " ". In java, quoted text is known as a String literal. Add this line of code to your main method:

System.out.println("This is a String literal.");

"This is a String literal." is a parameter to the println method. A parameter is information that is given to a method so that the method can do it's job. The println method needs to know what information it is going to print to the screen.

Compile and run your progam.

 

Formatting your code

It's important to make your code nicely formatted as well as your output. If you are printing long Strings of output, you can break them up to make your code more readable. The following is NOT legal (it would result in a compile-time error):

System.out.println("This is a long String of output and
                    it doesn't fit nicely on one line of code
.");

To break a long String across two lines of code, you must make two shorter Strings and concatenate them with a plus sign ( + ), like so:

System.out.println("This is a long String of output " +
                   "which is broken into two Strings
.");

Add a line of code to your program like the one above, which contains a concatenated String on two lines. Compile and run your program.

 

The two meanings of Plus

When working with Strings, the plus sign means concatenation. But, when working with numbers, the plus sign means addition. Add the following line of code to your program:

System.out.println(12+5);

Compile and run. What happened?

Notice that the 12 and the 5 in this line of code are NOT in double quotes. They are not Strings. They are integers. More specifically, they are integer literals.

In Java, the plus sign has two different meaning: add or concatenate. What determines which meaning will be used? Add these lines of code to your program:

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

Take a moment and try to predict the output of these three lines of code before you run.

Compile and run. What happened? Did you correctly predict the output? When does + mean addition, and when does + mean concatenate?

 

Some handy escape sequences

There are some special characters in Java, called escape sequences, that can be used to format output. Here are some:

Sequence Meaning
\n line feed
\t tab
\" double quote
\' single quote
\\ backslash

An escape sequence in Java starts with a backslash. A "\n" is a single character, a line feed, but we don't have a single character on the keyboard that represents it, so we use "\n" to represent it. Escape sequences in Java are used inside String literals.

Add the following line of code to your program:

System.out.println("\n\n\tSome handy escape sequences:\n");

Compile and run your program.

 

Practice what you've just learned to create some nicely formatted output

Add lines of code to print out, nicely formatted, the escape sequence table shown above, a diamond made of slashes, and a line with some numbers added together. When finished, your program will output something like this:

This is a String literal.
This is a long String of output which is broken into two Strings.
17
8 plus 5 is 85
8 plus 5 is 13
13 equals 8 plus 5.


       Some handy escape sequences:

       Sequence        Meaning
         \n            line feed
         \t            tab
         \"            double quote
         \'            single quote
         \\            backslash

          /\
         /  \
        /    \
        \    /
         \  /
          \/

Adding some numbers:
         1234
         2345
       + 3456
       ------
       = 7035

 

Submit Your Assignment

Your grade for this assignment will be based on your completion of two things:

You have 2 chances to submit Lab comprehension exercises. The highest score will get recorded. If you answer questions incorrectly the first time, try to figure out what you were missing and do the exercise again.