Sie sind auf Seite 1von 43

Chapter 1 Introduction to Computers, Programs, and Java

Objectives

Review computer basics, programs, and operating systems Explore the relationship between Java and the World Wide Web Distinguish the terms API, IDE, and JDK Write a simple Java program Display output on the console Explain the basic syntax of a Java program Create, compile, and run Java programs Display output using the JOptionPane output dialog boxes
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

Central Processing Unit (CPU)


Brain of a computer Control Unit cooridinates actions of components Arithmetic/logic unit performs numeric/logical operations Retrieves instructions from memory and executes them CPU speed is measured in gigahertz (GHz)
Bus

Storage Devices e.g., Disk, D, and Tape

Memory

ommunication Devices e.g., Modem, and NI

Input Devices e.g., Keyboard, Mouse

Output Devices e.g., Monitor, rinter

Memory
Store data and program instructions for CPU to execute emory unit is an ordered sequence of bytes Byte is 8 bits Program and its data must be brought to memory before they can be executed Random Access
Bus

Storage Devices e.g., Disk, D, and Tape

Memory

ommunication Devices e.g., Modem, and NI

Input Devices e.g., Keyboard, Mouse

Output Devices e.g., Monitor, rinter

Memory
Memory is volatile Information is lost when the power is off Programs and data are permanently stored on storage Moved to memory for execution

Bus

Storage Devices e.g., Disk, D, and Tape

Memory

ommunication Devices e.g., Modem, and NI

Input Devices e.g., Keyboard, Mouse

Output Devices e.g., Monitor, rinter

How Data is Stored?


Data 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. Encoding and decoding of data is performed automatically by the system based on the encoding scheme Byte is the minimum storage unit. Encoding scheme varies
y y y

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

character J is represented by 01001010 in one byte If number cannot fit into a single byte, it uses a number of adjacent bytes No two data can share or split a same byte

Storage Devices
Disk drives (hard and floppy) CD/DVD Flash Tape drives

Bus

Memory

CPU

e.g., isk, C , and Tape

e.g., Modem, and IC

e.g., eyboard, Mouse

Storage evices

e.g., Monitor, Printer

Communication evices

Input evices

utput evices

Output Devices Monitor


Monitor Displays information (text and graphics) Resolution and dot pitch determine the quality of the display.

Bus

Storage Devices e.g., Disk, CD, and Tape

Memory

CPU

e.g., Modem, and IC

e.g., eyboard, Mouse

e.g., Monitor, Printer

Communication Devices

Input Devices

utput Devices

Monitor Resolution and Dot Pitch


Resolution umber of pixels per square inch Pixels (short for picture elements) Tiny dots that form an image on the screen. Can be set manually Higher the resolution, the sharper and clearer the image Image may be very small if you set high resolution on a small screen monitor Dot Pitch Amount of space between pixels Smaller dot pitch equals better display quality
10

Communication Devices
Modem Digital Subsriber Line (DSL) Cable Modem etwork Interface Card ( IC)

Wireless Modem Bluetooth

Bus

Storage Devices e.g., Disk, CD, and Tape

Memory

CPU

e.g., Modem, and IC

e.g., eyboard, Mouse

e.g., Monitor, Printer

Communication Devices

Input Devices

utput Devices

11

Computer Programs

Known as software
Instructions to the computer

Tells the computer what to do Without software a computer is an empty machine Computer do not understand human languages Need computer languages to communicate Programs written using programming languages
12

Programming Languages
Machine language Set of primitive instructions built into every computer Binary code Enter binary codes for various instructions. Tedious process to program Highly difficult to read and modify Example: adding two numbers might have an instruction in binary like this:
1101101010011010
13

Programming Languages
Assembly languages Developed to make programming easy Computer cannot understand assembly language An assembler program is used to convert assembly into machine code. Example: adding two numbers might write an instruction in assembly code like this: ADDF3 R1, R2, R3
Machine ode File

ADDF3 R1, R2, R3

ssembler

1101101010011010

ssembly Source File

14

Programming Languages
High-level languages English-like 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;

15

Popular High-Level Languages


Business Oriented Language) FORTRAN (FORmula TRANslation) BASIC (Beginner All-purpose Symbolic Instructional Code) Pascal (named for Blaise Pascal) Ada (named for Ada Lovelace) C (whose developer designed B first) Visual Basic (Basic-like visual language developed by Microsoft) Delphi (Pascal-like visual language developed by Borland) C++ (an object-oriented language, based on C) C# (a Java-like language developed by Microsoft) Java (We use it in the book)
16

