? 2000 McGraw-Hill
Introduction to Object-Oriented Programming with Java--
Wu Chapter 2 - 1
Chapter 2
Java Programming Basics
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.
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.
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
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 2 - 5
Object Diagram for MyFirstApplication
MyFirstApplication
main
MainWindow
mainWindow
setVisibletrue
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
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;
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( );
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 CustomerCreated with the first new,Created with the second new,Reference to the first
Customer object is lost.
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( );
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 2 - 11
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 - 12
/*
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 );
}
}
Program Component,Comment
Comment
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 2 - 13
Matching Comment Markers
/* This is a comment on one line */
/*
Comment number 1
*/
/*
Comment number 2
*/
/*
/*
/*
This is a comment
*/
*/
Error,No matching
beginning marker.
These are part of the
comment.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 2 - 14
Three Types of Comments
/*
This is a comment with
three lines of
text.
*/
Multiline Comment
Single line Comments
// This is a comment
// This is another comment
// This is a third comment
/**
* This class provides basic clock functions,In addition
* to reading the current time and today’s date,you can
* use this class for stopwatch functions.
*/
javadoc Comments
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 2 - 15
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 javabook.*;
class MyFirstApplication
{
public static void main(String[ ] args)
{
MainWindow mainWindow;
mainWindow = new MainWindow();
mainWindow.setVisible( true );
}
}
Import Statement
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 2 - 16
Import Statement Syntax and Semantics
<package name>,<class name> ;
e.g,javabook,InputBox;
More
Examples
import javabook.*;
import java.awt.image.ColorModel;
import com.drcaffeine.galapagos.*;
Package Name
Name of the package that
contains the classes we
want to use.
Class Name
The name of the class we
want to import,Use asterisks
to import all classes.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 2 - 17
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.
*/
import javabook.*;
class MyFirstApplication
{
public static void main(String[ ] args)
{
MainWindow mainWindow;
mainWindow = new MainWindow();
mainWindow.setVisible( true );
}
}
Class Declaration
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 2 - 18
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.
*/
import javabook.*;
class MyFirstApplication
{
public static void main(String[ ] args)
{
MainWindow mainWindow;
mainWindow = new MainWindow();
mainWindow.setVisible( true );
}
}
Method Declaration
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 2 - 19
Method Declaration Elements
public static void main ( String[ ] args )
{
MainWindow mainWindow;
mainWindow = new MainWindow();
mainWindow.setVisible( true );
}
Modifier Modifier Return Type Method Name Parameter
Method Body
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 2 - 20
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
{
public static void main(String[ ] args)
{
MainWindow mainWindow;
mainWindow = new MainWindow();
mainWindow.setVisible( true );
}
}
Comment
Import
Statements
Class Name
Method Body
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 2 - 21
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 this image to read step-by-step
instructions on how to edit,compile,
and run Java programs.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 2 - 22
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 - 23
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 - 24
Sample Program,Design Document
Design Document,DisplayMessage
Class Purpose
DisplayMessage 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 - 25
Sample Program,Object Diagram
DisplayMessage
main
MainWindow
mainWindow
setVisibletrue
MessageBox
messageBox
show
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 2 - 26
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 ); //display two objects,first the frame
messageBox.show("I Love Java"); //and then the dialog
}
}
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 2 - 27
Sample Program,Testing
Run the program,and you will see…
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 2 - 28
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 - 29
Three Components of 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);
}
}
Header
Comment
Import
Statements
Class
Declaration
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 2 - 30
Object Diagram for MyFirstApplet
AppletViewer
main
MyFirstApplet
paintgraphic
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 2 - 31
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
This is where we draw on an
applet window by using the
Graphics methods.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 2 - 32
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 - 33
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
Import
Statements
Class Name
Method Body
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 2 - 34
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:
<HTML>
<BODY>
<APPLET CODE="MyFirstApplet.class" WIDTH=300 HEIGHT=190>
</APPLET>
</BODY>
</HTML>
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 2 - 35
Edit-Compile-Run Cycle for Applets
MyFirstApplet.java
(source file)
MyFirstApplet.class
(bytecode file)
MyFirstApplet.html
(HTML file)
Editor Compiler
AppletViewer
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 2 - 36