CSCI 220: HTML Authoring & WWW Programming


Java


What Is Java?

Java is an object-oriented programming language developed by Sun Microsystems, Inc. and it's recently created subsidiary JavaSoft. Java, which was originally intended for programming small home appliances, is currently being touted as the language which will make the vision of inexpensive home "Internet (or dumb) terminals" a reality. The reason for this enthusiasm is the fact that programs written in Java can run on any machine which has a Java interpreter (currently in the form of a web browser such as Netscape and Internet Explorer). Thus, a program written in Java can be run on a Macintosh, a PC or a UNIX workstation without ever having to make special, platform-specific, revisions to the program. In many ways, such a system is a programmer's dream come true: the programmer only needs to write one program and it will run without modification on all machines. This is what Java supporters mean when they talk about "platform-independence."

Java is a language still in its infancy. Although it has been around in pre-release form for a little over a year, it was formally released in the early spring of 1996. As you may or may not have heard, there are still a few minor glitches which must be worked out before Java can realize its full potential. First, Java applets are fairly slow. To illustrate, HTML "programs" are fully interpreted. What you write is the program. C and C++ programs, on the other hand, are transformed into machine language. That is, C/C++ compilers take the text instructions written by the programmer and convert them into language which the machine can directly access and understand. These machine language programs are called executables. Because they are in a language which the machine already understands, no translation is necessary and the programs therefore run very quickly. The drawback, of course, is that because they are translated to a machine's native language, they cannot be run on anything other than that machine type. PC programs cannot (without the aid of translator programs) run directly on a Macintosh and vice-versa. Although Java programs are compiled, they must be translated into something just short of machine language in order to be truly platform-independent. Thus, Java programs are compiled into what are called bytecodes, which are then taken by the interpreter and translated into native machine language. Because there is a translation step in the process, albeit shorter than with fully-interpreted languages, Java applets run more slowly than if they were created in a fully-compiled language such as C.

In addition, due to Java's relatively recent arrival on the programming scene, as well as ideosyncracies in various operating systems, Java still exhibits some platform-dependent behavior on occasions. Most of these issues will be resolved as Java develops. Indeed, C++ was around for several years before it became an accepted alternative to C.

How Do Java Programs Work?

Actually, there are two types of Java programs: applets and applications. Java applets are designed to be "distributed" over the Internet. That is, a person writes an applet and inserts it into his or her web page through the use of a special tag. When a person using a Java-compliant browser (e.g. interpreter) views the Web page, the Java applet code will be sent to the browser and run on the viewer's machine. Thus, Java applets can be said to be strictly client-side applications (as contrasted with CGI scripts which are more server-side). Java applications, on the other hand, are more like "real" programs. They are not distributed over the Internet and are run directly on the machine where the program resides on disk. Because we are interested in Internet programming, our focus will be on Java applets.

In order to avoid the obvious security risks imposed by distributed Internet programming, particularly the distribution of computer viruses via hostile applets, Sun has placed some fairly rigorous limitations on applet functionality in the interest of security. The most fundamental of these restrictions is the general prohibition against writing files to the client machine. Many other security issues, such as applet/data encryption, are currently being worked out by Sun, JavaSoft and their ever growing legion of business partners. Many other companies are working on special extensions to the language to help make Java a full-blown programming language on par with C, C++ and Visual Basic.

How Does Java Differ From JavaScript?

First and foremost, JavaScript is a proprietary scripting language created by Netscape Communications, not Sun/JavaSoft. In fact, until Sun/JavaSoft and Netscape formed an alliance to help develop both Java and JavaScript, JavaScript was known as LiveScript. Thus, it is best to view JavaScript as a distant cousin of Java rather than a "Java-lite." Both are object-oriented and roughly based upon C++. However, their supported objects, properties and methods are quite different. You cannot "import" JavaScript code into a Java program and successfully compile it.

This leads us to the next important distinction. As we have already discussed, Java programs are compiled. JavaScript programs, on the other hand, are fully interpreted. Thus, JavaScript is more like an HTML scripting extension. JavaScripts cannot be run outside the browser, unlike Java programs. Further, JavaScript does not have any file reading or writing support, unlike Java.

Further, Java applets run automatically when the Web page is downloaded. JavaScripts, on the other hand, are usually invoked in a form-like context. The user inputs something and the JavaScript then manipulates the data. Finally, Java applets are not capable of generating HTML code, whereas JavaScript is capable of creating dynamic HTML documents.

What Is The Advantage Of Inserting Java Applets Into My Web Page?

