01slide_2007 - Chapter 1 Introduction to Computers,...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Chapter 1 Introduction to Computers, Programs, and Java EE3206/EE5805 Java Programming & Application EE3206/EE5805 1 Objectives To review computer basics, programs, and operating To systems. To understand the relationship between Java and the World To Wide Web. To know Java’s advantages. To To distinguish the terms API, IDE, and JDK. To To write a simple Java program. To To create, compile, and run Java programs. To To understand the Java runtime environment. To To know the basic syntax of a Java program. To To display output on the console and on the dialog box. To EE3206/EE5805 Java Programming & Application EE3206/EE5805 2 What is a Computer? A computer consists of a CPU, memory, hard disk, floppy disk, monitor, printer, and communication devices. Bus Storage Devices e.g., Disk, CD, and Tape Memory CPU Communication Devices e.g., Modem, and NIC Input Devices e.g., Keyboard, Mouse Output Devices e.g., Monitor, Printer EE3206/EE5805 Java Programming & Application EE3206/EE5805 3 Memory Memory is to store data and program instructions for CPU to execute. A memory unit is an ordered sequence of bytes, each holds eight bits. A program and its data must be brought to memory before they can be executed. A memory byte is never empty, but its initial content may be meaningless to your program. The current content of a memory byte is lost whenever new information is placed in it. Bus Storage Devices e.g., Disk, CD, and Tape Memory CPU Communication Devices e.g., Modem, and NIC Input Devices e.g., Keyboard, Mouse Output Devices e.g., Monitor, Printer EE3206/EE5805 Java Programming & Application EE3206/EE5805 4 How Data is Stored? Data of various kinds, such as Data numbers, characters, and strings, are encoded as a series of bits (zeros and ones). Computers use zeros and ones because digital devices have two stable states, which are referred to as zero and one by convention. zero one Memory address Memory content . . . 2000 2001 2002 2003 2004 . . . 01001010 01100001 01110110 01100001 00000011 Encoding for character ‘J’ Encoding for character ‘a’ Encoding for character ‘v’ Encoding for character ‘a’ Encoding for number 3 The programmers need not to be The concerned about the encoding and decoding of data, which is performed automatically by the system based on the encoding scheme. The encoding scheme varies. For example, character ‘J’ is represented by 01001010 in one byte. A small number such as three can small be stored in a single byte. If computer needs to store a large number that cannot fit into a single byte, it uses a number of adjacent bytes. No two data can share or split a same byte. A byte is the byte minimum storage unit. 5 EE3206/EE5805 Java Programming & Application EE3206/EE5805 Programs Computer programs, known as software, are programs software instructions to the computer. You tell a computer what to do through programs. Without programs, a computer is an empty machine. Computers do not understand human languages, so you need to use computer languages to communicate with them. Programs are written using programming languages. EE3206/EE5805 Java Programming & Application EE3206/EE5805 6 Programming Languages Machine Language Assembly Language High-Level Language Machine language is a set of primitive instructions built into every computer. The instructions are in the form of binary code, so you have to enter binary codes for various instructions. Program with native machine language is a tedious process. Moreover the programs are highly difficult to read and modify. For example, to add two numbers, you might write an instruction in binary like this: 1101101010011010 EE3206/EE5805 Java Programming & Application EE3206/EE5805 7 Programming Languages Machine Language Assembly Language Assembly High-Level Language Assembly languages were developed to make programming easy. Since the computer cannot understand assembly language, however, a program called assembler is used to convert assembly language programs into machine code. For example, to add two numbers, you might write an instruction in assembly code like this: ADDF3 R1, R2, R3 Assembly Source File Machine Code File … ADDF3 R1, R2, R3 … Assembler … 1101101010011010 … EE3206/EE5805 Java Programming & Application EE3206/EE5805 8 Programming Languages Machine Language Assembly Language High-Level Language High The high-level languages are English-like and easy to learn and program. For example, the following is a high-level language statement that computes the area of a circle with radius 5: area = 5 * 5 * 3.1415; More natural to human mind! EE3206/EE5805 Java Programming & Application EE3206/EE5805 9 Popular High-Level Languages COBOL (COmmon Business Oriented Language) COBOL FORTRAN (FORmula TRANslation) FORTRAN BASIC (Beginner All-purpose Symbolic Instructional Code) BASIC Pascal (named for Blaise Pascal) Pascal Ada (named for Ada Lovelace) Ada C (whose developer designed B first) (whose Visual Basic (Basic-like visual language developed by Microsoft) Visual Delphi (Pascal-like visual language developed by Borland) Delphi C++ (an object-oriented language, based on C) C++ Java (We use it in the book) Java EE3206/EE5805 Java Programming & Application EE3206/EE5805 10 Compiling Source Code A program written in a high-level language is called a source program. Since a computer cannot understand a source program. Program called a compiler is used to translate the source program compiler into a machine language program called an object program. The object object program is often then linked with other supporting library code before the object can be executed on the machine. Source code Object/Machine code Source File Compiler Object File Linker Excutable File EE3206/EE5805 Java Programming & Application EE3206/EE5805 11 Compiling Java Source Code You can port a source program to any machine with appropriate compilers. The source program must be recompiled, however, because the object program can only run on a specific machine. (Platform dependent) Nowadays computers are networked to work together. Java was designed to run object programs on any platform. With Java, you write the program once, and compile the source program into a special type of object code, known as bytecode. The bytecode can bytecode then run on any computer with a Java Virtual Machine (JVM) which is a software that interprets Java bytecode. (Platform independent) EE3206/EE5805 Java Programming & Application EE3206/EE5805 Java Bytecode Java Virtual Machine Any Computer 12 Java History and Versions Designed by a team from Sun Microsystems led Designed by James Gosling in the early 1990’s Originally named “Oak” for embedded consumer Originally electronic applications Renamed to “Java” in May 20, 1995 for Internet Renamed applications Java Development Kit (JDK) Java • • • • • • • JDK JDK JDK JDK JDK JDK JDK 1.0 1.1 1.2 1.3 1.4 1.5 1.6 (1995) (1996) (1998) (2000) (2002) (2004) (2006) also know as JDK 6 or Java 6 also EE3206/EE5805 Java Programming & Application EE3206/EE5805 13 Java Platforms EE3206/EE5805 Java Programming & Application EE3206/EE5805 14 JDK Editions Java Standard Edition (Java SE) Java • Java SE can be used to develop client-side standalone applications or applets. Java Enterprise Edition (Java EE) Java • Java EE can be used to develop server-side applications such as Java servlets and Java ServerPages. Java Micro Edition (Java ME). Java • Java ME can be used to develop applications for mobile devices such as cell phones. This course uses Java SE to introduce Java programming. This EE3206/EE5805 Java Programming & Application EE3206/EE5805 15 Why Java? •Java is a general purpose programming language. •Java enables users to develop and deploy applications on the Internet for servers, desktop computers, and small hand-held devices. •Java can be used to develop Web applications. •Java Applets, Java Servlets and JavaServer Pages •Java can also be used to develop applications for hand-held devices such as Palm and cell phones EE3206/EE5805 Java Programming & Application EE3206/EE5805 16 Examples of Java’s Versatility Standalone Application: TicTacToe Applet: TicTacToe Servlets: SelfTest Web site Mobile Computing: Cell phones EE3206/EE5805 Java Programming & Application EE3206/EE5805 17 Java’s Advantages As stated in the white paper by Sun, Java is As • Simple and Object-Oriented (simplify from C++) • Distributed (network programming) • Interpreted, portable and architecture-neutral • Robust and secure (exception handling, digital signature) • High-performance? (Hotspot technology) • Multi-threaded (simultaneous tasks) • Dynamic (no installation, recompilation, load-on-the-fly) • (reading - www.cs.armstrong.edu/liang/intro6e/JavaCharacteristics.pdf ) EE3206/EE5805 Java Programming & Application EE3206/EE5805 18 Composing Java Source Java IDE Tools Java • NetBeans Open Source by Sun • Sun ONE Studio by Sun MicroSystems • Eclipse Open Source by IBM • Borland JBuilder Plain Text Editor Plain • Notepad We will use NetBeans in the tutorial class EE3206/EE5805 Java Programming & Application EE3206/EE5805 19 A Simple Java Program Listing 1.1 //This program prints Welcome to Java! public class Welcome { public static void main(String args) { System.out.println("Welcome to Java!"); } } Welcome EE3206/EE5805 Java Programming & Application EE3206/EE5805 20 Create/Modify Source Code Source code (developed by the programmer) Saved on the disk public class Welcome { public static void main(String args) { System.out.println("Welcome to Java!"); } } Source Code Creating, Compiling, and Running Programs Byte code (generated by the compiler for JVM to read and interpret, not for you to understand) Compile Source Code i.e., javac Welcome.java If compilation errors stored on the disk … Method Welcome() 0 aload_0 … Method void main(java.lang.String) 0 getstatic #2 … 3 ldc #3 <String "Welcome to Java!"> 5 invokevirtual #4 … 8 return Bytecode Run Byteode i.e., java Welcome Result If runtime errors or incorrect result EE3206/EE5805 Java Programming & Application EE3206/EE5805 21 animation Trace a Program Execution Enter main method //This program prints Welcome to Java! public class Welcome { public static void main(String args) { System.out.println("Welcome to Java!"); } } EE3206/EE5805 Java Programming & Application EE3206/EE5805 22 animation Trace a Program Execution Execute statement //This program prints Welcome to Java! public class Welcome { public static void main(String args) { System.out.println("Welcome to Java!"); } } EE3206/EE5805 Java Programming & Application EE3206/EE5805 23 animation Trace a Program Execution //This program prints Welcome to Java! public class Welcome { public static void main(String args) { System.out.println("Welcome to Java!"); } } print a message to the console EE3206/EE5805 Java Programming & Application EE3206/EE5805 24 Compiling and Running Java from the Command Window Set path to JDK bin directory Set • set path=c:\Program Files\java\jdk1.5.0\bin Set classpath to include the current directory Set • set classpath=. Compile Compile • javac Welcome.java Run Run • java Welcome • NOT java Welcome.class EE3206/EE5805 Java Programming & Application EE3206/EE5805 25 Anatomy of a Java Program Comments Comments Package Package Reserved words Reserved Modifiers Modifiers Statements Statements Blocks Blocks Classes Classes Methods Methods The main method The EE3206/EE5805 Java Programming & Application EE3206/EE5805 26 Comments In Java, comments are preceded by two slashes (//) in a line preceded • // comment 1 enclosed between /* and */ in one or multiple lines enclosed • /* multiple lines */ Javadoc format uses /** and */ Javadoc Anything being commented are not regarded as instructions Anything and will not be compiled. EE3206/EE5805 Java Programming & Application EE3206/EE5805 27 Package A Java package is a mechanism for organizing Java classes Java into namespaces. Java packages can be stored in compressed files called JAR Java (Java Archive) files, allowing classes to download faster as a group rather than one at a time. Programmers also typically use packages to organize classes Programmers belonging to the same category or providing similar functionality. Package is mirrored to the file structure Package • java.lang.Math (class Math in package java.lang) Math java.lang • $CLASSPATH/java/lang/Math.class EE3206/EE5805 Java Programming & Application EE3206/EE5805 28 Reserved Words and Modifiers Reserved words or keywords are words that have a specific Reserved meaning to the compiler and cannot be used for other purposes in the program. For example, when the compiler sees the word class, it For understands that the word after class is the name for the class. Other reserved words in Listing 1.1 are public, static, and void. Their use will be introduced later. Java uses certain reserved words called modifiers that Java specify the properties of the data, methods, and classes and how they can be used. Examples of modifiers are public, protected, private, final, Examples static and abstract. EE3206/EE5805 Java Programming & Application EE3206/EE5805 29 Statements and Blocks A statement represents an action or a sequence of actions. statement The statement System.out.println("Welcome to Java!") in the program in Listing 1.1 is a statement to display the greeting "Welcome to Java!" Every statement in Java ends with a semicolon (;). A pair of braces {} in a program forms a block that groups pair components of a program. public class Test { public static void main(String args) { System.out.println("Welcome to Java!"); } } Class block Method block EE3206/EE5805 Java Programming & Application EE3206/EE5805 30 Classes and Methods A class is a template or blueprint for objects. class The class is a essential Java construct. To program in Java, The you must understand classes and be able to write and use them. Similar to a function (procedural programming), a method is Similar a collection of statements that performs a sequence of operations. A method must be associated with a class. method EE3206/EE5805 Java Programming & Application EE3206/EE5805 31 main Method Any programs must provide an entry point for the start of Any execution. The Java interpreter executes the application by invoking the main method. The main method usually provides the control of program The flow. The main method looks like this: public static void main(String args) { // Statements; } EE3206/EE5805 Java Programming & Application EE3206/EE5805 32 Displaying Text Message To console To • System.out.println(“message”); Use the showMessageDialog method in the JOptionPane Use class, where x is a string for the text to be displayed and y is a string for the title of the message dialog box. JOptionPane.showMessageDialog(null, x, y, OptionPane.INFORMATION_MESSAGE)); or JOptionPane.showMessageDialog(null, x); WelcomeInMessageDialogBox EE3206/EE5805 Java Programming & Application EE3206/EE5805 33 The exit Method A program will be terminated by calling program System.exit() or upon the end of execution. Prior to JDK 1.5, you have to invoke this Prior method to terminate the program explicitly if the program uses JOptionPane dialog boxes. Since JDK 1.5, it is not necessary. EE3206/EE5805 Java Programming & Application EE3206/EE5805 34 ...
View Full Document

This note was uploaded on 01/11/2011 for the course EE 3206 taught by Professor Cwting during the Spring '07 term at City University of Hong Kong.

Ask a homework question - tutors are online