Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Java Program Components A Java program is composed of comments, import statements, and class declarations. DoDEA Summer 2002 Java Monday - 1 Template for Simple Java Applications center of the screen, and the size of the window is almost as big as the screen. */ import javabook.*; class MyFirstApplication { Comment Import Statements Class Name public static void main(String[ ] args) { MainWindow mainWindow; Method Body mainWindow = new MainWindow(); mainWindow.setVisible( true ); } } DoDEA Summer 2002 Java Monday - 2 Program Component: Comment /* Program MyFirstApplication This program displays a window on the screen. The window is positioned at the center of the screen, and the size of the window is almost as big as the screen. */ import javabook.*; class MyFirstApplication { public static void main(String[ ] args) { MainWindow Comment mainWindow; mainWindow = new MainWindow(); mainWindow.setVisible( true ); } } DoDEA Summer 2002 Java Monday - 3 Three Types of Comments /* This is a comment with Multiline Comment three lines of text. */ // This is a comment // This is another comment Single line Comments // This is a third comment /** * This is a javadoc comment that allows for the * automatic generation of web-page documentation javadoc Comments * if the correct format is followed. */ DoDEA Summer 2002 Java Monday - 4 Program Component: Import Statement /* Program MyFirstApplication This program displays a window on the screen. The window is positioned at the center of the screen, and the size of the window is almost as big as the screen. */ Import Statement import javabook.*; class MyFirstApplication { public static void main(String[ ] args) { MainWindow mainWindow; mainWindow = new MainWindow(); mainWindow.setVisible( true ); } } DoDEA Summer 2002 Java Monday - 5 Import Statement Syntax and Semantics Class Name The name of the class we want to import. Use asterisks to import all classes. Package Name Name of the package that contains the classes we want to use. Examples DoDEA Summer 2002 <package name> . e.g. . javabook import import <class name> ; InputBox; javabook.*; java.awt.image.ColorModel; Java Monday - 6 Program Component: Class Declaration /* Program MyFirstApplication This program displays a window on the screen. The window is positioned at the center of the screen, and the size of the window is almost as big as the screen. Class Declaration */ import javabook.*; class MyFirstApplication { public static void main(String[ ] args) { MainWindow mainWindow; mainWindow = new MainWindow(); mainWindow.setVisible( true ); } } DoDEA Summer 2002 Java Monday - 7 Program Component: Method Declaration /* Program MyFirstApplication This program displays a window on the screen. The window is positioned at the center of the screen, and the size of the window is almost as big as the screen. */ Method Declaration import javabook.*; class MyFirstApplication { public static void main(String[ ] args) { MainWindow mainWindow; mainWindow = new MainWindow(); mainWindow.setVisible( true ); } } DoDEA Summer 2002 Java Monday - 8 Your First Java Application A program to display a window on the screen using the javabook package. The size of the window is slightly smaller than the screen, and the window is positioned at the center of the screen. A class in an application must have a “main” method where execution begins. DoDEA Summer 2002 Java Monday - 9 Your First Java Application A program to display a window on the screen using the javabook package. The size of the window is slightly smaller than the screen, and the window is positioned at the center of the screen. The fundamental OOP concept illustrated by the program: An object-oriented program uses objects. (e.g., mainWindow is the object) DoDEA Summer 2002 Java Monday - 10 Program MyFirstApplication /* Program MyFirstApplication This program displays a window on the screen. The window is positioned at the center of the screen, and the size of the window is almost as big as the screen. */ import javabook.*; class MyFirstApplication { public static void main(String[ ] args) { MainWindow mainWindow; Declare a name mainWindow = new MainWindow(); Create an object mainWindow.setVisible( true ); Make it visible } } DoDEA Summer 2002 Java Monday - 11 Flow of the MyFirstApplication Program MainWindow mainWindow; mainWindow = new MainWindow(“Sample Java Application); mainWindow.setVisible( true ); mainWindow MainWindow State-of-Memory Diagram DoDEA Summer 2002 Java Monday - 12 Steps in Executing Java Applications Step 1 Edit Type in the program using an editor and save the program to a source file (className.java). Step 2 Compile Compile the source file to create the bytecode file (className.class). Step 3 Run Execute the compiled source file called bytecode file. We’ll use and free IDE called jGrasp to perform steps 1-3. DoDEA Summer 2002 Java Monday - 13 The javabook Package We used predefined classes from the javabook package. To download the package or get its detailed documentation, please visit Dr. Caffeine's web site at www.drcaffeine.com. Advantages of using javabook: Gives you a taste of how real-world programs are developed. Minimizes the impact of programming language syntax and semantics. Allows you to write practical programs without learning too many details. Serves as good example of how to design classes. DoDEA Summer 2002 Java Monday - 14