One of the primary advantages of Java at the present time is it's support for animation, sound and user interaction. Java exceeds JavaScript particularly in the area of animation. Many creative programmers have already used Java with dramatic effect to make their pages more interactive, interesting and aesthetically pleasing. The first time people see Sun's Java mascot "Duke" doing cartwheels across their screen, the potential presented by this new language is apparent.

As people become more familiar with Java's remote file read & write capabilities, and as new inter-applet communication support is added, Java could replace PERL as the CGI programming language of choice. More important, looking into the future, Java could become the language by which full-blown Internet word processors, spreadsheets and the like will be created. Indeed, Corel Corporation recently announced that it is re-writing it's Perfect Office Suit in Java for just such a purpose. Thus, we are only beginning to see the opportunities presented by this new (and sometimes over-hyped) language.

How Do I Insert Java Applets Into My Web Page?

Java applets may be inserted into a Web page by using the following syntax:

Permissions for Java class files should be set to 755.

A Crash Course In Java Language Syntax

Compiled Java applets and their supporting files are called classes. There are a couple of important considerations when creating, modifying and using Java classes.

  1. The convention for naming Java source code files is classname.java. All compiled Java classes contain the name classname.class. The Java compiler automatically generates the class filename for you. Do not change the names of source/class files unless you are very familiar with the program. Changing filenames could cause your applet not to function properly.

  2. The Java source code file must have the same name as the class name specified in the source code. For example, if the Java source code creates an applet class with the following syntax:

    Then the name of the Java source file must be MyApplet.java. When compiled, this file will be named MyApplet.class.

  3. Applet classes must be specified as public. If they are not, the applet will not run.

Although Java methods and objects differ from those in JavaScript, the basic syntax is remarkably the similar. The basic Java language constructs are:

  1. Variables
  2. Repetition Operators
  3. Selection Operators
  4. Assignment Operator
  5. Logical Operators
  6. Comments

Another important component of the Java programming language is the use of objects. There are several theories for designing programming languages. Languages such as C and PERL are procedural. That is, the focus is the manipulation of data through the use of functions. Object-oriented languages, on the other hand, try to allow one to create programs which imitate real-world characteristics and behavior. Thus, objects will have two components:

  1. Data Members/Properties
  2. Member Functions/Methods

Objects are actually individual instances of classes of objects. Classes are simply the code which defines the properties and methods for a particular object type. The creation of classes of objects representing real-world items is often referred to as data encapsulation or data abstraction. That is, we are thinking more abstractly, in terms of objects and the actions taken upon them, rather than integers, strings and the like.

Another concern of object-oriented programming is a concept called data hiding. That is, when we create an object in a program, all we should be concerned with is the object, it's basic characteristics, and the actions we can take upon them. We don't worry about how the object actually does what it does, all we worry about is what it can do. All of the variables and functions actually used by the object to take the action are "hidden" from the end-user, making the end-user's task much easier. Data hiding is accomplished by "telling" the program whether the object or member function is public or private. Public data and functions are available to programs outside the class definition itself. Member functions are examples of public functions. Private functions and data may only be used by the class itself. You cannot reference private functions and data outside of the class definition. If an object, property or function is not specified public, it is automatically considered private.

An example may help clarify all of these concepts. Let's assume that we want to create a program which represents the game of basketball. One of the "objects" used in the game is a ball. Thus, we must create a ball object suited for the game of basketball. One characteristic (e.g. property) of a ball is it's color. Thus, we need a way (e.g. method) to specify the ball's color. Actions (e.g. methods) that we can take on a ball are to roll it, bounce it or throw it. We should thus create a Ball class which will imitate these behaviors and be used in the basketball program.

To create this program we need two files. The first creates the Ball class definition. The second, the basketball program itself, creates a Ball and takes actions on it. The ball source file would be named Ball.java and might look something like this:

import java.awt.*;
  
public class Ball {
  
	String color;	// This is a private data member which
			// describes the Ball's color
  
	public void Ball() {	// This is called a constructor.
				//It creates our object for the first time   
				// and sets the default color to red.  

	color = new String("red");  

	}
  

	public String getColor() {	// This lets other programs
					// get the color of the Ball
		return color;
	}
  
	public void setColor(String newcolor){	// This lets other programs 
						// set the value of the Ball's color  
		color = newcolor;
	}
  
	public void Bounce() {	// Lets you bounce a Ball  
		…  
	}
  
	public void Roll() {	// Lets you roll a Ball  
		…  
	}
  
	public void Throw() {	// Lets you throw a Ball  
		…  
	}
}

Now we may create an applet which uses a Ball which looks something like this:

