Objectives: |
|
Background: |
Java is a strongly typed language. In some cases, widening conversion may be done to make both operands the same type before the operation is carried out. In other cases, automatic conversion cannot be done. This lab explores some of these features while also exploring the Scanner class. You will find information about Scanner in chapter 2.13 of the text. |
New Terms: |
|
Materials Needed: |
|
Prerequisite |
|
Submission: |
Upload these items via the Canvas assignment link:
|
Part 1: Startup
Set up your programming environment for this lab. Create a folder on your network space, jump drive, or desktop and download the files found in the materials section.
For your teams, decide who will start out as the driver and who will be the navigator. You will turn in one assignment for both partners.
Part 2: Introduction to Input in Java
- Download the program Types.java.
- Open it up in the editor of your choice.
- Read through the program, noticing the following new features:
- Scanner - The Scanner class provides a mechanism for reading in values from the keyboard (among other things).
- The very first line of the program is an import statement. This import makes the
Scanner class available to this program.
- Before we can use the Scanner, we
must make and instance of it. We call an instance of a class an object.
So we need to create a Scanner object.
- 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.
- 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.
- We then echo the value of num1 in the following line.
- Compile the program and execute it using any integer value for num1. If you enter something that is not an integer the program will crash (have a run-time error);
- Now, duplicate the three 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().
- Compile and execute the program with your changes.
- Finally, duplicate the last three lines and alter them to read a
value into text. You will need to
use the nextLine() method instead of nextDouble(). NOTE: When
reading in both numbers and text, you need to be aware that Scanner
leaves in the new line character that you enter as return. When you read a number followed by text you
need to have a single line which will consume the new line character.
So you would need to add the line keyboard.nextLine();
after the prompt and before the actual read of the text value. See
Gaddis, page 88-91 of either text.
Part 3: 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. Note: questions are thought questions only and do not need
to be turned in.
- 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?
- Comment out that line (inline comments) and add a new line that assigns the value of val1 to num1. What happens when you try to compile it?
- 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?
- Add a print statement to display the contents of val1 and val2 after
the assignment.
- 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?
- Add a print statement to display the contents of num1 and val2 after
the assignment.
- 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?
Part 4: The cast operation
Let's say that you wanted only the integer portion of the value in val1. To do this, you must "cast" the value of val1 as an integer. Casting tells the compiler that it should treat the value as if it were the cast type. See an example in your book, section 2.7, Conversion between Primitive Data Types. NOTE: This is changing the value but not the contents of val1.- Uncomment the line you created in Step 4.2 (assigning val1 to num1).
- Add a cast operation to convert val1's value to an integer.
- Compile and execute the program.
Part 5: Optional - Going further
- Look at Programming Challenges, number 1 in the back of Chapter 2.
- Write a new program called NameAgeIncome that will read in the three values described and print them back out again as described in the assignment.
- The income will print with as many decimal places as there are in
the number with a minimun of 2. How can you format the number to print
it nicely (in currency format). Hint: Look for the DecimalFormatting
class or printf.
Updated 01/27/2015 (nlh)