/**
 * CS139 - Programming Fundamentals
 * Department of Computer Science
 * James Madison University
 * @version Spring 2016
 */

Lab03A: Scanner, Concatenation and Types

Objectives

Key Terms

data type
The kind of data that a variable can hold.
concatenation
Process of combining (or joining) two strings.
Scanner
A Java class that simplifies input processing.

Part 1: Getting Started

There is no worksheet for today's lab. Instead, you will need to download, complete, and submit the following Java files:  Concat.java  Types.java.

Part 2: Two meanings of +

In Java, the symbol + can be used to add numbers or to concatenate strings. This lab 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). 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.");

Note how the continued line is indented to indicate it is a continuation line.

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: to add!

  1. Open Concat.java. Do not compile or run the program yet.
  2. Unlike primitive data types (do you remember the 8 primitive data types?), Strings are Java classes just like the Hello program was a class. This means that strings have methods or actions that can be performed on them.
  3. This program uses the length method of the String class. length is a method that returns a value which, in this example, is the number of characters in sample. Or another way to read this is "sample's length".
  4. "Hand trace" the program. That is, act as if you were a computer and execute each statement in the program like a computer would.
  5. Write the output your hand tracing produces on your paper. Raise your hand and show the instructor your paper before proceeding.
  6. Compile and run the actual Java program. Compare your "expected result" with the "actual result".
  7. For each line where the "expected result" differed from the "actual result", do you understand why your prediction was wrong? If you cannot figure it out so you know for sure, ASK!

Correct the second output statement so blanks are printed around the 55. Recompile and run.

Before turning in this assignment, summarize what you have learned in the Javadoc comment for main. Be sure to fill in your name and today's date in the class's Javadoc comment.

Part 3: Input in Java

  1. Open Types.java.
  2. Read through the program, noticing the following new features:
    1. Scanner - The Scanner class provides a mechanism for reading in values from the keyboard (among other things).
    2. The very first line of the source file is an import statement. This import makes the Scanner class available to this program.
    3. Before we can use the Scanner, we must make an instance of it. We call an instance of a class an object. So we need to create a Scanner object.
    4. So, we first declare a Scanner variable named keyboard. Then in the initializations, we instantiate or make a new Scanner object. The value in the parentheses is System.in which refers to standard system input or the keyboard.
    5. Whenever we expect the user of a program to enter something at the command line, we have to prompt them. The line System.out.print("Enter a number: "); is performing the prompt. When you run this program, the line will display, then the program will stop executing until the user enters a value on the command line. The line num1 = keyboard.nextInt(); is the place where the keyboard Scanner is reading what the user has typed in and assigning it to the variable num1.
    6. We then "echo" the value of num1 in the following line.
  3. Compile the program and execute it. Use any integer value for num1. (Note that if you enter something that is not an integer the program will crash.)
  4. Now, duplicate the four lines at the end of the program and alter them to read a value into val1. You will need to use the nextDouble() method instead of nextInt(). There should be a blank line between this new block of code and the previous one.
  5. Compile and execute the program with your changes.
  6. Finally, duplicate the last four lines again and alter them to read a value into text. You will need to use the nextLine() method instead of nextDouble(). Make sure you are also updating the comments!

    NOTE: When reading in numbers via nextInt or nextDouble, Scanner does not read the newline character that you typed (by pressing Enter). If you want to read the next line of text, you may need to call nextLine twice! The first call reads the newline after the number, and the second call reads the next line of text. See Gaddis pages 88-91 for more details.
Raise your hand and show the instructor your working program before proceeding.

Part 4: Data types and compatibility

The last part of the lab will have you practice with different data types. The three shown in this lab are the ones you will most commonly use. Questions in green are thought questions only and do not need to be turned in.
  1. Add a statement to the end of the program to assign the value found in text to the variable val1. What happens when you try to compile it?
  2. Comment out that line and add a new line that assigns the value of val1 to num1. What happens when you try to compile it?
  3. Comment out that line and add a new line that assigns the value of val1 to val2. What happens when you try to compile it? Will it execute?
  4. Add a print statement to display the contents of val1 and val2 after the assignment.
  5. Add a statement to the end of the program to assign the value found in num1 to val2. What happens when you try to compile it? Will it execute?
  6. Add a print statement to display the contents of num1 and val2 after the assignment.
  7. What can you say about compatibility among Java data types? In other words, when can you assign values of different data types to one another? Summarize your answer in the Javadoc comment for main.

Submitting

Submit your completed .java files through Canvas. Make sure that you've included the necessary answers in your Javadoc comments.