JAVA how to program Seventh Edition Deitel Deitel

  • Slides: 45
Download presentation

 کﺘﺎﺏ ﺑﺮﻧﺎﻣﻪ ﻧﻮیﺴی JAVA how to program, Seventh Edition, Deitel & Deitel, 2007

کﺘﺎﺏ ﺑﺮﻧﺎﻣﻪ ﻧﻮیﺴی JAVA how to program, Seventh Edition, Deitel & Deitel, 2007 Code Complete, Second Edition, Steve Mc. Connell, Microsoft Press, 2004.

Instructions in Assembly and Machine Language

Instructions in Assembly and Machine Language

High-Level Assembly-Machine Code

High-Level Assembly-Machine Code

Evolution of Programming Languages High-level languages make programming easier Closer to spoken languages Examples

Evolution of Programming Languages High-level languages make programming easier Closer to spoken languages Examples Basic FORTRAN COBOL C/C++ Java

Java is Simple? Truth: Java greatly simplifies several language features Java has automatic memory

Java is Simple? Truth: Java greatly simplifies several language features Java has automatic memory management Does Windows and takes out the garbage No dangling pointers. No memory leaks. A problem for real-time programs Java simplifies pointer handling No explicit reference/dereference operations Everything is a pointer (like Lisp) No makefiles No header files C++ syntax streamlined

Java is Powerful? Myth: Java will increase programmer productivity for all applications by XXX%.

Java is Powerful? Myth: Java will increase programmer productivity for all applications by XXX%. Myth: Java will kill C++ Myth: All software should be written in Java Unix utilities: C Small/medium Windows-only programs: Visual Basic String parsing: Perl High-performance, single-platform OO systems: C++ Air traffic control, aircraft flight software: Ada Knowledge-based systems: Lisp/CLOS/Prolog

Basic Hello World Application “Application” is Java lingo for a stand-alone Java program Note

Basic Hello World Application “Application” is Java lingo for a stand-alone Java program Note that the class name and the filename must match A file can contain multiple classes, but only one can be declared public, and that one’s name must match the filename File Hello. World. java: public class Hello. World { public static void main(String[] args) { System. out. println("Hello, world. "); } }

Basic Hello World Application (Continued) Compiling: javac Hello. World. java Running: java Hello. World

Basic Hello World Application (Continued) Compiling: javac Hello. World. java Running: java Hello. World Output: Hello, world.

