Sie sind auf Seite 1von 25

Table of Contents

LESSON: Java Jumpstart.................................................................................................................................1


Subjects................................................................................................................................................................2
Java Applications and Applets..........................................................................................................................3
Java Application Edit......................................................................................................................................4
Java Application Compile...............................................................................................................................5
Java Application Run......................................................................................................................................6
Anatomy Declaring a class..............................................................................................................................7
Anatomy The main() method..........................................................................................................................8
Anatomy Printing Output...............................................................................................................................9
Anatomy Using Comments...........................................................................................................................10
Applet Edit.....................................................................................................................................................11
Applet Compile..............................................................................................................................................12
Applet Embed in HTML Page......................................................................................................................13
Applet Run (Browser)...................................................................................................................................14
Applet Run (Browser) Result.......................................................................................................................15
Applet Run (appletviewer)............................................................................................................................16
Anatomy Extend from java.applet.Applet..................................................................................................17
Anatomy paint() method...............................................................................................................................18
Anatomy drawString()..................................................................................................................................19
Anatomy Importing Classes..........................................................................................................................20
LABS.....................................................................................................................................................20
1. HelloWorld Application..............................................................................................................20
2. Using CLASSPATH and packages.............................................................................................20
3. Using System Input/Output.........................................................................................................21
4. Using command line parameters.................................................................................................21
RESOURCES....................................................................................................................................................23

LESSON: Java Jumpstart


author: Just van den Broecke just@justobjects.nl
organization: Just Objects B.V. www.justobjects.nl
Slides
1. Subjects
2. Java Applications and Applets
3. Java Application Edit
4. Java Application Compile
5. Java Application Run
6. Anatomy Declaring a class
7. Anatomy The main() method
8. Anatomy Printing Output
9. Anatomy Using Comments
10. Applet Edit
11. Applet Compile
12. Applet Embed in HTML Page
13. Applet Run (Browser)
14. Applet Run (Browser) Result
15. Applet Run (appletviewer)
16. Anatomy Extend from java.applet.Applet
17. Anatomy paint() method
18. Anatomy drawString()
19. Anatomy Importing Classes
Labs
1. HelloWorld Application
2. Using CLASSPATH and packages
3. Using System Input/Output
4. Using command line parameters
Resources

Subjects
Two ways to write and execute a Java Program
Java Applications
writing, compiling, running a Java Application
anatomy of a java program
Java Applets
writing, compiling, running an Java Applet
anatomy of a Java Applet
Java Jumpstart

Java Applications and Applets


Java programs can be written and executed in two ways
1. Java Applications: standalone, command line applications
2. Java Applets: run under control of a Javacapable browser
We will learn how to edit/compile/run each of these
Java Jumpstart

Java Application Edit


1: // Demonstrates the most basic Java Application.
2: public class HelloProgram {
3:
4:
public static void main (String [] args) {
5:
System.out.println ("Hello World");
6:
}
7: }

take a text editor


write the above program
save the program as HelloProgram.java
Java Jumpstart

Java Application Compile


Enter the commandline:
javac HelloProgram.java

Java Jumpstart

Java Application Run


Enter the commandline:
java HelloProgram

Observe "Hello World" printed on the console.


Java Jumpstart

Anatomy Declaring a class


Declaring a class
public class HelloProgram {
...
}

Java Jumpstart

1: // Demonstrates the most basic Java Application.


2: public class HelloProgram {
3:
4:
public static void main (String [] args) {
5:
System.out.println ("Hello World");
6:
}
7: }

Anatomy The main() method


public static void main(String[] args) {
...
}

A Java application should define a main() method


Entry for start of execution
Optional commandline arguments passed as array of String
Java Jumpstart

Anatomy Printing Output


Object System provides object out Object out
has print method println
...
System.out.println ("Hello World");
...

Java Jumpstart

1: // Demonstrates the most basic Java Application.


2: public class HelloProgram {
3:
4:
public static void main (String [] args) {
5:
System.out.println ("Hello World");
6:
}
7: }

