Introduction to Java Programming CS 21a: Introduction to Computing I First Semester,
Programming ► Recall that a program is defined as: a sequence of instructions for a computer ► A large part (but not all) of CS 21a is about how to write programs in a programming language (Java)
The Java Programming Language ► Java: an object-oriented programming language that is ► simple ► safe ► platform independent ► designed for the internet ► Many universities use Java as the introductory programming language for beginning programmers ► Ateneo adopted Java for CS 21a in 1997
Java (a brief history) ► 1991 ► Sun Microsystems develops a language (based on C) for consumer electronic devices ► 1993 ► WWW explodes in popularity ► increased need for "dynamic" Web pages ► 1995 ► Sun formally announces Java for web use
Two Types of Java Programs ► Applications ► general-purpose programs ► standalone ► executed through the operating system ► Applets ► programs meant for the WWW ► embedded in a Web page ► normally executed through a browser
Simple Java Application File: Hello.java // Hello World application public class Hello { public static void main( String args[] ) { System.out.println( "Hello world" ); }
The Programming Process Create/Edit Program Compile Program Execute Program Compile Errors?Run-Time Errors? Source Program Object Program
Creation, Compilation, and Execution ► Create Java program C:\> edit Hello.java ► Hello.java file is created ► Compile using javac (compiler) C:\> javac Hello.java ► Hello.class file is produced ► Execute using java (interpreter) C:\>java Hello ► requires a Hello.class file
Simple Java Applet File: HelloAgain.java import javax.swing.*; import java.awt.*; public class HelloAgain extends JApplet { public void paint( Graphics g ) { g.drawString( "Hello", 50, 50 ); }
Executing Applets After compiling the java program: ► Embed an "applet tag" in an.html document that references the.class file ► Open the.html document using a browser or the appletviewer
Sample.html Document File: HA.html My Sample Applet
What is HTML? ► Hypertext Markup Language ► Underlying language of Web pages ► A means of providing formatting instructions for presenting content ► Text-based ►.html documents: ► collection of content and controls (tags)
Java Program Structure ► Java Program ► (optional) import declarations ► class declaration ► Class ► class name should match its file name ► may extend an existing class (such as JApplet) ► contains method/function declarations