Newest Viewed Downloaded

Chapter 2 - Introduction to Java ApplicationsOutline 2.1 Introduction 2.2 A First Program in Java: Printing a Line of Text 2.3 Modifying Our First Java Program 2.4 Displaying Text in a Dialog Box 2.5 Another Java Application: Adding Integers 2.6 Memory Concepts 2.7 Arithmetic 2.8 Decision Making: Equality and Relational Operators

Chapter 2 - Introduction to Java Applications

Outline 2.1 Introduction 2.2 A First Program in Java: Printing a Line of Text 2.3 Modifying Our First Java Program 2.4 Displaying Text in a Dialog Box 2.5 Another Java Application: Adding Integers 2.6 Memory Concepts 2.7 Arithmetic 2.8 Decision Making: Equality and Relational Operators

2.1 Introduction

In this chapter Introduce examples to illustrate features of Java Two program styles - applications and applets

2.2 A First Program in Java: Printing a Line of Text

Application Program that executes using the java interpreter Sample program Show program, then analyze each line

Welcome1.java Program Output 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 Programming!

2.2 A First Program in Java: Printing a Line of Text

1 // Fig. 2.1: Welcome1.java 2 // Text-printing program. Comments start with: // 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 */ Another line of comments Note: line numbers not part of program, added for reference

2.2 A Simple Program: Printing a Line of Text

3 4 public class Welcome1 { Blank line Makes program more readable Blank lines, spaces, and tabs are white-space characters Ignored by compiler 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 SampleClassName

2.2 A Simple Program: Printing a Line of Text

4 public class Welcome1 { 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 For chapters 2 to 7, use public keyword Certain details not important now Mimic certain features, discussions later

2.2 A Simple Program: Printing a Line of Text

4 public class Welcome1 { 7 public static void main( String args[] ) 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) Part of every Java application Applications begin executing at main Parenthesis indicate main is a method (ch. 6) Java applications contain one or more methods

2.2 A Simple Program: Printing a Line of Text

7 public static void main( String args[] ) 8 { Exactly one method must be called main Methods can perform tasks and return information void means main returns no information For now, mimic main's first line Left brace begins body of method declaration Ended by right brace } (line 11)

2.2 A Simple Program: Printing a Line of Text

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 ;

2.2 A Simple Program: Printing a Line of Text

11 } // end method main 13 } // end class Welcome1 Ends method declaration Ends class declaration Can add comments to keep track of ending braces

2.2 A Simple Program: Printing a Line of Text

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

2.2 A Simple Program: Printing a Line of Text

Fig. 2.2 Executing Welcome1 in a Microsoft Windows 2000 Command Prompt. Executing a program Type java Welcome1 Interpreter loads .class file for class Welcome1 .class extension omitted from command Interpreter calls method main

2.3 Modifying Our First Java Program

Modify example in Fig. 2.1 to print same contents using different code

2.3 Modifying Our First Java Program

9 System.out.print( "Welcome to " ); 10 System.out.println( "Java Programming!" ); Modifying programs Welcome2.java (Fig. 2.3) produces same output as Welcome1.java (Fig. 2.1) Using different code Line 9 displays “Welcome to ” with cursor remaining on printed line Line 10 displays “Java Programming! ” on same line with cursor on next line

Welcome2.java 1. Comments 2. Blank line 3. Begin class Welcome2 3.1 Method main 4. Method System.out.print 4.1 Method System.out.println 5. end main, Welcome2 Program Output 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! System.out.print keeps the cursor on the same line, so System.out.println continues on the same line.

2.3 Modifying Our First Java Program

9 System.out.println( "Welcome\nto\nJava\nProgramming!" ); Newline 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) 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!" );

Welcome3.java 1. main 2. System.out.println (uses \n for new line) Program Output 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

Welcome to Java Programming! Notice how a new line is output for each \n escape sequence.

2.3 Modifying Our First Java Program

Escape characters Backslash ( \ ) Indicates special characters be output

2.4 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)

Showing 1 - 20 of 51 items Details

Name: 
lecture1 part2
Author: 
kalid
Company: 
Deitel & Associates, Inc.
Description: 
Chapter 2 - Introduction to Java ApplicationsOutline 2.1 Introduction 2.2 A First Program in Java: Printing a Line of Text 2.3 Modifying Our First Java Program 2.4 Displaying Text in a Dialog Box 2.5 Another Java Application: Adding Integers 2.6 Memory Concepts 2.7 Arithmetic 2.8 Decision Making: Equality and Relational Operators
Tags: 
java | string | class | program | line | joptionpane | and | main
Created: 
8/10/2000 9:14:05 PM
Slides: 
51
Views: 
1
Downloads: 
0
Rating: 
0


> Comment



Share this presentation
|

Comments

Share this presentation:

|
Sitemap