CS160: Foundations in Programming: Summer 2016
Recitation R1 - Java Variables, Data Types, and Expressions
Preview
This lab is broken into two parts, the goals are as follows. Part I:
Learn about Java variables and expressions
Understand integer versus floating point math
See how the order of precedence affects expressions
Find out the difference between println, print, and printf.
Learn how to format output using printf.
Part II:
Learn about calling methods on a String object
Learn how to use a Scanner object to read input
Getting Started
Open Eclipse by typing eclipse.sh in the terminal
Create a new Java Project named R1
Create two classes names R1P0 and R1P1
Make sure you select the
public static void main(String[] args)
method stub when you create each class
Open R1P0
Introduction to Primitive Types
Follow the instructions below, in order.
Add declarations of the following variables to the main method:
Two variables of character type (char) called c0 and c1, initialized to '$' and 'W'.
Two variables of 8-bit integer type (byte) called b0 and b1, initialized to 15 and 4.
Two variables of 16-bit integer type (short) called s0 and s1, initialized to 5577 and 1234.
Two variables of 32-bit integer type (int) called i0 and i1, initialized to 12345 and -99999.
Two variables of 64-bit integer type (long) called l0 and l1, initalized to 8000000000l and -7000000000l.
Two variables of 32-bit floating-point type (float) called f0 and f1, initialized to 1.2345f and 66.7788f.
Two variables of 64-bit floating-point type (double) called d0 and d1, initialized to 0.00001 and 83475.29837.
Print the following expressions, using System.out.println:
NOTE: The instructor will show you how to copy statements to minimize typing!
b0 + b1
(b0 + b1) / 4
(b0 + b1) / 4.0
s0 / 1000
s0 / 1000.0
s1 % 100
(i0 - 2345) * 10
i0 - 2345 * 10
l0 + l1
(f0 + f1) / (d0 * d1)
0.1 + 0.2 - 0.3
6 % 4 + 12 - 3 * (8 + 3) / 2
Create comments in your Java file that answer the following questions:
Explain the difference in the result of (b0 + b1) / 4 and (b0 + b1) / 4.0.
Explain the difference in the result of s0 / 1000 and s0 / 1000.0.
Explain the result of the modulo operation in s1 % 100.
Explain the difference in the result of (i0 - 2345) * 10 and i0 - 2345 * 10.
Explain the result of 0.1 + 0.2 - 0.3, why is this not zero?
Can you hand calculate the expression 6 % 4 + 12 - 3 * (8 + 3) / 2 and get the right answer?
Experiment with different print statements, as follows:
Increment
c0 and c1, then print (c0 + "," + c1) using System.out.println;
Print ("5577 + 1234 = " + s0 + s1) using System.out.println.
Print ("5577 + 1234 = %d\n", s0 + s1), using System.out.printf.
Print ("0.00001 + 83475.29837 = %.2f\n", d0 + d1) using System.out.printf.
Print ("0.00001 + 83475.29837 = %.5f\n", d0 + d1) using System.out.printf.
After making sure the following requirements are satisfied, turn R1P0.java into Checkin
R1P0.java compiles
R1P0.java produces 17 lines of output
Switch to R1P1.java
Learning About Reference types: Strings
Your TA will discuss the String class:
Declaring and initializing String objects
length() - returns the length of the string
indexOf() - returns the index of the specified character
charAt() - returns the character at the specified index
substring() - returns a portion of the string
toUpperCase() - converts letters in the string to uppercase
toLowerCase() - converts letter in the string to lowercase
+ - operator used for string concatenation
Your TA will discuss the Scanner class:
Declaring and initializing Scanner objects
nextInt() - reads an integer
nextDouble() - reads a double
next() - reads a token
nextLine() - reads a line of text
close() - closes scanner
Introduction to Reference Types
Add declarations for the following variables to the main method:
Three variables of type String called myString0, myString1, and myString2 initialized to "Java", "Programming", and "Language".
A variable of type integer called myInteger initialized to 0.
Create a double variable called myDouble initialized to 0.0.
Print the following expressions using System.out.println:
The concatenation of myString0 + " is a " + myString1 + " " + myString2 + ".".
The sum of the lengths of myString1 and MyString2 (must call the length() method).
The 2nd, 4th, and 7th character of myString1 (must use the charAt() method), separated by commas.
The index of 'a' in myString0 (must use the indexOf() method).
myString2 converted to uppercase (must use the toUpperCase() method).
The 3rd through 8th character of myString1 (must use the substring() method).
Create Scanner object to read from the keyboard.
Read in and print an integer:
Print the prompt "Enter an integer: " using System.out.print().
Use the Scanner object you created to read in an integer to myInteger.
Use printf to print "myInteger = " followed by the value of myInteger.
Read in and print a double:
Print the prompt "Enter a double: " using System.out.print().
Use the Scanner object you created to read in a double to myDouble.
Print "myDouble = " followed by the value of myDouble, with 5 decimal places.
After making sure the following requirements are satisfied, turn R1P1.java into Checkin
R1P1.java compiles
R1P1.java produces 10 lines of output, including the prompts
Once you have submitted R1P0.java and R1P1.java using the Checkin tab on the course website you are finished.
© 2016 CS160 Colorado State University. All Rights Reserved.