/** * CS139 - Programming Fundamentals * Department of Computer Science * James Madison University * @version Spring 2016 */
Practice string concatenation using the + operator.
Use a Scanner to read values from the keyboard.
Explore Java data types and their compatibility.
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.
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!
Concat.java
. Do not compile or run
the program yet.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".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.
Types.java
.Scanner
- The Scanner
class
provides a mechanism for reading in values from the keyboard
(among other things).import
statement. This import makes the Scanner
class available to this program.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.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.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
.num1
in the
following line.num1.
(Note that if you enter something that is not
an integer the program will crash.)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.text
. You will need to use the
nextLine()
method instead of nextDouble()
. Make sure you are also updating the comments!text
to the variable val1
.
What happens when you try to compile it?val1
to num1
. What happens when you try to compile it?val1
to val2
. What happens when you try to compile it? Will it
execute?val1
and val2
after the
assignment.num1
to val2
. What happens when you try to compile it? Will it execute?num1
and val2
after the
assignment.Submit your completed .java files through Canvas. Make sure that you've included the necessary answers in your Javadoc comments.