Anatomy Using Comments


1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:

Java Jumpstart

// Comment type 1: HelloProgram.java


/*
* This is comment type 2.
*/
public class HelloProgram {
/** Comment type 3: Javadoc style comment. */
public static void main (String [] args) {
System.out.println ("Hello World"); // also type 1
}
}

10

Applet Edit
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:

// Demonstrates the most basic Java applet.


import java.applet.Applet;
import java.awt.Graphics;
public class HelloApplet extends Applet {
public void paint (Graphics g) {
g.drawString ("Hello World", 50, 50);
}
}

take a text editor


write the above applet
save file as HelloApplet.java
Java Jumpstart

10

11

Applet Compile
The same as with Java Application
Enter on a commandline:
javac HelloApplet.java

Java Jumpstart

11

12

Applet Embed in HTML Page


1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:

<html>
<head>
<title>HelloApplet html file</title>
</head>
<body>
<h1>HelloApplet html file</h1>
<applet code="HelloApplet.class" height="100" width="200">
</applet>
</body>
</html>

Applet requires browser


Applet is part of an HTML page
You have to write a HTML page
Save as HelloApplet.html
Java Jumpstart

12

13

Applet Run (Browser)


start browser
select HelloApplet.html
Java Jumpstart

13

14

Applet Run (Browser) Result


Java Jumpstart

14

15

Applet Run (appletviewer)


Another way to run an Applet
Advantage: no caching
Disadvantage: only shows applet and not the rest of the HTML page
Enter commandline:
appletviewer HelloApplet.html

Java Jumpstart

15

16

Anatomy Extend from java.applet.Applet


1:
2:
3:
4:
5:
6:
7:
8:
9:
10:

// Demonstrates the most basic Java applet.


import java.applet.Applet;
import java.awt.Graphics;
public class HelloApplet extends Applet {
public void paint (Graphics g) {
g.drawString ("Hello World", 50, 50);
}
}

Your applet should inherit from java.applet.Applet


Java Jumpstart

16

17

Anatomy paint() method


1:
2:
3:
4:
5:
6:
7:
8:
9:
10:

// Demonstrates the most basic Java applet.


import java.applet.Applet;
import java.awt.Graphics;
public class HelloApplet extends Applet {
public void paint (Graphics g) {
g.drawString ("Hello World", 50, 50);
}
}

Overriding paint() method


public void paint(Graphics g) {
...
}

Java Jumpstart

17

18

Anatomy drawString()
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:

// Demonstrates the most basic Java applet.


import java.applet.Applet;
import java.awt.Graphics;
public class HelloApplet extends Applet {
public void paint (Graphics g) {
g.drawString ("Hello World", 50, 50);
}
}

Calling drawString()
...
g.drawString("Hello World", 50, 50);
...

Java Jumpstart

18

19

Anatomy Importing Classes


1:
2:
3:
4:
5:
6:
7:
8:
9:
10:

// Demonstrates the most basic Java applet.


import java.applet.Applet;
import java.awt.Graphics;
public class HelloApplet extends Applet {
public void paint (Graphics g) {
g.drawString ("Hello World", 50, 50);
}
}

Use import statements to include library classes.


import java.applet.Applet;
import java.awt.Graphics;
...

Java Jumpstart

19

LABS
1. HelloWorld Application
This lab is meant to make you familiar with the J2SE environment. by making the HelloWorld application.
1. Create HelloWorld.java with a text editor.
2. Compile HelloWorld.java with the "javac" command.
3. Run HelloWorld with the "java" command.
If you see something like
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld

set your CLASSPATH as follows:


set CLASSPATH=.;%CLASSPATH%

2. Using CLASSPATH and packages


