Chapter 2 Java Programming Basics ntroduction to Object-Oriented Programming with Java 2000 McGraw-Hl‖ Chapter 2-1
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java-- Wu Chapter 2 - 1 Chapter 2 Java Programming Basics
Chapter 2 objectives After you have read and studied this chapter, you should be able to Identify the basic components of Java programs Distinguish two types of Java programs-applications and applets e Write simple Java applications and applets e Describe the difference between object declaration and object creation e Describe the process of creating and running Java programs. e Use mainWindow and message Box classes from the javabook package to write Java applications Use the graphics class from the standard Java package C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 2-2
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 2 - 2 Chapter 2 Objectives After you have read and studied this chapter, you should be able to Identify the basic components of Java programs. Distinguish two types of Java programs-applications and applets. Write simple Java applications and applets. Describe the difference between object declaration and object creation. Describe the process of creating and running Java programs. Use MainWindow and MessageBox classes from the javabook package to write Java applications. Use the Graphics class from the standard Java package
The First Java Application r a program to display a window on the screen r The size of the window is slightly smaller than the screen, and the window is positioned at the center of the screen with a default title sample Java Application r The fundamental OoP concept illustrated by the program An object-oriented program uses objects. C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 2-3
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 2 - 3 The First Java Application A program to display a window on the screen. The size of the window is slightly smaller than the screen, and the window is positioned at the center of the screen with a default title Sample Java Application. The fundamental OOP concept illustrated by the program: An object-oriented program uses objects
Program MyFirstApplication Program MyFirstApplication This program displays a window on the screen. The window i positioned at the center of the screen, and the size of the almost as big as the screen class MyFirstApplication public static void main(string[] args) Mainwindow mainwindow Declare a name mainWindow new Mainwindow()i create an object lainWindow. setvisible( true )i Make it visible C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 2-4
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 2 - 4 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; mainWindow = new MainWindow(); mainWindow.setVisible( true ); } } Declare a name Create an object Make it visible
Object Diagram for MyFirstApplication MyFirstApplication main Window Main Window main true setvisible C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 2-5
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 2 - 5 Object Diagram for MyFirstApplication MyFirstApplication main MainWindow mainWindow setVisible true
Flow of the My FirstApplication Program MainWindow mainWindow; mainWindow new Mainwindow()i mainWindow. setvisible( true )i mainWindow MainWindow State-of-Memory Diagram C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 2-6
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 2 - 6 Flow of the MyFirstApplication Program MainWindow mainWindow; mainWindow = new MainWindow(); mainWindow.setVisible( true ); mainWindow MainWindow State-of-Memory Diagram
Object Declaration Class Name This class must be defined Object Name before this declaration can One object is declared be stated here MainWindow mainWindow MO Account customer ore Examples Student Jan, jim, jon, Vehiclecar. car2 C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 2-7
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 2 - 7 More Examples Object Declaration Class Name This class must be defined before this declaration can be stated. Object Name One object is declared here. MainWindow mainWindow; Account customer; Student jan, jim, jon; Vehiclecar1, car2;
Object creation Object Name Class Name Argument Name of the objectve An instance of this class is No arguments are used are creating here created mainWindow new MainWindow More customer=new Customer() Examples Jon new Student( John Java) car new Vehicle(): C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 2-8
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 2 - 8 Object Creation Class Name An instance of this class is created. Object Name Name of the object we are creating here. mainWindow = new MainWindow ( ) ; Argument No arguments are used here. More Examples customer = new Customer( ); jon = new Student(“John Java” ); car1 = new Vehicle( );
Distinction between declaration and creation Customer customer i customer new Cus tomer()i customer new Cus tomer( )i customer Created with the Created with the second first new Customer Customer new. Reference to the first Customer object is lost C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 2-9
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 2 - 9 Distinction Between Declaration and Creation Customer customer; customer = new Customer( ); customer = new Customer( ); customer Customer Customer Created with the first new. Created with the second new. Reference to the first Customer object is lost
Sending a message object Name Name of the object to Method Name Argument which we are sending a The name of the message The argument we are message we are sending passing with the message mainWindow. setVisible( true) More account deposit( 200.0) Examples student setName(john) car l. startEngine() C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 2-10
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 2 - 10 Sending a Message Method Name The name of the message we are sending. Object Name Name of the object to which we are sending a message. mainWindow . setVisible ( true ) ; Argument The argument we are passing with the message. More Examples account.deposit( 200.0 ); student.setName(“john”); car1.startEngine( );