COBOL (COmmon

Compiling Source Code


Program written in a high-level language is called a source program Computer cannot understand a source program A compiler is used to translate the source program into a machine language program called an object program Object program linked with supporting library code
rce ile iler
ac i e-la ile a e

er

ec ta le ile

i rar

17

Operating Systems (OS)


Control and monitor system activities Allocate and assign resources Scheduling operations Windows XP, 7, etc. Mac OS Linux Unix, VAX, HPUX

ser

pplication rograms

Operating System

ard are

18

Why Java?

General purpose programming language Enables users to develop and deploy applications

Internet for servers Desktop computers Small hand-held devices.

Future of computing is being profoundly influenced by the Internet (Applets and Web Applications) Java promises to remain a big part of that future Java is the Internet programming language
19

Examples of Javas Versatility (Applets)

20

PDA and Cell Phone

21

JDK Editions

Java Standard Edition (J2SE)


y J2SE can be used to develop client-side standalone

applications or applets.

Java Enterprise Edition (J2EE)


y J2EE can be used to develop server-side applications

such as Java servlets and Java ServerPages.

Java Micro Edition (J2ME).


y J2ME can be used to develop applications for mobile

devices such as cell phones.

This book uses J2SE to introduce Java programming.


22

Popular Java IDEs


NetBeans Open Source by Sun Eclipse Open Source by IBM Combine


y Editing y Compiling y Building y Debugging y Help

23

API and JDK


API Application Program Interface
y Contains predefined classes and interfaces

JDK Java Development Kit


y Set of separate programs y Invoked from a command line y For developing and testing

24

Creating, Compiling, and Running Programs


reate/Modi y Source ode
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 ode

Byte code (generated by the compiler or JVM to read and interpret, not or you to understand)

ompile Source ode i.e., javac Welcome.java


I compilation errors stored on the disk

Method void main(java.lang.String[]) 0 getstatic #2 3 ldc #3 <String "Welcome to Java!"> 5 invokevirtual #4 8 return

Bytecode

un Byteode i.e., java Welcome

esult
I runtime errors or incorrect result

Method Welcome() 0 aload_0

25

Compiling Java Source Code


Ability to port a source program to any machine with appropriate compilers Java designed to run object programs on any platform Write once, Run anywhere

y Compile the source program into bytecode y The bytecode can then run on any computer with a Java Virtual

Machine,

Java Virtual Machine (JVM) is a software that interprets Java bytecode


Java Bytecode Java Virtual Machine ny omputer

26

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!"); } }

27

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!"); } }

28

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

29

Companion Website

Compiling and Running Java from the Command Window

Set path to JDK bin directory y set path=c:\Program Files\java\jdk1.6.0\bin Set

classpath to include the current directory


y set classpath=.

Compile y javac Welcome.java Run y java Welcome


30

Compiling and Running Java from Companion TextPad Website

See Supplement II.A on the Website for details

31

Anatomy of a Java Program


Comments Reserved Modifiers Statements Blocks Classes Methods The

words

main method
32

Comments
Line Preceded by two slashes // on a line Paragraph Enclosed between /* and */ in one or multiple lines
Javadoc Begin with /** and end with */ Used for documenting classes, data, and methods Can be extracted into an HTML file using JDK's javadoc command

33

Reserved Words
Reserved words or keywords Have a specific meaning to the compiler Cannot be used for other purposes For example, when the compiler sees the word class, it understands that the word after class is the name for the class public, static, void

34

Modifiers
Reserved words that specify the properties of the data, methods, and classes and how they can be used public, static, private, final, abstract, and protected

35

Statements
Represents an action or a sequence of actions System.out.println("Welcome to Java!")

y Statement to display the greeting "Welcome to Java!

Every statement in Java ends with a semicolon ;

36

Blocks
Pair of braces in a program forms a block that groups components of a program.

public class Test { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }

lass block Method block

37

Classes
Essential Java construct Template or blueprint for objects You must understand classes and be able to write and use them The mystery of the class will continue to be unveiled throughout this book. For now, though, understand that a program is defined by using one or more classes.

38

Methods
Collection of statements that performs a sequence of operations System.out.println()

y Display a message on the console y Can be used even without fully understanding the

details of how it works y Used by invoking a statement with a string argument


Quoted string argument is enclosed within parentheses

39

main Method
Provides the control of program flow Java interpreter executes the application by invoking the main method

The main method looks like this: public static void main(String[] args) { // Statements; }

40

Displaying Text in a Message Dialog Box


Use the showMessageDialog method in the JOptionPane class JOptionPane is one of the many predefined classes in the Java system Can be reused rather than reinventing the wheel.
WelcomeInMessage ialogBox Run

41

The showMessageDialog Method


JOptionPane.showMessageDialog(null, "Welcome to Java!", "Display Message", JOptionPane.INFORMATION_MESSAGE);

42

Two Ways to Invoke the Method



y

Several ways to use the showMessageDialog method For the time being, all you need to know are two ways to invoke it
One is to use a statement as shown in the example: JOptionPane.showMessageDialog(null, x, y, JOptionPane.INFORMATION_MESSAGE);

where x is a string for the text to be displayed, and y is a string for the title of the message dialog box.
y

The other is to use a statement like this: JOptionPane.showMessageDialog(null, x);

where x is a string for the text to be displayed.


43