LINKS
Java resources
Gamelan
The clock
Animator
Mandel
and its source
Java Tutorial
First programs
Java version of algorithmic image gallery
Javaworld
Online java compiler
comp.lang.java.programmer
comp.lang.java FAQ
Interesting applets
How to table
JDK demo
test applet
Jdbc
Applet collection
Digital Unix java
Applet Proxy Server
ONLINE BOOKS AND COURSES
CPSC124
Hooked On Java
Teach yourself Java in 21 days
The Java tutorial
Using Java
Java Quick Reference
List of Java Books
INDEX
Using other people's applets
Material to get started
Definition of some terms
The first step
OOP in a nutshell
Learning programming : step 2
Using Netscape to run applications
The actors on stage metaphor
Paint,repaint,update
Using predefined Classes
Learn Java in a hour
Java packages
Data Types
Frequently used constants and methods
Java I/O tutorial
Strings tutorial
Abstract data types
Classpath
| |
Learning Java
Using other people's applets
First of all why not trying to use in your pages applets developped by other
people calling them like routines.
Of course you can use the Gamelan Java
directory.
As a first try, see here a page with a clock.
The code shows that I use a Clock2.class applet (this has
been copied on my disk but I could have used the original) with parameters
width=170 and height=150. I can call the applet changing the parameters like
I call a library subroutine.
In this second example I have to copy first the applet on my disk, since it uses
some data from disk and a remote applet cannot access data on my disk.
Animator animates a set
of static gifs stored on disk.
Files with a name that ends in class contain a compiled piece of Java code.
Sometime the source code is also available and you can copy it and try
to compile it producing the compiled code that you then
try to use as applet. If everything works, you can then try to make some
changes to the source in order to adapt it to your needs.
This is what I have done with this
Mandelbrot generator.
This is the
source. Note that the source ends with .java .
Some material on the Web good to get started
The book Hooked on Java has put online all the applets used in the book and other interesting
information. These applets show how to perform some basic function.
Material from the book Teach yourself
Java is also available.
This instead is a Java tutorial
Some terms with definition
The first step
Download and install the JDK on your computer. Write the "hello world" application
and applet, compile them and see them.
Object-oriented programming in a nutshell
In OOP programming programs are sets of objects wich communicate through
messages. An object is defined by variables and methods(functions). A class
is a prototype describing variables and methods of all objects of the same
type. You program by defining the classes of objects necessary to
perform a task.Classes are organized in hierarchies with classes lower in hierarchy
inheriting state and behaviour from parent classes. This inheritance
mechanism provides a natural way of structuring programs.This also means
that you define new classes by specifying how these are different.
A class inherits all the methods and variables from its parent class.
You rewrite a method if necessary, overriding it.
With the JDK you get some libraries of classes that can be used to
perform some frequent tasks (for example graphics programming).
Learning programming:step 2
If this is not your first programming language, then learning to
program is more like learning to write in a foreign language.
You start by copying (simple) programs written by other people.
Although now you can easily paste them (or ftp them) I prefer
to actually write them:this make more easy the understanding
of the program. This is a table pointing
to programs copied from various sources in this way.
Using Netscape to view applications
You can use netscape (netscape2 on Unix platforms) to view applications
compiled with the command:
netscape -java classname
but first you should define CLASSPATH with
setenv CLASSPATH .:/usr/local/lib/netscape/moz2_0.zip:/usr/local/lib/netscape/classes.zip
Apparently this should also work to execute the compiler with the command
netscape -java sun.tools.javac.Main [filename].java but I
didn't have luck. More info here
Object oriented graphics programming;the AWT class library;the actors on stage metaphor
You can think about graphics objects (but also other kind of objects) as actors on
stage that play toghether directed by messages. One special message (which has the same name as
the class) will make the actor appear:this is the constructor. It also gives a name A,B,C..to the
actor.
For example you say :
Panel A = new Panel(); and a new actor of the class Panel is created with name A.
After that you call the actor by its name saying A.dothis(parameter) and
the actor will perform the action you ordered.
For example A.add(slider1); .
Now actors on stage will perform in parallel (i.e. doing things in the same time). You
can do the same with objects by using threads.Threads are themselves objects (as
everything in Java) which respond to the messages run (i.e. memorize a script to be executed),
start (i.e. start running the script) and stop (stop executing the script).
Follows a partial AWT class hierarchy
- Component
- Label
- Button
- Checkbox
- Choice
- Textfield
- Scrollbar
- Container
Paint,repaint,update
Although you define in paint what to draw inside your applet,to
request the actual drawing you call repaint(). When repaint is processed,
before calling paint, there is a call to update() which will erase the zone
to be painted and then calls paint. If you don't want the drawing zone to be
erased, then you have to modify update.
Using predefined classes
To use predefined classes in a applet, you just add a command:
import classname;
at the beginning of your applet. You put the .class file in the same directory
where you have the applet. I had a few problems compiling an applet with
an external class. I was not able to compile the applet using the remote
compiler. Instead on a pc, after some trials, it worked if I put the
class at the same level in the JDK where you have the "java" and "sun"
directory.
Learn Java in a hour
This small Java course
on the Datamation site, is an interesting tutorial.Following the instructions
you will develop a non trivial applet from design(object oriented)
to implementation.
What you really understand depends on your previous preparation:
- Have you ever done programming writing a simple small program ?
If yes, then you at least know what means:- data types
- to compile,interpret,execute,link(use libraries)
- simple statements
- Have you done some complex program (more than a subroutine) in any language?
If yes then you understand
- program development(top down or bottom-up)
- call of subroutines/methods
- definition of global data structures
- Have you done object oriented programming?
If yes then you understand:
- That a program is a set of classes
- Each class deals with a object defined by some data structure
composed from instance variables and a set of methods to process this
data structure.
- how to define subclasses extending already existing classes in libraries.
- how to override methods
- Have you done GUI programming?
If yes then you understand:
- Set of predefined graphics objects with their methods to manipulate them.
- The need of writing methods(subroutines) to process events sent to you from
the event server when the user does some action like clicking with the mouse.
- Have you ever done multithreaded programming?
If yes then you know what means having many threads running in parallel and
how to synchronize them to access shared resources.
Each of these five items is a new layer of increasing complexity.
You need years to master them .Anyway by following the lesson you can
follow the expert seeing step by step how the final program develops.
Java Packages
The JDK contains:
- directory bin:basic Java language tools
- directory demo:Demo programs
- directory lib:Class libraries
- java.lang:basic language classes (String,etc)
- java.applet:classes for applet manipulation.
- java.awt:Abstract windowing toolkit (the classes for GUI)
- java.io:File input/output classes
- java.net:Networking protocol API
- java.util:Miscellanea like random-numbers,date,etc
Java basic data types
- boolean:true or false
- byte:8-bit integer
- short:16-bit integer
- int:32 bit integer(this is the normal integer.
- char:used to represent single characters
- float:32 bit IEEE float
- double:64-bit IEEE float (the normal float type).
- String:class used to represent character strings.
Frequently used constants and methods
Java I/O tutorial
In most languages the basic reading/writing loop goes like this:
OPEN file filename for I/O
DO while(there are new records)
read/write record
END DO
In Java this is slightly more complex because you have to take care
of exceptional conditions in a explicit way with the syntax:
try { }
catch (Exception e) { handle exception};
In addition, you have to know what are the objects connected to i/o in
order to use the read/write methods.
These are the important things to know:
-
The standard input and output are the objects:System.in and System.out
- System.out is an object PrintStream which has the
methods println(string) to print a new line or print(string) to simply
add a new string to the current line.
- To direct your print output to a file "temp.out" you have to
create a PrintStream object connected to this file:let's call
it myOutput. Then you can send your output to this file with
myOutput.print( ) or myOutput.println( );
- Copy the writing loop from the following code:
try {
FileOutputStream out = new FileOutputStream("temp.out");
PrintStream myOutput = new PrintStream(out);
/* here the printing loop */
}
catch (IOException e) {
System.out.println("Error: "+e);
System.exit(1);
}
- System.in is instead an object of the class FileInputStream
This class has a method nc=read(vector of byte) that allows for reading
characters from standard input(nc is the number of read characters read).
- To read from System.in use the following code:
byte line[] = new byte[100];
int nc = 0;
try {
nc = System.in.read(line);
}
catch (IOException e){
System.out.println("Error in reading line");
}
- Note that in order to create a PrintStream object, we created
also a FileOutputStream
object and this has a method write(array_of_bytes,nc) that allows to write nc characters in output.This can be used also with
System.out . So we can echo the input line by writing:
System.out.write(line,nc);
- The method string=readLine() allows to read complete lines from input in
strings but this works only with objects DataInputStream
So to use readLine from System.in you should use the following code:
String line;
DataInputStream in = new DataInputStream(System.in);
try {
line = in.readLine();
}
catch (Exception e){}
- Reading from a file "temp.dat" using readLine will involve the following code:
String line;
try {
FileInputStream in = new FileInputStream("temp.dat");
try {
DataInputStream myInput = new DataInputStream(in);
try {
while((line=readLine()) != null){
/* process line */
}
}
catch (Exception e){
System.out.println("Error: "+ e);
}
}
catch (Exception e){
System.out.println("Error: "+ e);
}
}
catch(Exception e) {
System,out.println("failed to open file");
System.out.println("Error: "+ e);
}
- If the line contains more then one data, you should use the StreamTokenizer
object to get the single data.
Strings tutorial
In other languages to handle strings you have functions like:
length(s),index(s,s1),substring(s,i,n),s(i:j)= , if(s1==s2),etc
In Java, being a string s an object you do everything with methods connected
with this object:
- n= length(s) becomes n=s.length()
- s1=substr(s,11,7) becomes s1=s.substr(11,17)
- i=index(s,'d') i=s.indexOf("d")
- if(s1 == s2) if (s1.equals(s2))
Abstract data types
Another way to think about object oriented languages like Java is
that they allow you to extend the language by defining new data
types (abstract data types) and the operations that you can do with
them.
Classpath
If you are using JDK setting an incorrect CLASSPATH seems to be the more
frequent cause of error.I have found this relatively simple solution.
In the directory where I am working I define a "mylogin.bat" file with
the command:
set CLASSPATH=C:\java\lib;C:\website\htdocs\telnet;C:\website\java
Here I list all directories with classes needed by my applications including
the directory where I am now.
So when I start working with Java I open a Ms-DOS window, redirect myself on
the directory and then ,as first thing, I give the command "mylogin".
Then I can start giving the commands "java" and "javac".
Maintained by Giuseppe Zito:info@zitogiuseppe.com
|