|
Home | Résumé | Courses | Contact | Useful Links | Favorite Links | USC - Homepage |
Introduction to Programming - (CPTR125) - Lectures
JAVA Lecture 2 - Version 2.1
Lecture Outline
- A First Program in Java: Printing a Line of Text
- Modifying Our First Java Program
- Displaying Text in a Dialog Box
• Application
– Program that executes using the java interpreter
• Sample program
– Show program, then analyze each line
1 // Fig. 2.1: Welcome1.java
2 // Text-printing program.
3
4 public class Welcome1 {
5
6 // main method begins execution of Java application
7 public static void main( String args[] )
8 {
9 System.out.println( "Welcome to Java Programming!" );
10
11 } // end method main
12
13 } // end class Welcome1
Welcome to Java Programing!
A First Program in Java: Printing a Line of Text (explanation)
1 // Fig. 2.1: Welcome1.java
–Comments start with: // (one line only)
• Comments ignored during program execution
• Document and describe code
• Provides code readability
– Traditional comments: /* ... */
/* This is a traditional
comment. It can be
split over many lines */
2 // Text-printing program.
–Another line of comments
Note: line numbers are not part of the programs, added for reference
3
–Blank line
• Makes program more readable
• Blank lines, spaces, and tabs are white-space characters
– Ignored by compiler
4 public class Welcome1 {
–Begins class declaration for class Welcome1
• Every Java program has at least one user-defined class
• Keyword: words reserved for use by Java–class keyword followed by class name
• Naming classes: capitalize every word (recommended)
– SampleClassName
–Name of class called 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
–Saving files
• File name must be class name with .java extension
• Welcome1.java
– Left brace {
• Begins body of every class
• Right brace ends declarations (line 13)
7 public static void main( String args[] )
–Part of every Java application
• Applications begin executing at main
– Parenthesis indicate main is a method
– Java applications contain one or more methods
• Exactly one method must be called main
– Methods can perform tasks and return information
• void means main returns no information
8 {
–Left brace begins body of method declaration
• Ended by right brace } (line 11)
9 System.out.println( "Welcome to Java Programming!" );
–Instructs computer to perform an action
• Prints string of characters
– String - series characters inside double quotes
• White-spaces in strings are not ignored by compiler
– System.out
• Standard output object
• Print to command window (i.e., MS-DOS prompt)
– Method System.out.println
• Displays line of text
• Argument inside parenthesis
– This line known as a statement
• Statements must end with semicolon ;
11 } // end method main
- Ends method declaration
13 } // end class Welcome1
–Ends class declaration
– Can add comments to keep track of ending braces
– Lines 8 and 9 could be rewritten as:
– Remember, compiler ignores comments
– Comments can start on same line after code
Compiling a program
•Compiling a program
– Open a command prompt window, go to directory where program is stored
– Type javac Welcome1.java
– If no errors, Welcome1.class created
• Has bytecodes that represent application
• Bytecodes passed to Java interpreter
Executing a program
– Type java Welcome1
• Interpreter loads .class file for class Welcome1
• .class extension omitted from command
– Interpreter calls method main
![]()
Executing Welcome1 in a Microsoft Windows 2000 Command Prompt.
Modifying Our First Java Program
Modify the previous example to print same contents using different code
•Modifying programs
– Welcome2.java (Fig. 2.3) produces same output as Welcome1.java (Fig. 2.1)
– Using different code
9 System.out.print( "Welcome to " );
10 System.out.println( "Java Programming!" );
Line 9 displays “Welcome to ” with cursor remaining on printed line
Line 10 displays “Java Programming! ” on same line with cursor on next line
1 // Fig. 2.3: Welcome2.java
2 // Printing a line of text with multiple statements.
3
4 public class Welcome2 {
5
6 // main method begins execution of Java application
7 public static void main( String args[] )
8 {
9 System.out.print( "Welcome to " );
10 System.out.println( "Java Programming!" );
11
12 } // end method main
13
14 } // end class Welcome2
Welcome to
Java Programming!
• New line characters (\n)
– Interpreted as “special characters” by methods System.out.print and System.out.println
– Indicates cursor should be on next line
– Welcome3.java (Fig. 2.4)
9 System.out.println( "Welcome\nto\nJava\nProgramming!" );
–Line breaks at \n
• Usage
– Can use in System.out.println or System.out.print to create new lines
• System.out.println( "Welcome\nto\nJava\nProgramming!" );
1 // Fig. 2.4: Welcome3.java
2 // Printing multiple lines of text with a single statement.
3
4 public class Welcome3 {
5
6 // main method begins execution of Java application
7 public static void main( String args[] )
8 {
9 System.out.println( "Welcome\nto\nJava\nProgramming!" );
10
11 } // end method main
12
13 } // end class Welcome3
Welcometo
Java
Programming!
Escape characters
Backslash ( \ ) indicates special characters be output
Displaying Text in a Dialog Box
•Display
– Most Java applications use windows or a dialog box
• We have used command window
– Class JOptionPane allows us to use dialog boxes
• Packages
– Set of predefined classes for us to use
– Groups of related classes called packages•Group of all packages known as Java class library or Java applications programming interface (Java API)
– JOptionPane is in the javax.swing package
• Package has classes for using Graphical User Interfaces (GUIs)
Upcoming program
–Application that uses dialog boxes
– Explanation will come afterwards
– Demonstrate another way to display output
– Packages, methods and GUI
1 // Fig. 2.6: Welcome4.java
2 // Printing multiple lines in a dialog box.
3
4 // Java packages
5 import javax.swing.JOptionPane; // program uses JOptionPane
6
7 public class Welcome4 {
8
9 // main method begins execution of Java application
10 public static void main( String args[] )
11 {
12 JOptionPane.showMessageDialog(null, "Welcome\nto\nJava\nProgramming!" );
1314
15 System.exit( 0 ); // terminate application with window
16
17 } // end method main
18
19 } // end class Welcome4
Lines 1-2: comments as before
4 // Java packages
–Two groups of packages in Java API
– Core packages
• Begin with java
• Included with Java 2 Software Development Kit
– Extension packages
• Begin with javax
• New Java packages
5 import javax.swing.JOptionPane; // program uses OptionPane
–import declarations
• Used by compiler to identify and locate classes used in Java programs
• Tells compiler to load class JOptionPane from javax.swing package
Lines 6-11: Blank line, begin class Welcome4 and main
12 JOptionPane.showMessageDialog(null, "Welcome\nto\nJava\nProgramming!" );
–Call method showMessageDialog of class JOptionPane
• Requires two arguments
• Multiple arguments separated by commas (,)
• For now, first argument always null•Second argument is string to display
– showMessageDialog is a static method of class JOptionPane
• static methods called using class name, dot (.) then method name
Displaying Text in a Dialog Box
- All statements end with ;
- A single statement can span multiple lines
- Cannot split statement in middle of identifier or string
- Executing lines 12 and 13 displays the dialog box
•Automatically includes an OK button
– Hides or dismisses dialog box
• Title bar has string Message
15 System.exit( 0 ); // terminate application with window
– Calls static method exit of class System
• Terminates application
– Use with any application displaying a GUI
• Because method is static, needs class name and dot (.)
• Identifiers starting with capital letters usually class names
– Argument of 0 means application ended successfully
• Non-zero usually means an error occurred
– Class System part of package java.lang
• No import declaration needed
• java.lang automatically imported in every Java program
– Lines 17-19: Braces to end Welcome4 and main
Resource: Java How to Program – 5th Edition – Deitel & Deitel
|
Home | Résumé | Courses | Contact | Useful Links | Favorite Links | USC - Homepage |