JavaHTP6e 02
JavaHTP6e 02
2
Introduction
to Java Applications
2 // Text-printing program.
Outline
3
8 {
10
12
– Blank line
• Makes program more readable
• Blank lines, spaces, and tabs are white-space characters
– Ignored by compiler
– Java identifier
• Series of characters consisting of letters, digits,
underscores ( _ ) and dollar signs ( $ )
• Does not begin with a digit, has no spaces
• Examples: Welcome1, $value, _value, button7
– 7button is invalid
• Java is case sensitive (capitalization matters)
– a1 and A1 are different
– In chapters 2 to 7, use public class
• Certain details not important now
• Mimic certain features, discussions later
– Saving files
• File name must be class name with .java extension
• Welcome1.java
5 {
– Left brace {
• Begins body of every class
• Right brace ends declarations (line 13)
8 {
5 { 1. Comments
11 4.1 Method
System.out.print
12 } // end method main ln
13 5. end main,
Welcome2
14 } // end class Welcome2
Program Output
– Line breaks at \n
10
12
•System.out.printf
– New feature of J2SE 5.0
– Displays formatted data
9 System.out.printf( "%s\n%s\n",
10 "Welcome to", "Java Programming!" );
– Format string
• Fixed text
• Format specifier – placeholder for a value
– Format specifier %s – placeholder for a string
8 { printf
9 System.out.printf( "%s\n%s\n",
System.out.printf
displays formatted data.
10 "Welcome to", "Java Programming!" );
11
13
Welcome to
Java Programming! Program output
– import declarations
• Used by compiler to identify and locate classes used in Java
programs
• Tells compiler to load class Scanner from java.util
package
5 public class Addition
6 {
– Assignment statement
• Calculates sum of number1 and number2 (right hand side)
• Uses assignment operator = to assign result to variable sum
• Read as: sum gets the value of number1 + number2
• number1 and number2 are operands
2.7 Arithmetic
Fig. 2.20 | Use case diagram for the ATM system from the user's perspective.
Fig. 2.21 | Use case diagram for a modified version of our ATM system that also allows
users to transfer money between accounts.