This lab is meant to make you familiar with CLASSPATH and packages.
1. Copy the HelloWorld.java from the previous lab.
2. Be sure your CLASSPATH is set as in the previous lab. Add as the first line of HelloWorld.java the
following:
package mypackage;
3. Recompile.
4. Run the HelloWorld program by typing "java HelloWorld". What do you observe ?
5. Run the HelloWorld program by typing "java mypackage/HelloWorld". What do you observe ?
6. Run the HelloWorld program by typing "java mypackage.HelloWorld". What do you observe ?
7. Make a subdirectory 'mypackage' and move HelloWorld.class to that directory.
8. Run the HelloWorld program by typing "java mypackage/HelloWorld". What do you observe ?
9. Run the HelloWorld program by typing "java mypackage.HelloWorld". What do you observe ?
20

10. Delete HelloWorld.class from the mypackage subdir.


11. Make a subdirectory 'myclasses'.
12. Compile HelloWorld.java with
javac d myclasses HelloWorld.java

.
13. Where is the HelloWorld.class now located ?
14. Run the HelloWorld program by typing "java mypackage.HelloWorld". What do you observe ?
15. Adapt the CLASSPATH by typing
set CLASSPATH=myclasses;%CLASSPATH%

16. Run the HelloWorld program by typing "java mypackage.HelloWorld". What do you observe ?

3. Using System Input/Output


This lab is meant to introduce System.in and System.out. Input/output will be treated in more depth in
subsequent lessons. Make a program that reads an int from the input and prints it to the output.
1. Use the following skeleton code (copy from the lab3 subdir).
1: /**
2: * Echo input to output.
3: * <p>
4: * core/jumpstart/lab3
5: * <p>
6: * $Id: EchoInput.java,v 1.2 2004/03/14 15:42:43 justb Exp $
7: * <p>
8: *
9: * @author $Author: justb $ Just van den Broecke Just Objects &copy;
10: */
11:
12: public class EchoInput {
13:
14:
public static void main (String [] args) throws java.io.IOException {
15:
int i = 0;
16:
17:
<YOUR CODE HERE>
18:
19:
System.out.println ("you typed "+i);
20:
}
21: }

2. Add a line that reads the int from the standard input (keyboard) using i = System.in.read()
3. Compile and run.
4. Type a number. What do you observe ? (Hint: what about bytes?)

4. Using command line parameters


This lab is meant to make you familiar with the commandline parameters.
Make a program Echo.java that echoes its commandline parameters to the standard output.
Hints:
You will need a "for" loop. The general structure in Java is
for (int i=0; i < N; i++) {
// do something;
}

21

You may want to skip ahead to the Control Flow lesson.


You will need the size (N) of the argsarray. This is provided by args.length.

22

RESOURCES
[javasoft52]

JavaSoft ; The Java Tutorial Trail: Getting Started ; From the Java Tutorial. Contains
detailed instructions for compiling and running your first program.
http://www.javasoft.com/docs/books/tutorial/getStarted/TOC.html

[baldwin010]

Richard G. Baldwin ; Hello World ; This lesson introduces you to Java programming by
presenting and discussing some different versions of the Hello World program.
http://www.dickbaldwin.com/java/Java010.htm

[baldwin012]

Richard G. Baldwin ; Defining a Class ;


http://www.dickbaldwin.com/java/Java012.htm

[baldwin014]

Richard G. Baldwin ; The main() Method ;


http://www.dickbaldwin.com/java/Java014.htm

[baldwin016]

Richard G. Baldwin ; Using the System and PrintStream Classes ;


http://www.dickbaldwin.com/java/Java016.htm

[baldwin017]

Richard G. Baldwin ; Introduction to Graphical User Interfaces (GUI) ;


http://www.dickbaldwin.com/java/Java017.htm

[baldwin018]

Richard G. Baldwin ; Introduction to Applets ;


http://www.dickbaldwin.com/java/Java018.htm

[baldwin032]

Richard G. Baldwin ; CommandLine Arguments ;


http://www.dickbaldwin.com/java/Java032.htm

[baldwin052]

Richard G. Baldwin ; Setting Program Attributes ;


http://www.dickbaldwin.com/java/Java052.htm

[baldwin054]

Richard G. Baldwin ; Using System Resources ;


http://www.dickbaldwin.com/java/Java054.htm

23

Das könnte Ihnen auch gefallen