▲
Image
|
Return to Text Page
Chapter 2
Java Programming Basics
© 2000 McGraw-Hill
Introduction to Object-Oriented Programming with Java--Wu
Chapter 2 - 1
▲
▲
Image
|
Return to Text Page
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.
© 2000 McGraw-Hill
Introduction to Object-Oriented Programming with Java--Wu
Chapter 2 - 2
▲
▲
Image
|
Return to Text Page
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.
© 2000 McGraw-Hill
Introduction to Object-Oriented Programming with Java--Wu
Chapter 2 - 3
▲
▲
Image
|
Return to Text Page
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
}
}
© 2000 McGraw-Hill
mainWindow;
Declare Declareaaname name
mainWindow = new MainWindow();
Create Createan anobject object
mainWindow.setVisible( true );
Make Makeititvisible visible
Introduction to Object-Oriented Programming with Java--Wu
Chapter 2 - 4
▲
▲
Image
|
Return to Text Page
Object Diagram for MyFirstApplication
mainWindow
MyFirstApplication
MainWindow main
© 2000 McGraw-Hill
true
setVisible
Introduction to Object-Oriented Programming with Java--Wu
Chapter 2 - 5
▲
▲
Image
|
Return to Text Page
Flow of the MyFirstApplication Program MainWindow
mainWindow;
mainWindow mainWindow = = new new MainWindow(); MainWindow();
mainWindow.setVisible( mainWindow.setVisible( true true ); );
mainWindow
MainWindow MainWindow
State-of-Memory Diagram
© 2000 McGraw-Hill
Introduction to Object-Oriented Programming with Java--Wu
Chapter 2 - 6
▲
▲
Image
|
Return to Text Page
Object Declaration Class ClassName Name This class must This class mustbe bedefined defined before this declaration before this declarationcan can be stated. be stated.
MainWindow
More Examples
© 2000 McGraw-Hill
Account Student Vehicle
Object ObjectName Name One object is declared One object is declared here. here.
mainWindow;
customer; jan, jim, jon; car1, car2;
Introduction to Object-Oriented Programming with Java--Wu
Chapter 2 - 7
▲
▲
Image
|
Return to Text Page
Object Creation Object ObjectName Name Name of the object Name of the objectwe we are creating here. are creating here.
mainWindow
More Examples
© 2000 McGraw-Hill
Class ClassName Name An instance of An instance ofthis thisclass classisis created. created.
Argument Argument No arguments No argumentsare areused used here. here.
= new MainWindow (
);
customer = new Customer( ); jon = new Student(“John Java” ); car1 = new Vehicle( );
Introduction to Object-Oriented Programming with Java--Wu
Chapter 2 - 8
▲
▲
Image
|
Return to Text Page
Distinction Between Declaration and Creation Customer customer
customer
customer;
=
=
new
new
Customer( );
Customer( );
customer
Created Createdwith withthe the first new. first new.
© 2000 McGraw-Hill
Customer Customer
Customer Customer
Introduction to Object-Oriented Programming with Java--Wu
Created Createdwith withthe thesecond second new. Reference to new. Reference tothe thefirst first Customer object is lost. Customer object is lost.
Chapter 2 - 9
▲
▲
Image
|
Return to Text Page
Sending a Message Object ObjectName Name Name of the object Name of the objecttoto which whichwe weare aresending sendingaa message. message.
Method MethodName Name The name of the The name of themessage message we are sending. we are sending.
mainWindow
More Examples
© 2000 McGraw-Hill
.
setVisible
(
Argument Argument The argument The argumentwe weare are passing with the message. passing with the message.
true
) ;
account.deposit( 200.0 ); student.setName(“john”); car1.startEngine( );
Introduction to Object-Oriented Programming with Java--Wu
Chapter 2 - 10
▲
▲
Image
|
Return to Text Page
Program Components A Java program is composed of comments, import statements, and class declarations.
© 2000 McGraw-Hill
Introduction to Object-Oriented Programming with Java--Wu
Chapter 2 - 11
▲
▲
Image
|
Return to Text Page
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 Comment
mainWindow;
mainWindow = new MainWindow();
}
}
mainWindow.setVisible( true );
© 2000 McGraw-Hill
Introduction to Object-Oriented Programming with Java--Wu
Chapter 2 - 12
▲
▲
Image
|
Return to Text Page
Matching Comment Markers /* This is a comment on one line */ /* Comment number 1 */ /* Comment number 2 */
These Theseare arepart partofofthe the comment. comment.
/* /* /* This is a comment */ */
© 2000 McGraw-Hill
Error: Error:No Nomatching matching beginning marker. beginning marker.
Introduction to Object-Oriented Programming with Java--Wu
Chapter 2 - 13
▲
▲
Image
|
Return to Text Page
Three Types of Comments /* This is a comment with three lines of
Multiline MultilineComment Comment
text. */
// This is a comment // This is another comment // This is a third comment
Single Singleline lineComments Comments
/** * This class provides basic clock functions. In addition * to reading the current time and today’s date, you can
javadoc javadocComments Comments
* use this class for stopwatch functions. */
© 2000 McGraw-Hill
Introduction to Object-Oriented Programming with Java--Wu
Chapter 2 - 14
▲
▲
Image
|
Return to Text Page
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 ImportStatement Statement
import javabook.*; class MyFirstApplication { public static void main(String[ ] args) { MainWindow
mainWindow;
mainWindow = new MainWindow();
}
}
mainWindow.setVisible( true );
© 2000 McGraw-Hill
Introduction to Object-Oriented Programming with Java--Wu
Chapter 2 - 15
▲
▲
Image
|
Return to Text Page
Import Statement Syntax and Semantics Class ClassName Name The name of the The name of theclass classwe we want to import. Use asterisks want to import. Use asterisks totoimport importall allclasses. classes.
Package PackageName Name Name of the package Name of the packagethat that contains the classes we contains the classes we want wanttotouse. use.
e.g.
More Examples
© 2000 McGraw-Hill
javabook
import import import
.
.
; InputBox;
javabook.*; java.awt.image.ColorModel; com.drcaffeine.galapagos.*;
Introduction to Object-Oriented Programming with Java--Wu
Chapter 2 - 16
▲
▲
Image
|
Return to Text Page
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 ClassDeclaration Declaration
*/ import javabook.*; class MyFirstApplication { public static void main(String[ ] args) { MainWindow
mainWindow;
mainWindow = new MainWindow();
}
}
mainWindow.setVisible( true );
© 2000 McGraw-Hill
Introduction to Object-Oriented Programming with Java--Wu
Chapter 2 - 17
▲
▲
Image
|
Return to Text Page
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 MethodDeclaration Declaration
import javabook.*; class MyFirstApplication { public static void main(String[ ] args) { MainWindow
mainWindow;
mainWindow = new MainWindow();
}
}
mainWindow.setVisible( true );
© 2000 McGraw-Hill
Introduction to Object-Oriented Programming with Java--Wu
Chapter 2 - 18
▲
▲
Image
|
Return to Text Page
Method Declaration Elements Modifier Modifier
Modifier Modifier
public
static
Return ReturnType Type
void
Method MethodName Name
main
(
Parameter Parameter
String[ ] args
{ MainWindow
mainWindow;
mainWindow = new MainWindow();
Method MethodBody Body
mainWindow.setVisible( true ); }
© 2000 McGraw-Hill
Introduction to Object-Oriented Programming with Java--Wu
Chapter 2 - 19
)
▲
▲
Image
|
Return to Text Page
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 Comment
Import Import Statements Statements Class ClassName Name
public static void main(String[ ] args) { MainWindow
mainWindow;
mainWindow = new MainWindow();
}
}
© 2000 McGraw-Hill
Method MethodBody Body
mainWindow.setVisible( true );
Introduction to Object-Oriented Programming with Java--Wu
Chapter 2 - 20
▲
▲
Image
|
Return to Text Page
Steps in Executing Java Applications Step 1 Edit Type in the program using an editor and save the program to a file. Step 2 Compile Compile the source file. Step 3 Run Execute the compiled source file called bytecode file. Click Clickthis thisimage imageto toread readstep-by-step step-by-step instructions on how to edit, instructions on how to edit,compile, compile, and run Java programs. and run Java programs.
© 2000 McGraw-Hill
Introduction to Object-Oriented Programming with Java--Wu
Chapter 2 - 21
▲
▲
Image
|
Return to Text Page
The javabook Package To become a good object-oriented programmer, one must first learn how to use predefined classes. We used predefined classes from the javabook package. To download the package or get its detailed documentation, please visit Dr. Caffeine's web site. 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.
© 2000 McGraw-Hill
Introduction to Object-Oriented Programming with Java--Wu
Chapter 2 - 22
▲
▲
Image
|
Return to Text Page
Sample Program: Displaying Messages Problem Statement Write an application that displays the message I Love Java. Design Alternative 1: Set the title of the MainWindow to the designated message. Alternative 2: Use a MessageBox object. This object is intended for displaying a single line of short text to grab the enduser’s attention. The MessageBox class is available from the javabook package.
© 2000 McGraw-Hill
Introduction to Object-Oriented Programming with Java--Wu
Chapter 2 - 23
▲
▲
Image
|
Return to Text Page
Sample Program: Design Document Design Document: Class DisplayMessage
DisplayMessage Purpose The main class of theprogram.
MainWindow
The main frame window of the program. The title is set to Display Message. This class is from javabook.
MessageBox
The dialog for displaying the required message. This class is from javabook.
© 2000 McGraw-Hill
Introduction to Object-Oriented Programming with Java--Wu
Chapter 2 - 24
▲
▲
Image
|
Return to Text Page
Sample Program: Object Diagram mainWindow DisplayMessage
MainWindow true
setVisible
main
“I
Lo
ve
Ja
va
messageBox
”
MessageBox show
© 2000 McGraw-Hill
Introduction to Object-Oriented Programming with Java--Wu
Chapter 2 - 25
▲
▲
Image
|
Return to Text Page
Sample Program: Source Code /*
*/
Program DisplayMessage The program displays the text "I Love Java". The program uses a MessageBox object from the javabook package to display the text.
import javabook.*; class DisplayMessage { public static void main(String[] args) { MainWindow mainWindow; //declare two objects MessageBox messageBox; //create two objects mainWindow = new MainWindow("Display Message"); messageBox = new MessageBox(mainWindow);
}
}
mainWindow.setVisible( true ); messageBox.show("I Love Java");
© 2000 McGraw-Hill
//display two objects: first the frame //and then the dialog
Introduction to Object-Oriented Programming with Java--Wu
Chapter 2 - 26
▲
▲
Image
|
Return to Text Page
Sample Program: Testing Run the program, and you will see…
© 2000 McGraw-Hill
Introduction to Object-Oriented Programming with Java--Wu
Chapter 2 - 27
▲
▲
Image
|
Return to Text Page
Program MyFirstApplet /*
*/
Program MyFirstApplet An applet that displays the text "I Love Java" and a rectangle around the text.
import java.applet.*; import java.awt.*; public class MyFirstApplet extends Applet { public void paint( Graphics graphic) { graphic.drawString("I Love Java",70,70); graphic.drawRect(50,50,100,30); } }
© 2000 McGraw-Hill
Introduction to Object-Oriented Programming with Java--Wu
Chapter 2 - 28
▲
▲
Image
|
Return to Text Page
Three Components of Program MyFirstApplet /*
Header Header Comment Comment */
Import Import Statements Statements
Class Class Declaration Declaration
Program MyFirstApplet An applet that displays the text "I Love Java" and a rectangle around the text.
import java.applet.*; import java.awt.*; public class MyFirstApplet extends Applet { public void paint( Graphics graphic) { graphic.drawString("I Love Java",70,70); graphic.drawRect(50,50,100,30); } }
© 2000 McGraw-Hill
Introduction to Object-Oriented Programming with Java--Wu
Chapter 2 - 29
▲
▲
Image
|
Return to Text Page
Object Diagram for MyFirstApplet
AppletViewer MyFirstApplet main
© 2000 McGraw-Hill
graphic
paint
Introduction to Object-Oriented Programming with Java--Wu
Chapter 2 - 30
▲
▲
Image
|
Return to Text Page
Drawing Graphics inside the paint Method public void paint( Graphics graphic) { graphic.drawString("I Love Java",70,70); graphic.drawRect(50,50,100,30); } Drawing Drawing This is where This is wherewe wedraw drawon onan an applet window by using the applet window by using the Graphics Graphicsmethods. methods.
© 2000 McGraw-Hill
Introduction to Object-Oriented Programming with Java--Wu
Chapter 2 - 31
▲
▲
Image
|
Return to Text Page
Drawing Methods drawLine( x1, y1, x2, y2) draws a line from (x1,y1) to (x2, y2) drawRect(x, y, w, h) draws a rectangle w pixels wide and h pixels high at (x,y). drawOval( x, y, w, h) draws an oval w pixels wide and h pixels high at (x, y). See java.awt.Graphics for information on these and other drawing methods.
© 2000 McGraw-Hill
Introduction to Object-Oriented Programming with Java--Wu
Chapter 2 - 32
▲
▲
Image
|
Return to Text Page
Template for Simple Java Applets /*
Program MyFirstApplet An applet that displays the text "I Love Java" and a rectangle around the text.
*/ import java.applet.*; import java.awt.*;
public class MyFirstApplet extends Applet { public void paint( Graphics graphic) { graphic.drawString("I Love Java",70,70); graphic.drawRect(50,50,100,30); }
Comment Comment
Import Import Statements Statements Class ClassName Name
Method MethodBody Body
}
© 2000 McGraw-Hill
Introduction to Object-Oriented Programming with Java--Wu
Chapter 2 - 33
▲
▲
Image
|
Return to Text Page
Executing Java Applets Basic steps for Java applications apply for applets as well. The main difference is that you need to define an HTML file. A Web browser or the AppletViewer needs this HTML file to execute an applet. An HTML file for the sample applet looks like this:
© 2000 McGraw-Hill
Introduction to Object-Oriented Programming with Java--Wu
Chapter 2 - 34
▲
▲
Image
|
Return to Text Page
Edit-Compile-Run Cycle for Applets MyFirstApplet.java
MyFirstApplet.class
MyFirstApplet.html
(bytecode file)
(HTML file)
Compiler
Editor (source file)
AppletViewer
© 2000 McGraw-Hill
Introduction to Object-Oriented Programming with Java--Wu
Chapter 2 - 35
▲ © 2000 McGraw-Hill
Image
|
Return to Text Page
Introduction to Object-Oriented Programming with Java--Wu
Chapter 2 - 36