Download A First Taste of Java

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Java Workshop for Teachers
May 6, 2005
A Brief Look
at the
Java Programming Language
What Java Is and Is Not
 Java is a full-fledged, powerful and very
versatile object-oriented programming
language.
 Java is a “sequel” to C++ and a “prequel” to
Microsoft’s C #.
 Java is not Javascript.
How is Java like and unlike what
you may already know?
 Syntax is very similar to C and C++
 Must deal with classes from the beginning
 Can write stand-alone programs (old-
fashioned console, as well as new-fangled
GUI) and also “applets” that run via HTML
pages on the WWW
 A platform-dependent java compiler produces
a file of platform-independent “bytecodes”
that are then interpreted by a platformdependent java interpreter
Early History of Java
 Started at Sun Microsystems (1990/91)
 James Gosling headed development team
 Began life as ‘oak’, was later renamed ‘Java’
 Designed for consumer electronic products
 First Person, Inc. lost Time-Warner contract
 Also worked well for internet programming
 Java and HotJava (Web browser) formally
announced (Sun World ’95, May 23 in San F)
Version History of Java
 Java 1.0 (1995) 212 classes in 8 packages
 Java 1.1 (1997) 504 classes in 23 packages
 Java 1.2 (1998) 1520 classes in 59 packages
 Java 1.3 (2000) 1842 classes in 76 packages
 Java 1.4 (2002) 2991 classes in 135 packages
 Java 5.0 (2004) 3270 classes in ? packages
Java 5.0 contains the first major changes in the
language itself since version 1.1.
Java Language Features
 object-oriented
 architecture neutral
 simple
 portable
 distributed
 high performance
 interpreted
 multithreaded
 robust
 dynamic
 secure
The Java “Hello, world!” Program
//HelloWorld.java
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello, world!");
}
}
$ javac HelloWorld.java
The above command produces HelloWorld.class,
which is then interpreted by the following command:
$ java HelloWorld
Some Hands-On Experience
 A couple of console applications:
WelcomeConApp.java
ConsoleWelcome.java and ConsoleWelcomeTest.java
 A GUI application:
WelcomeGUIApp.java
 An applet:
WelcomeApplet.java and WelcomeApplet.html
WelcomeConApp.java
 A typical single-file “console program”, with
text output to a “console window” (also called
“the screen” or “the standard output”)
 Compile it with the command
$ javac WelcomeConApp.java
 Confirm that you got a file called
WelcomeConApp.class
 Run that .class file with the command
$ java WelcomeConApp
ConsoleWelcome.java and
ConsoleWelcomeTest.java
 A typical, but very small, multi-file Java
program (only two files)
 Consists of a class file capable of producing
objects of that class, plus another “driver
class” that creates and uses an object of the
first class
 Compile both files with the single command
$ javac ConsoleWelcomeTest.java
 Run the program with the command
$ java ConsoleWelcomeTest
WelcomeGUIApp.java
 A small GUI application program, illustrating
- programmer color choices
- programmer font selection
- programmer choice of window size
- programmer placement of text in the window
 This is again a single-file program, but a later
practice exercise asks you to convert it to a
two-file program (and make some other
changes as well).
Java Applets vs. Java Applications
 An applet is not a standalone program, but
instead runs within a web browser
 An applet must be run via an HTML file which
refers to the applet’s class file in an applet tag
 An applet can also be run by a utility called
appletviewer that comes with the JDK (Java
Development Kit), just like javac and java
 appletviewer just shows the applet and ignores
any HTML code in the HTML file
 Applications have a main function, applets don’t
WelcomeApplet.java and
WelcomeApplet.html
 WelcomeApplet.java shows the order in which
three of the “built-in” methods of an applet are
called:
init() is called to do whatever needs doing once
start() is called after init() and at each “restart”
paint() is called to draw the Graphics object
 WelcomeApplet.html contains an <applet>
element that refers to the applet-containing file
WelcomeApplet.class which is loaded and run
by the browser
Follow-Up Practice Exercises
 Convert WelcomeConsoleTest.java to use the
first two “command-line parameters” as the
text of the welcome message and the date.
 Convert WelcomeGUIApp.java to a two-file
program and choose another font, color
scheme, and message.
 Revise WelcomeApplet.java so that the
following command will run the applet:
$ appletviewer WelcomeApplet.java
(assuming WelcomeApplet.class is available)
StudentMarks1.java to
StudentMarks6.java
 A sequence of programs to illustrate
incremental program development
 Get something that works and does a “small
version” of what your ultimate goal program
will do
 With each “iteration” add one or more new
features that take you toward that goal
 This kind of program development is closer to
the modern “extreme programming” approach
than to the older “structured programming”
Some of the things you will see in the
StudentMarks*.java sequence
 Building a GUI interface with buttons and an
area to display text
 Reading input from a file of text
 Exception handling
 Event handling
 Use of a “comparator” to change the “natural”
order of a list of strings
 Generic programming (a brand new feature in
Java 5.0)
SplashScreen.java
 A “fun” application to promote Java
 Shows how to position your application in the
middle of the screen independently of the
screen resolution
 Uses randomly generated values
 Uses the sleep() method of the Thread class
to generate a short delay
 Uses a “keyboard listener” to terminate the
program