Professional Documents
Culture Documents
0598 Java Programming Basics PDF
0598 Java Programming Basics PDF
0598 Java Programming Basics PDF
Chapter 2
▲
Image | Return to Text Page
Chapter 2 Objectives
After you have read and studied this chapter, you
should be able to
▲
Image | Return to Text Page
▲
Image | Return to Text Page
Program MyFirstApplication
/*
Program MyFirstApplication
*/
import javabook.*;
class MyFirstApplication
{
public static void main(String[ ] args)
{
MainWindow mainWindow; Declare
Declareaaname
name
mainWindow = new MainWindow(); Create
Createan
anobject
object
mainWindow.setVisible( true ); Make
Makeititvisible
visible
}
}
▲
Image | Return to Text Page
MyFirstApplication mainWindow
MainWindow
main true
setVisible
▲
Image | Return to Text Page
MainWindow mainWindow;
mainWindow = new MainWindow();
mainWindow.setVisible( true );
mainWindow
MainWindow
MainWindow
State-of-Memory Diagram
▲
Image | Return to Text Page
Object Declaration
Class
ClassName
Name
This Object
ObjectName
This class mustbe
class must bedefined
defined One object is
Name
declared
before
before this declarationcan
this declaration can One object is declared
be stated. here.
here.
be stated.
MainWindow mainWindow;
Account customer;
More
Student jan, jim, jon;
Examples
Vehicle car1, car2;
▲
Image | Return to Text Page
Object Creation
Object Class
ClassName Argument
ObjectName
Name Name Argument
Name An
An instance ofthis
instance of thisclass
classisis No
No argumentsare
arguments areused
Name of the objectwe
of the object we used
are creating here. created.
created. here.
here.
are creating here.
▲
Image | Return to Text Page
Customer customer;
customer = new Customer( );
customer = new Customer( );
customer
Created
Createdwith
withthe
the Created
Createdwith
withthe
thesecond
second
first new. Customer Customer new.
first new. Customer Customer new. Reference tothe
Reference to thefirst
first
Customer object is lost.
Customer object is lost.
▲
Image | Return to Text Page
Sending a Message
Object
ObjectName
Name
Name Method
MethodName
Name Argument
Name of the objecttoto
of the object
The Argument
which The name of themessage
name of the message The
The argumentwe
argument weare
whichwe
weare
aresending
sendingaa we are sending. are
message. we are sending. passing with the message.
passing with the message.
message.
account.deposit( 200.0 );
More
student.setName(“john”);
Examples
car1.startEngine( );
▲
Image | Return to Text Page
Program Components
comments,
class declarations.
▲
Image | Return to Text Page
*/
import javabook.*;
class MyFirstApplication
{
public static void main(String[ ] args) Comment
Comment
{
MainWindow mainWindow;
mainWindow = new MainWindow();
mainWindow.setVisible( true );
}
}
▲
Image | Return to Text Page
/*
Comment number 1
*/
/*
Comment number 2
*/
These
Theseare
arepart
partofofthe
the
/*
comment.
comment.
/*
/*
This is a comment
*/
Error:
Error:No
Nomatching
matching
beginning marker.
beginning marker.
*/
▲
Image | Return to Text Page
// This is a comment
// This is another comment Single
Singleline
lineComments
Comments
// This is a third comment
/**
* This class provides basic clock functions. In addition
javadoc
javadocComments
Comments
* to reading the current time and today’s date, you can
* use this class for stopwatch functions.
*/
▲
Image | Return to Text Page
*/
▲
Image | Return to Text Page
import javabook.*;
More import java.awt.image.ColorModel;
Examples import com.drcaffeine.galapagos.*;
▲
Image | Return to Text Page
*/ Class
ClassDeclaration
Declaration
import javabook.*;
class MyFirstApplication
{
public static void main(String[ ] args)
{
MainWindow mainWindow;
mainWindow = new MainWindow();
mainWindow.setVisible( true );
}
}
▲
Image | Return to Text Page
*/
Method
MethodDeclaration
Declaration
import javabook.*;
class MyFirstApplication
{
public static void main(String[ ] args)
{
MainWindow mainWindow;
mainWindow = new MainWindow();
mainWindow.setVisible( true );
}
}
▲
Image | Return to Text Page
Modifier
Modifier Modifier
Modifier Return
ReturnType
Type Method
MethodName
Name Parameter
Parameter
MainWindow mainWindow;
mainWindow.setVisible( true );
▲
Image | Return to Text Page
▲
Image | Return to Text Page
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
instructions on how to edit,compile,
on how to edit, compile,
and run Java programs.
and run Java programs.
▲
Image | Return to Text Page
▲
Image | Return to Text Page
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.
▲
Image | Return to Text Page
Class Purpose
The main class of theprogram.
DisplayMessage
▲
Image | Return to Text Page
true
setVisible
main
“I
Lo
ve
Ja
va
messageBox
”
MessageBox
show
▲
Image | Return to Text Page
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);
▲
Image | Return to Text Page
▲
Image | Return to Text Page
Program MyFirstApplet
/*
Program MyFirstApplet
import java.applet.*;
import java.awt.*;
▲
Image | Return to Text Page
/*
Program MyFirstApplet
Header
Header
Comment
Comment An applet that displays the text "I Love
Java"
and a rectangle around the text.
*/
Import
Import import java.applet.*;
Statements
Statements import java.awt.*;
▲
Image | Return to Text Page
AppletViewer
MyFirstApplet
main graphic
paint
▲
Image | Return to Text Page
Drawing
Drawing
This
This is wherewe
is where wedraw
drawononan
an
applet window by using the
applet window by using the
Graphics
Graphicsmethods.
methods.
▲
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).
▲
Image | Return to Text Page
▲
Image | Return to Text Page
<HTML>
<HTML>
<BODY>
<BODY>
<APPLET
<APPLET CODE="MyFirstApplet.class"
CODE="MyFirstApplet.class" WIDTH=300
WIDTH=300 HEIGHT=190>
HEIGHT=190>
</APPLET>
</APPLET>
</BODY>
</BODY>
</HTML>
</HTML>
▲
Image | Return to Text Page
Editor Compiler