Command Line Arguments File Show. Args. java: public class Show. Args { public static

Command Line Arguments File Show. Args. java: public class Show. Args { public static void main(String[] args) { for(int i=0; i<args. length; i++) { System. out. println("Arg " + i + " is " + args[i]); } } }

Command Line Arguments, Results Compiling and Running: > javac Show. Args. java > java

Command Line Arguments, Results Compiling and Running: > javac Show. Args. java > java Arg 0 Arg 1 Arg 2 Arg 3 Show. Args fee fie foe fum is fee is fie is foe is fum

Basic Hello WWW Applet File Hello. WWW. java: import java. applet. Applet; import java.

Basic Hello WWW Applet File Hello. WWW. java: import java. applet. Applet; import java. awt. *; public class Hello. WWW extends Applet { public void init() { set. Background(Color. gray); set. Foreground(Color. white); set. Font(new Font("Sans. Serif", Font. BOLD, 30)); } public void paint(Graphics g) { g. draw. String("Hello, World Wide Web. ", 5, 35); //5=left, 35=bottom } }

Basic Hello WWW Applet (Continued) File Hello. WWW. html: <!DOCTYPE HTML PUBLIC "-//W 3

Basic Hello WWW Applet (Continued) File Hello. WWW. html: <!DOCTYPE HTML PUBLIC "-//W 3 C//DTD HTML 4. 0 Transitional//EN"> <HTML> <HEAD> <TITLE>Hello. WWW: Simple Applet Test. </TITLE> </HEAD> <BODY> <H 1>Hello. WWW: Simple Applet Test. </H 1> <APPLET CODE="Hello. WWW. class" WIDTH=400 HEIGHT=40> <B>Error! You must use a Java enabled browser. </B> </APPLET> </BODY> </HTML>

Basic Hello WWW Applet (Continued) Compiling: javac Hello. WWW. java Running: Load Hello. WWW.

Basic Hello WWW Applet (Continued) Compiling: javac Hello. WWW. java Running: Load Hello. WWW. html in a Java-enabled browser

Customizing Applets with PARAM import java. applet. Applet; import java. awt. *; public class

Customizing Applets with PARAM import java. applet. Applet; import java. awt. *; public class Message extends Applet { private int font. Size; private String message; public void init() { set. Background(Color. black); set. Foreground(Color. white); font. Size = get. Size(). height - 10; set. Font(new Font("Sans. Serif", Font. BOLD, font. Size)); // Read heading message from PARAM entry in HTML. message = get. Parameter("MESSAGE"); } } public void paint(Graphics g) { if (message != null) g. draw. String(message, 5, font. Size+5); }

Customizing Applets with PARAM, cont. <!DOCTYPE HTML PUBLIC "-//W 3 C//DTD HTML 4. 0

Customizing Applets with PARAM, cont. <!DOCTYPE HTML PUBLIC "-//W 3 C//DTD HTML 4. 0 Transitional//EN"> <HTML> <HEAD> <TITLE>The Message Applet</TITLE> </HEAD> <BODY BGCOLOR="WHITE"> <H 1>The <CODE>Message</CODE> Applet</H 1> <P> <APPLET CODE="Message. class" WIDTH=325 HEIGHT=25> <PARAM NAME="MESSAGE" VALUE="Tiny"> <B>Sorry, these examples require Java</B> </APPLET> <P> <APPLET CODE="Message. class" WIDTH=325 HEIGHT=50> <PARAM NAME="MESSAGE" VALUE="Small"> <B>Sorry, these examples require Java</B> </APPLET>. . . </BODY> </HTML>

Customizing Applets with PARAM, Result

Customizing Applets with PARAM, Result

Requirements for the Course Browse-to-download: Java SE JDK 6 latest update for Windows Platform

Requirements for the Course Browse-to-download: Java SE JDK 6 latest update for Windows Platform http: //java. sun. com/ make sure to Accept License Agreement Install JDK (use defaults) Browse-to-and-download: Net. Beans IDE 6. 5 IDE with Java SE update ? Bundle http: //www. netbeans. org/ make sure to Accept License Agreement install Net. Beans (use defaults) Intelli. J IDEA 6. 0 Eclipse - IBM Rational Developer - Jdeveloper - Jbuilder - etc. NOTEPAD !!!!!!!!!!!!!!!!!!!

Evolution of Programming Languages Compiler: a program that translates a program written in a

Evolution of Programming Languages Compiler: a program that translates a program written in a high-level language into the equivalent machine language In the case of Java, this machine language is the bytecode Java Virtual Machine (JVM): hypothetical computer developed to make Java programs machine independent

Problem-Analysis-Coding-Execution Cycle

Problem-Analysis-Coding-Execution Cycle

First Cup of Java A Step by step example on how to Create Compile

First Cup of Java A Step by step example on how to Create Compile Run a java program Plus the works What is a Programming Language What is Object Orientation

First Java Program Does and Don'ts Java Environment A Simple Program that Displays Hello

First Java Program Does and Don'ts Java Environment A Simple Program that Displays Hello World Create the Program Compile the Program Load and Run the Program

First Java Program The Do not(s) Do Not Use the Desktop or “My Documents”

First Java Program The Do not(s) Do Not Use the Desktop or “My Documents” as a folder for your java programs Always Use the Letter Drive as your Root (A, C, D, etc. ) Java Does not Like Spaces in File Names or Folder Names Java is case Sensitive the string hello. Java is not the same as Hello. java

First Java Program Start a DOS Commands window Start Run cmd

First Java Program Start a DOS Commands window Start Run cmd

First Java Program Command Prompt Window

First Java Program Command Prompt Window

First Java Program Create the Appropriate folders

First Java Program Create the Appropriate folders

Which Java Environment are you running javac is not recoginzed: it is not in

Which Java Environment are you running javac is not recoginzed: it is not in my path

Search your drive for the latest JDK Hardwire the path to point to the

Search your drive for the latest JDK Hardwire the path to point to the bin folder

Hardwire the path to point to the bin folder

Hardwire the path to point to the bin folder

java and javac are both recognizable

java and javac are both recognizable

Where is my First Java Program Remember: java is case sensetive

Where is my First Java Program Remember: java is case sensetive

Where is my First Java Program edit and save to the Homeworks folder

Where is my First Java Program edit and save to the Homeworks folder

Where is my First Java Program Use the dir command: 228 Bytes

Where is my First Java Program Use the dir command: 228 Bytes

My First Java Program Comments Java is case sensitive File name extension is. java

My First Java Program Comments Java is case sensitive File name extension is. java (Lower case) Class Name in public class Hello. World Should exactly match the first part of the filename Hello. World. java A runnable Java Program is a Java class with a method called: public static void main(String[] args){ //Control Flow Code }

Compiling My First Java Program javac Helloworld. java Helloworld. class is created

Compiling My First Java Program javac Helloworld. java Helloworld. class is created

Compiling My First Java Program Where did Hello. World. class come from

Compiling My First Java Program Where did Hello. World. class come from

Running My First Java Program java Helloworld

Running My First Java Program java Helloworld

Try this Hello. World. java program Create, compile and run // // My First

Try this Hello. World. java program Create, compile and run // // My First Java Program //our Info // // import javax. swing. *; public class Hello. World{ public static void main(String[] args){ try{ String s = JOption. Pane. show. Input. Dialog("Please Enter your Name"); System. out. println("Hello " + s); System. out. print(" Welcome to CIS at WTAMU"); } catch(Exception e){ } } }

Summary

Summary

My J 2 SE Environment Install Environment

My J 2 SE Environment Install Environment

Java 2 Standard Edition Components http: //java. sun. com/javase/6/docs/

Java 2 Standard Edition Components http: //java. sun. com/javase/6/docs/