Section 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
Slide 22.1 Introduction In this part Introduce cases to represent elements of Java Two program styles - applications and applets
Slide 32.2 A First Program in Java: Printing a Line of Text Application Program that executes utilizing the java translator Sample program Show program, then break down each line
Slide 41/Fig. 2.1: Welcome1.java 2/Text-printing program. 3 4 open class Welcome1 { 5 6/principle technique starts execution of Java application 7 open static void fundamental( String args[] ) 8 { 9 System.out.println( "Welcome to Java Programming!" ); 10 11 }/end strategy primary 12 13 }/end class Welcome1 Welcome1.java Program Output Welcome to Java Programming!
Slide 51/Fig. 2.1: Welcome1.java 2/Text-printing program. 2.2 A First Program in Java: Printing a Line of Text Comments begin with:/Comments overlooked amid program execution Document and portray code Provides code comprehensibility Traditional remarks:/* ... *//* This is a conventional remark. It can be part over many lines */Another line of remarks Note: line numbers not some portion of program, included for reference
Slide 63 4 open class Welcome1 { 2.2 A Simple Program: Printing a Line of Text Blank line Makes program more coherent Blank lines, spaces, and tabs are white-space characters Ignored by compiler Begins class revelation for class Welcome1 Every Java program has no less than one client characterized class Keyword: words saved for use by Java class catchphrase took after by class name Naming classes: underwrite each word SampleClassName
Slide 74 open class Welcome1 { 2.2 A Simple Program: Printing a Line of Text Name of class called identifier Series of characters comprising of letters, digits, underscores ( _ ) and dollar signs ( $ ) Does not start with a digit, has no spaces Examples: Welcome1 , $ esteem , _value , button7 7button is invalid Java is case touchy (upper casing matters) a1 and A1 are diverse For sections 2 to 7, utilize open watchword Certain points of interest not imperative now Mimic certain elements, talks later
Slide 84 open class Welcome1 { 7 open static void primary( String args[] ) 2.2 A Simple Program: Printing a Line of Text Saving documents File name must be class name with .java expansion Welcome1.java Left prop { Begins body of each class Right support closes presentations (line 13) Part of each Java application Applications start executing at fundamental Parenthesis show principle is a technique (ch. 6) Java applications contain at least one techniques
Slide 97 open static void primary( String args[] ) 8 { 2.2 A Simple Program: Printing a Line of Text Exactly one strategy must be called principle Methods can perform assignments and return data void means fundamental returns no data For now, copy fundamental 's first line Left prop starts collection of technique presentation Ended by right support } (line 11)
Slide 109 System.out.println( "Welcome to Java Programming!" ); 2.2 A Simple Program: Printing a Line of Text Instructs PC to play out an activity Prints series of characters String - arrangement characters inside twofold quotes White-spaces in strings are not overlooked by compiler System.out Standard yield question Print to charge window (i.e., MS-DOS provoke) Method System.out.println Displays line of content Argument inside bracket This line known as an announcement Statements must end with semicolon ;
Slide 1111 }/end technique principle 13 }/end class Welcome1 2.2 A Simple Program: Printing a Line of Text Ends technique assertion Ends class revelation Can add remarks to monitor finishing supports Lines 8 and 9 could be reworked as: Remember, compiler disregards remarks Comments can begin on same line after code
Slide 122.2 A Simple Program: Printing a Line of Text Compiling a program Open an order incite window, go to catalog where program is put away Type javac Welcome1.java If no blunders, Welcome1.class made Has bytecodes that speak to application Bytecodes go to Java mediator
Slide 132.2 A Simple Program: Printing a Line of Text Executing a program Type java Welcome1 Interpreter loads .class petition for class Welcome1 .class augmentation precluded from summon Interpreter calls technique primary Fig. 2.2 Executing Welcome1 in a Microsoft Windows 2000 Command Prompt.
Slide 142.3 Modifying Our First Java Program Modify case in Fig. 2.1 to print same substance utilizing diverse code
Slide 159 System.out.print( "Welcome to " ); 10 System.out.println( "Java Programming!" ); 2.3 Modifying Our First Java Program Modifying programs Welcome2.java (Fig. 2.3) produces same yield as Welcome1.java (Fig. 2.1) Using distinctive code Line 9 shows "Welcome to " with cursor staying on printed Line 10 shows "Java Programming! " on same line with cursor on next line
Slide 16System.out.print keeps the cursor on a similar line, so System.out.println proceeds on a similar line. 1/Fig. 2.3: Welcome2.java 2/Printing a line of content with various explanations. 3 4 open class Welcome2 { 5 6/principle technique starts execution of Java application 7 open static void primary( String args[] ) 8 { 9 System.out.print( "Welcome to " ); 10 System.out.println( "Java Programming!" ); 11 12 }/end strategy fundamental 13 14 }/end class Welcome2 Welcome2.java 1. Remarks 2. Clear line 3. Start class Welcome2 3.1 Method primary 4. Strategy System.out.print 4.1 Method System.out.println 5. end principle , Welcome2 Program Output Welcome to Java Programming!
Slide 179 System.out.println( "Welcome\nto\nJava\nProgramming!" ); 2.3 Modifying Our First Java Program Newline characters ( \n ) Interpreted as "extraordinary characters" by techniques System.out.print and System.out.println Indicates cursor ought to 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 make new lines System.out.println( "Welcome\nto\nJava\nProgramming!" );
Slide 18Notice how another line is yield for each \n escape grouping. 1/Fig. 2.4: Welcome3.java 2/Printing different lines of content with a solitary proclamation. 3 4 open class Welcome3 { 5 6/primary strategy starts execution of Java application 7 open static void fundamental( String args[] ) 8 { 9 System.out.println( "Welcome\nto\nJava\nProgramming!" ); 10 11 }/end technique principle 12 13 }/end class Welcome3 Welcome3.java 1. fundamental 2. System.out.println (utilizes \n for new line) Program Output Welcome to Java Programming!
Slide 192.3 Modifying Our First Java Program Escape characters Backslash ( \ ) Indicates exceptional characters be yield
Slide 202.4 Displaying Text in a Dialog Box Display Most Java applications utilize windows or a discourse box We have utilized summon window Class JOptionPane permits us to utilize exchange boxes Packages Set of predefined classes for us to utilize Groups of related classes called bundles Group of all bundles known as Java class library or Java applications programming interface (Java API) JOptionPane is in the javax.swing bundle Package has classes for utilizing Graphical User Interfaces (GUIs)
Slide 212.4 Displaying Text in a Dialog Box
Slide 222.4 Displaying Text in a Dialog Box Upcoming project Application that utilizations exchange boxes Explanation will come a short time later Demonstrate another approach to show yield Packages, strategies and GUI
Slide 231 /Fig. 2.6: Welcome4.java 2 /Printing different lines in a discourse box 3 import javax.swing.JOptionPane;/import class JOptionPane 4 5 open class Welcome4 { 6 open static void fundamental( String args] ) 7 { 8 JOptionPane.showMessageDialog( 9 invalid, "Welcome\nto\nJava\nProgramming!" ); 10 11 System.exit( 0 );/end the program 12 } 1/Fig. 2.6: Welcome4.java 2/Printing various lines in an exchange box. 3 4/Java bundles 5 import javax.swing.JOptionPane;/program utilizes JOptionPane 6 7 open class Welcome4 { 8 9/fundamental technique starts execution of Java application 10 open static void primary( String args[] ) 11 { 12 JOptionPane.showMessageDialog( 13 invalid , "Welcome\nto\nJava\nProgramming!" ); 14 15 System.exit( 0 );/end application with window 16 17 }/end strategy principle 18 19 }/end class Welcome4 Welcome4.java 1. import presentation 2. Class Welcome4 2.1 primary 2.2 showMessageDialog 2.3 System.exit Program Output
Slide 244/Java bundles 5 import javax.swing.JOptionPane;/program utilizes OptionPane 2.4 Displaying Text in a Dialog Box Lines 1-2: remarks as before Two gatherings of bundles in Java API Core bundles Begin with java Included with Java 2 Software Development Kit Extension bundles Begin with javax New Java bundles import revelations Used by compiler to recognize and find classes utilized as a part of Java projects Tells compiler to load class JOptionPane from javax.swing bundle .:tslidese
SPONSORS
SPONSORS
SPONSORS