1 UCLA PIC 20A Java Programming
Total Page:16
File Type:pdf, Size:1020Kb
UCLA PIC 20A PIC 20 A – Course Organization Java Programming zInstructor: Ivo Dinov, z http://www.stat.ucla.edu/~dinov/courses_students.html Asst. Prof. In Statistics, Neurology and z Program in Computing Summer2002\HTMLs\PIC20A.html z Teaching Assistant: Yon Seo Kim, PIC zJava Demo: C:\Ivo.dir\LONI_Viz\LONI_Viz_MAP_demo\run.bat University of California, Los Angeles, Summer 2002 http://www.stat.ucla.edu/~dinov/ PIC 20A, UCLA, Ivo Dinov Slide 1 PIC 20A, UCLA, Ivo Dinov Slide 2 Chapter 1 – Getting Started Chapter 1 – Getting Started z What is the Java platform and what it can do? z Java is a high-level language that is characterized by:- z How to compile and run two simple programs on the • Simple • Robust • High performance Windows, the UNIX/Linux or the Mac OS X platforms. • Object oriented • Secure • Multithreaded z Software Development Kits (SDKs) provided by Sun • Distributed • Architecture neutral • Dynamic Microsystems include a minimal set of tools to let you run and compile your programs. • Interpreted • Portable z Advanced developers are encouraged to use a z Most programming languages, you either compile or professional Integrated Development Environment interpret a program to run it on your computer. In (IDE). Java a program is both compiled and interpreted. Slide 3 PIC 20A, UCLA, Ivo Dinov Slide 4 PIC 20A, UCLA, Ivo Dinov Chapter 1 – Getting Started Java Platform z Bytecodes is the machine code instructions for the Java z A platform is the hardware or software environment in Virtual Machine (Java VM). Every Java interpreter is an which a program runs (W2K, Linux, Solaris, MacOS). implementation of the Java VM. Most platforms can be described as a combination of the operating system and hardware. z Write once, run anywhere. As long as a computer has a Java VM, the same Java program should run on z Java platform is software-only platform that runs on top Windows, Solaris, or Mac. of other, hardware-based platforms and has these 2 components: TThe Java Virtual Machine (Java VM) TThe Java Application Programming Interface (API) Slide 5 PIC 20A, UCLA, Ivo Dinov Slide 6 PIC 20A, UCLA, Ivo Dinov 1 Java Platform Java VM & Java API TThe Java Virtual Machine (Java VM) z The Java VM is the base for the Java platform and is TThe Java Application Programming Interface ported onto various hardware-based platforms. (API) z The Java API is a large collection of ready-made software components that provide many useful capabilities, such as graphical user interface (GUI) widgets. The Java API is grouped into libraries of related classes and interfaces; these libraries are known as packages. Slide 7 PIC 20A, UCLA, Ivo Dinov Slide 8 PIC 20A, UCLA, Ivo Dinov Native vs. Portable Code Java Applets & Applications z Native code is code that, after you compile it, runs on a z The most common types of programs written in the Java specific hardware platform. programming language are applets and applications. z An applet is a program that adheres to certain conventions that z As a platform-independent (portable) environment, the allow it to run within a Java-enabled web browser. Java platform can be a bit slower than native code. z Example: C:/Ivo.dir/UCLA_Classes/Applets.dir/DiceApplet.htm However, smart compilers, well-tuned interpreters, and z An application is a standalone program that runs directly on the just-in-time bytecode compilers can bring performance Java platform. A special kind of application known as a server close to that of native code without threatening serves and supports clients on a network. portability. z Servers Ex: Web servers, Mail servers, Print servers. z Servlets, similar to applets, are run-time extensions of applications. Instead of working in browsers, though, servlets run within Java Web servers, configuring or tailoring the server. Slide 9 PIC 20A, UCLA, Ivo Dinov Slide 10 PIC 20A, UCLA, Ivo Dinov Java 2 Platform SE v 1.4 First Java Program – HelloWorldApp z Your first program, HelloWorldApp , will simply display the greeting “Hello World!” To create this program, you complete each of the following steps. z Create a source file. A source file contains text, written in the Java programming language, that you and other programmers can understand. You can use any text editor to create and to edit source files. z Compile the source file into a bytecode file. The compiler takes your source file and translates the text into instructions that the Java VM can understand. The compiler converts these instructions into a bytecode file. Slide 11 PIC 20A, UCLA, Ivo Dinov Slide 12 PIC 20A, UCLA, Ivo Dinov 2 First Java Program – HelloWorldApp First Java Applet – HelloWorld z /** z Applet called HelloWorld , displaying the greeting “Hello z * The HelloWorldApp class implements an application World!” Unlike HelloWorldApp , however, the applet runs in a Java-enabled Web browser, such as the HotJava TM z * that displays "Hello World!"to the standard output. browser, Netscape Navigator, or Microsoft Internet z */ Case Explorer. z public class HelloWorldApp { Sensitive z public static void main(String [] args)) { z Create a source file, compile the source file, and run the z program. However, unlike for an application, you must //Display "Hello World!" also create an HTML file. z System.out.println("Hello World!"); z } Do this example in a DOS shell or Explorer z } javac HelloWorldApp.java java HelloWorldApp Slide 13 PIC 20A, UCLA, Ivo Dinov Slide 14 PIC 20A, UCLA, Ivo Dinov First Java Applet – HelloWorld.java First Java Applet – HelloWorld z HTML file to accompany your applet. HelloWorld.html z z <HTML> import java.applet.Applet; z z <HEAD> import java.awt.Graphics; z <TITLE>A Simple Program</TITLE> zpublic class HelloWorld extends Applet { z </HEAD> z z <BODY> public void paint(Graphics g) { z z Here is the output of my program: //Display "Hello world!" z <APPLET CODE="HelloWorld.class“ z g.drawString("Hello world!",50,25); WIDTH=150 HEIGHT=25 > z z </APPLET> } z z </BODY> } z </HTML> Slide 15 PIC 20A, UCLA, Ivo Dinov Slide 16 PIC 20A, UCLA, Ivo Dinov Running Java Applet – HelloWorld Exceptions … z Compiling: z Exception in thread “main ” z java.lang.NoClassDefFoundError:HelloWorldApp javac HelloWorld.java z If you receive this error, the interpreter cannot find your bytecode z The compiler should generate a Java bytecode file, file, HelloWorldApp.class . HelloWorld.class z One of the places java tries to find your bytecode file is your current directory. So, if your bytecode file is in C drive, you should change z Although you can use a Web browser to view your applets, your current directory to that. To change your directory, type the you may find it easier to test your applets by using the following command at the prompt and press Enter : simple appletviewer application that comes with the Java z cd c: platform. To view the elloWorld applet using appletviewer z The prompt should change to C:. If you enter dir at the prompt, you enter at the prompt: should see your .java and .class files. Now again enter zappletviewer HelloWorld.html z java HelloWorldApp znetscape HelloWorld.html z Still have problems, change your CLASSPATH variable. z set CLASSPATH= Slide 17 PIC 20A, UCLA, Ivo Dinov Slide 18 PIC 20A, UCLA, Ivo Dinov 3 First Java Application Program First Java Applet z HelloWorldApp , displays the greeting “Hello World!” z HelloWorldA, displays the greeting Hello World! in a To createthis program: java enabled browser z Create a source file – text written in the Java that others z Create a source file – notepad HelloWorld.java can understand. Use any text editor to generate/edit source files – *.java. notepad Hello.html. z z Compile the source file into a bytecode file – javac Compile the source – javac HelloWorld.java translates the source/text into instructions that the Java makes – HelloWorld.class. Virtual Machine can understand, bytecode file – *.class. z Run the program – appletviewer Hello.html. z Run the program contained in the bytecode file via Java interpreter – carries out the instructions by translating them z E.g.,C:\Ivo.dir\UCLA_Classes\Summer2002\PIC20A\Examples\JavaTutorialExa into instructions that your computer can understand. mples\getStarted\applet\example\HelloWorld.java && Hello.html z E.g.,C:\Ivo.dir\UCLA_Classes\Summer2002\PIC20A\Examples\JavaTutorialExa mples\getStarted\application\example\HelloWorldApp.java Slide 19 PIC 20A, UCLA, Ivo Dinov Slide 20 PIC 20A, UCLA, Ivo Dinov HelloWorld – Program description HelloWorld – Program description zKinds of Comments: zDefining a Class: z /* text */ z class HelloWorldApp { z Compiler ignores everything from opening /* to closing */. public static void main(String [] args)){ z /** documentation */ System.out.println("Hello World!"); // Display the string z This style indicates a documentation comment (doc comment, for } short). Compiler ignores all the text within the comment. The SDK } javadoc tool uses doc comments to automatically generate docs. z A class is the basic building block of an object-oriented language, z Example:C:\Ivo.dir\UCLA_Classes\Summer2002\PIC20A\Examples\JavaTutor ialExamples\getStarted\applet\example\doc\index.html such as Java. A class is a blueprint that describes the state and the behavior associated with instances of that class. When you z // text instantiate a class, you create an object that has the same states z The compiler ignores everything from the //to the end of the line. and behaviors as other instances of the same class. z The state associated with a class or an object is stored in member variables. Slide 21 PIC 20A, UCLA, Ivo Dinov Slide 22 PIC 20A, UCLA, Ivo Dinov HelloWorld – Program description HelloWorld – Program description zDefining a Class: zMain method of a Class: z The behavior associated with a class or an object is implemented z The entry point of every Java application is its main method.