import java.awt.*;  
import java.applet.*;  
import Ball;	// We need to make sure that our program
		// is aware of the Ball class
  
public class BasketballGame extends Applet {  
	// Note that the class is specified public

	Ball basketball = new Ball();	
		// Make a new Ball object called basketball
  	basketball.color = "orange";   
		// This won't work because color is private.  
	basketball.setColor("orange");  
		// This works because setColor() is public.  
	basketball.Bounce();   
		// This works because Bounce()is public  
	basketball.Throw();  
		// This works because Throw()is public  
}

Now that you have an idea of what object-oriented programming is about, let's take a look at a java applet in action.

A Simple Applet With Source Code

In order to give you an idea of what Java code looks like, here is a very simple "Cyber Etch-A-Sketch" applet. You can use the mouse to create a sketch. Pressing the "Shake" button clears the drawing area to create a new drawing.

Click here to view the source code.

Note : This applet uses code presented in pages 10-12 and 94-95 of David Flanagan's "Java In A Nutshell", O'Reilley & Associates, 1996. This is an invaluable reference book if you desire to perform extensive Java programming.

Where Can I Find Java Applets For Use In My Pages?

If you would like to write your own applets, there are several how-to books available. One of the best is Gary Cornell and Cay S. Horstmann's "Core Java", SunSoft Press, 1996. Additionally, JavaSoft maintains an online Java language specification.

There are many great repositories of pre-written Java applets which can be downloaded and added to you pages. The best are probably Gamelan and JARS (Java Applet Rating Service). Both of these pages have a vast array of applets to view and use, as well as links to other Java-related Internet sites. Additionally, "Instant Java", from SunSoft, is a repository of dozens of ready-made applets for insertion into Web pages.

VRML


What Is VRML?

VRML is an acronym for Virtual Reality Modeling Language: a scripting language which allows one to create three dimensional worlds on the World Wide Web. While "standard" HTML documents allow only two-dimensional, book-like, browsing, VRML allows one to create virtual "rooms" or "worlds" which can be navigated via your browser with the assistance of special plug-in software. This technology opens up previously unimaginable opportunities for Web commerce and recreation. Imagine someday being able to enter a "virtual shopping mall" and actually "walk" down the aisles looking for virtual wares. You could scan the "shelves" for items you desire and even pick up and look at virtual wares. Or, how about looking for a new house via the web? Instead of traveling across town (or even across the country), what if you could simply "walk through" available homes without ever leaving your living room?

While VRML has not quite reached the point of 3-D shopping malls or real estate showrooms, VRML has gained significant public acceptance, and continues to evolve to the point that such dreams may become a reality in the not-too-distant future.

How Do I Create VRML Worlds?

VRML is a somewhat involved scripting language involving basic shapes and xyz coordinate manipulation. Each VRML object is individually defined by its location relative to an "origin" (as measured in arbitrary "units"), the object's shape and size (in units), and other characteristics such as color, texture, shininess, etc. The following VRML code creates a simple blue cube, under which is placed a green sphere.

#VRML V1.0 ascii

Separator {


	Material {
		diffuseColor 0.0 0.0 1.0
	}


	Cube {  
		width 4.0
		height 4.0
		depth 4.0
	}

}

Separator {


	Translation { 
		translation 0.0 -6.0 0.0
	}

	Material {
		diffuseColor 0.0 1.0 0.0
		shininess 1.0
	}

	Sphere {
		radius 4.0
	}
}

If you would like to view the objects which this code produces, click here.

As is readily evident, creating even the simplest of 3-D worlds involves a fairly significant amount of code. Fortunately, there are several VRML world editors which let you create complex worlds with relative ease. I used Caligari's Pioneer to create this 3-D snowman.

How Do I Insert VRML Worlds Into My Web Documents?

VRML worlds are referenced via the same anchor tags that one uses to access any HTML page. The only difference is that VRML sites have a .wrl suffix rather than .html. Thus, if I wanted to make a reference to a VRML world defined by the document myworld.wrl, I would use the following syntax:

The VRML language itself offers a "tag" to allow inter-world navigation. By adding a WWWAnchor tag to a VRML object, if one clicks on that object, one will be transported to the specificed VRML world. Thus, one can "navigate" VRML worlds in much the same way as one navigates HTML documents.

VRML Resources On The World Wide Web

If you would like to explore the world of VRML in greater detail, there are several excellent sites which provide tutorials and software resources for creating VRML virtual worlds. Here are a few:



© 1996 Ted D. Biggs. All Rights Reserved.
Indiana University, Purdue University, Indianapolis
Department of Computer & Information Science
email: tbiggs@klingon.iupucs.iupui.edu