2 2 java start jumpstart
TRANSCRIPT
7/27/2019 2 2 Java Start Jumpstart
http://slidepdf.com/reader/full/2-2-java-start-jumpstart 2/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
i
7/27/2019 2 2 Java Start Jumpstart
http://slidepdf.com/reader/full/2-2-java-start-jumpstart 3/25
LESSON: Java Jumpstart
author: Just van den Broecke − [email protected]
organization: Just Objects B.V. − www.justobjects.nl
Slides
Subjects1.
Java Applications and Applets2.
Java Application − Edit3.
Java Application − Compile4.
Java Application − Run5.
Anatomy − Declaring a class6.
Anatomy − The main() method7.
Anatomy − Printing Output8.
Anatomy − Using Comments9.
Applet − Edit10.
Applet − Compile11.
Applet − Embed in HTML Page12.
Applet − Run (Browser)13.
Applet − Run (Browser) Result14.
Applet − Run (appletviewer)15.
Anatomy − Extend from java.applet.Applet16.
Anatomy − paint() method17.
Anatomy − drawString()18.
Anatomy − Importing Classes19.
Labs
HelloWorld Application1.
Using CLASSPATH and packages2.
Using System Input/Output3.
Using command line parameters4.
Resources
1
7/27/2019 2 2 Java Start Jumpstart
http://slidepdf.com/reader/full/2-2-java-start-jumpstart 4/25
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 1
2
7/27/2019 2 2 Java Start Jumpstart
http://slidepdf.com/reader/full/2-2-java-start-jumpstart 5/25
Java Applications and Applets
Java programs can be written and executed in two ways
Java Applications: stand−alone, command line applications1.
Java Applets: run under control of a Java−capable browser2.
We will learn how to edit/compile/run each of these
Java Jumpstart 2
3
7/27/2019 2 2 Java Start Jumpstart
http://slidepdf.com/reader/full/2-2-java-start-jumpstart 6/25
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 3
4
7/27/2019 2 2 Java Start Jumpstart
http://slidepdf.com/reader/full/2-2-java-start-jumpstart 7/25
Java Application − Compile
Enter the commandline:
javac HelloProgram.java
Java Jumpstart 4
5
7/27/2019 2 2 Java Start Jumpstart
http://slidepdf.com/reader/full/2-2-java-start-jumpstart 8/25
Java Application − Run
Enter the commandline:
java HelloProgram
Observe "Hello World" printed on the console.
Java Jumpstart 5
6
7/27/2019 2 2 Java Start Jumpstart
http://slidepdf.com/reader/full/2-2-java-start-jumpstart 9/25
Anatomy − Declaring a class
Declaring a class
public class HelloProgram {
...}
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: }
Java Jumpstart 6
7
7/27/2019 2 2 Java Start Jumpstart
http://slidepdf.com/reader/full/2-2-java-start-jumpstart 10/25
Anatomy − The main() method
public static void main(String[] args) {
...
}
A Java application should define a main() method•
Entry for start of execution•
Optional command−line arguments passed as array of String•
Java Jumpstart 7
8
7/27/2019 2 2 Java Start Jumpstart
http://slidepdf.com/reader/full/2-2-java-start-jumpstart 11/25
Anatomy − Printing Output
Object System provides object out Object out
has print method println
...
System.out.println ("Hello World");
...
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: }
Java Jumpstart 8
9
7/27/2019 2 2 Java Start Jumpstart
http://slidepdf.com/reader/full/2-2-java-start-jumpstart 12/25
Anatomy − Using Comments
1: // Comment type 1: HelloProgram.java
2:
3: /*
4: * This is comment type 2.
5: */6: public class HelloProgram {
7:
8: /** Comment type 3: Javadoc style comment. */
9: public static void main (String [] args) {
10: System.out.println ("Hello World"); // also type 1
11: }
12: }
13:
Java Jumpstart 9
10
7/27/2019 2 2 Java Start Jumpstart
http://slidepdf.com/reader/full/2-2-java-start-jumpstart 13/25
Applet − Edit
1: // Demonstrates the most basic Java applet.
2: import java.applet.Applet;
3: import java.awt.Graphics;
4:
5: public class HelloApplet extends Applet {6:
7: public void paint (Graphics g) {
8: g.drawString ("Hello World", 50, 50);
9: }
10: }
take a text editor•
write the above applet•
save file as HelloApplet.java•
Java Jumpstart 10
11
7/27/2019 2 2 Java Start Jumpstart
http://slidepdf.com/reader/full/2-2-java-start-jumpstart 14/25
Applet − Compile
The same as with Java Application
Enter on a commandline:
javac HelloApplet.java
Java Jumpstart 11
12
7/27/2019 2 2 Java Start Jumpstart
http://slidepdf.com/reader/full/2-2-java-start-jumpstart 15/25
Applet − Embed in HTML Page
1: <html>
2:
3: <head>
4: <title>HelloApplet html file</title>
5: </head>6:
7: <body>
8: <h1>HelloApplet html file</h1>
9:
10: <applet code="HelloApplet.class" height="100" width="200">
11: </applet>
12:
13: </body>
14: </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
7/27/2019 2 2 Java Start Jumpstart
http://slidepdf.com/reader/full/2-2-java-start-jumpstart 16/25
Applet − Run (Browser)
start browser•
select HelloApplet.html•
Java Jumpstart 13
14
7/27/2019 2 2 Java Start Jumpstart
http://slidepdf.com/reader/full/2-2-java-start-jumpstart 17/25
Applet − Run (Browser) Result
Java Jumpstart 14
15
7/27/2019 2 2 Java Start Jumpstart
http://slidepdf.com/reader/full/2-2-java-start-jumpstart 18/25
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
7/27/2019 2 2 Java Start Jumpstart
http://slidepdf.com/reader/full/2-2-java-start-jumpstart 19/25
Anatomy − Extend from java.applet.Applet
1: // Demonstrates the most basic Java applet.
2: import java.applet.Applet;
3: import java.awt.Graphics;
4:
5: public class HelloApplet extends Applet {6:
7: public void paint (Graphics g) {
8: g.drawString ("Hello World", 50, 50);
9: }
10: }
Your applet should inherit from java.applet.Applet
Java Jumpstart 16
17
7/27/2019 2 2 Java Start Jumpstart
http://slidepdf.com/reader/full/2-2-java-start-jumpstart 20/25
Anatomy − paint() method
1: // Demonstrates the most basic Java applet.
2: import java.applet.Applet;
3: import java.awt.Graphics;
4:
5: public class HelloApplet extends Applet {6:
7: public void paint (Graphics g) {
8: g.drawString ("Hello World", 50, 50);
9: }
10: }
Overriding paint() method
public void paint(Graphics g) {
...
}
Java Jumpstart 17
18
7/27/2019 2 2 Java Start Jumpstart
http://slidepdf.com/reader/full/2-2-java-start-jumpstart 21/25
Anatomy − drawString()
1: // Demonstrates the most basic Java applet.
2: import java.applet.Applet;
3: import java.awt.Graphics;
4:
5: public class HelloApplet extends Applet {6:
7: public void paint (Graphics g) {
8: g.drawString ("Hello World", 50, 50);
9: }
10: }
Calling drawString()
...
g.drawString("Hello World", 50, 50);
...
Java Jumpstart 18
19
7/27/2019 2 2 Java Start Jumpstart
http://slidepdf.com/reader/full/2-2-java-start-jumpstart 22/25
Anatomy − Importing Classes
1: // Demonstrates the most basic Java applet.
2: import java.applet.Applet;
3: import java.awt.Graphics;
4:
5: public class HelloApplet extends Applet {6:
7: public void paint (Graphics g) {
8: g.drawString ("Hello World", 50, 50);
9: }
10: }
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.
Create HelloWorld.java with a text editor.1.
Compile HelloWorld.java with the "javac" command.2.
Run HelloWorld with the "java" command.3.
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.
Copy the HelloWorld.java from the previous lab.1.
Be sure your CLASSPATH is set as in the previous lab. Add as the first line of HelloWorld.java the
following:
package mypackage;
2.
Recompile.3.
Run the HelloWorld program by typing "java HelloWorld". What do you observe ?4.
Run the HelloWorld program by typing "java mypackage/HelloWorld". What do you observe ?5.
Run the HelloWorld program by typing "java mypackage.HelloWorld". What do you observe ?6.
Make a subdirectory 'mypackage' and move HelloWorld.class to that directory.7.
Run the HelloWorld program by typing "java mypackage/HelloWorld". What do you observe ?8.
Run the HelloWorld program by typing "java mypackage.HelloWorld". What do you observe ?9.
20
7/27/2019 2 2 Java Start Jumpstart
http://slidepdf.com/reader/full/2-2-java-start-jumpstart 23/25
Delete HelloWorld.class from the mypackage subdir.10.
Make a subdirectory 'myclasses'.11.
Compile HelloWorld.java with
javac −d myclasses HelloWorld.java
.
12.
Where is the HelloWorld.class now located ?13.
Run the HelloWorld program by typing "java mypackage.HelloWorld". What do you observe ?14.
Adapt the CLASSPATH by typing
set CLASSPATH=myclasses;%CLASSPATH%
15.
Run the HelloWorld program by typing "java mypackage.HelloWorld". What do you observe ?16.
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.
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 ©
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: }
1.
Add a line that reads the int from the standard input (keyboard) using i = System.in.read()2.
Compile and run.3.
Type a number. What do you observe ? (Hint: what about bytes?)4.
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
7/27/2019 2 2 Java Start Jumpstart
http://slidepdf.com/reader/full/2-2-java-start-jumpstart 24/25
You may want to skip ahead to the Control Flow lesson.
You will need the size (N) of the args−array. This is provided by args.length.•
22
7/27/2019 2 2 Java Start Jumpstart
http://slidepdf.com/reader/full/2-2-java-start-jumpstart 25/25
RESOURCES
[javasoft−5−2] 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
[baldwin−010] 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.dick baldwin.com/java/Java010.htm
[baldwin−012] Richard G. Baldwin ; Defining a Class ;
http://www.dick baldwin.com/java/Java012.htm
[baldwin−014] Richard G. Baldwin ; The main() Method ;
http://www.dick baldwin.com/java/Java014.htm
[baldwin−016] Richard G. Baldwin ; Using the System and PrintStream Classes ;
http://www.dick baldwin.com/java/Java016.htm
[baldwin−017] Richard G. Baldwin ; Introduction to Graphical User Interfaces (GUI) ;
http://www.dick baldwin.com/java/Java017.htm
[baldwin−018] Richard G. Baldwin ; Introduction to Applets ;
http://www.dick baldwin.com/java/Java018.htm
[baldwin−032] Richard G. Baldwin ; Command−Line Arguments ;
http://www.dick baldwin.com/java/Java032.htm
[baldwin−052] Richard G. Baldwin ; Setting Program Attributes ;
http://www.dick baldwin.com/java/Java052.htm
[baldwin−054] Richard G. Baldwin ; Using System Resources ;
http://www.dickbaldwin.com/java/Java054.htm