E N D
1. ITK 177spring 2011 Shirley White
2. Java fundamentals 2-2
3. Chapter Topics Today we will discuss the following main topics
Scope
Programming style
Using the Scanner class for input
Common errors
2-3
4. Quick review -- Predict the output System.out.print("Hearing in the distance");
System.out.print("Two mandolins like creatures in the");
System.out.print("dark");
System.out.print("Creating the agony of ecstasy.");
System.out.println(" - George Barker");
How could you fix the spacing issues? 2-4
5. Scope of a variable The extent of access to a variable’s contents
Local variables
Declared inside a method
Only visible inside the method (unless passed)
Class variables
Class level data
Typically private
Accessible only through class methods by instances of the class 2-5
6. Programming Style Use of comments
Use of whitespace
Use of indentation 2-6
7. Commenting review 2-7
8. The Scanner Class To read input from the keyboard
The Scanner class is defined in java.util
Requires an import statement import java.util.Scanner;
Orimport java.util.*;
NOTimport com.sun.java_cup.internal.runtime.Scanner; 2-8
9. The Scanner Class Scanner objects work with System.in
A System object associated with the keyboard
To create a keyboard Scanner object useScanner keyboard = new Scanner (System.in);
Scanner class methods are listed in Table 2-17 pp 88-89 of the text or in the Java API
See example: Payroll.java 2-9
10. Using a Scanner Remember all declarations must appear at the top of your method…Scanner scan = new Scanner(System.in);
We will choose between 4 Scanner methods
next()
nextInt()
nextDouble()
nextLine() 2-10
11. Do This Create an application to
Get and store an integer
Get and store a double
Get and store a single token name
Get and store a multi token phrase
Report all data to the screen with labels 2-11
12. Keep your eclipse workspace organized Use a single workspace on your h-drive
Maybe a local workspace if you live off campus
Make a single java project for each lab & program
Can and often will have multiple classes
Make a java project for each chapter
For work done while reading the text
For work done in class
Name your projects reasonably 2-12
13. Common errors Mismatched braces, quotations marks, or parentheses
Misspelling keywords, class types, method names, or variables names
Attempting to use a keyword as a variable or method name
Attempting to include whitespace in variable, method name, or class name 2-13
14. More Common errors Attempting to use a variable without a declaration
Attempting to use a variable without an intitial value
Backwards variable assignment
Unintentional integer division
Order of operation logic errors 2-14
15. More Common errors Incompatible type assignment
Failing to capture returned method values
Especially from String methods
Leaving out import statements
Using the wrong import statement
Using // for multiple line comments
Incorrectly terminating multiple line comments
Etc… 2-15
16. Practice – code this IntegerInput
Request, get, and store 3 integers
Print the integers in the opposite of the order entered
Print the integer division of the 1st divided by the 2nd
Print the double division of the 1st divided by the 3rd
Print the remainder of the 1st divided by the 2nd
End IntegerInput 2-16
17. More Practice – code this StringInput
Request and get the user’s full name (storing in 3 strings: first, middle, and last)
Store all names with 1st letter capital and others lower case
Print the first name in all caps
Print the last name in all lowercase
Print the full name in Title Case (with spaces between names)
Print the full name in CamelCase (no spaces)
Print the 3 initials only (in all lower case – no spaces)
End StringInput 2-17
18. Questions?