1 Java Software Solutions Chapter 1 Computer Systems.

68
1 Java Software Solutions Chapter 1 Computer Systems

Transcript of 1 Java Software Solutions Chapter 1 Computer Systems.

1

Java Software Solutions

Chapter 1

Computer Systems

2

Basic Computer Processing

A computer system is made up of hardware and software. Hardware: Chips, keyboards, disks, etc. Software: Programs and the data those

programs use. Key Components of a Computer System

central processing unit (CPU) input/output (I/O) devices main memory secondary memory devices

3

Memory

Programs and data are held in storage devices called memory, which fall into two categories: Main Memory

The storage device (e.g., chips on the mother board) that holds the programs and their data (software) while the programs are executing.

Secondary Memory Devices such as hard disks that store software in a

relatively permanent manner. We save our software on secondary memory.

Copies of the software are brought into main memory when we execute our programs.

4

Software Categories

We will be concerned with two categories of software. The operating system.

Provides a user interface that allows us to interact with the machine (enter input and view output).

Manages computer resources such as the CPU and main memory.

Application Software Generic term for just about any software other than

the operating system. Word processors, web browsers, games, business

applications, etc. The focus of this course is development of high-

quality application programs.

5

What This Means to Us

We will be learning to create application programs using the Java programming language. Initially, the hardware we will use is a computer named

Osprey that is located on campus, but can be accessed from home.

Our software will be the Linux operating system, one of two text editors (vi or Pico), the Java compiler (javac), and the Java interpreter (java).

Other software such as communications software, etc. will be used, but will not require much interaction on our part.

6

Analog versus Digital Signals

Two fundamental techniques are used to store and manage information in a computer system. Analog information is continuous, varying in direct

proportion to the source of the information. An old-fashioned record used analog technology to store music.

Digital technology breaks information into discrete pieces and represents the pieces as numbers. Music on a CD is stored digitally, as a series of

numbers. The music is sampled about 40,000 times per second and each number represents the voltage level of one specific instance of the recording.

7

An Analog Signal versus a Digital Signal

analog digital

8

Binary Numbers

A digital computer stores information as numbers, but those numbers are not stored as decimal numbers. All information in a computer is stored and

managed as binary values. The binary number system has only two digits,

0 and 1. A single binary digit is called a bit.

9

Converting Binary Numbers to Decimal Numbers

Starting with the rightmost bit in a binary number, each position represents a power of 2, with the rightmost bit position representing 20 which is decimal 1, the next bit position representing 21 which is 2 and the next representing 22 which is 4, etc.

Binary 1011 is 1* 23 or 8, plus 0 * 22 or 0, plus 1 * 21 or 2 plus 1 * 20 or 1 which adds up to 11 decimal.

Binary 11001001 is 201 decimal. In general, N bits can represent 2N unique items. Computers typically manipulate sequences of 8 bits

with each sequence of 8 bits referred to as a byte.

10

Hardware Components

We use the term computer architecture to describe how the hardware components of a computer are put together.

A program and its data reside in main memory while the program runs. The Central Processing Units (CPU) reads one

program instruction from main memory and executes it. If an instruction needs data, such as the amount in a

bank account, the CPU reads that information as well. The process repeats until the program ends.

11

Peripherals

Almost all devices in a computer other than the CPU and main memory are called peripherals. Controllers are devices that coordinate the

activities of specific peripherals. Input/Output (I/O) devices and secondary

memory devices are considered peripherals.

12

Input/Output Devices

For our purposes, the primary I/O devices will be the keyboard and the monitor. Output on a monitor is achieved by breaking

the screen picture into small pieces called picture elements (pixels). The pixels are represented in the computer as numbers (as is everything else).

A typical monitor can display a grid of 1280 x 1024 pixels.

13

Main Memory and Secondary Memory

Main memory is made up of a series of small, consecutive memory locations. Associated with each memory location is a unique

number called an address. On many computers, each memory location consists of

8 bits, or one byte of information. The storage capacity of a device such as main

memory is the total number of bytes it can hold. A kilobyte (KB)is 1,024 bytes or 210 bytes. Larger units are a megabyte (MB), a gigabyte (GB),

and a terabyte (TB). It is usually easier to think about these capacities by

rounding them off.

14

Characteristics of Main Memory

When data is stored in a main memory location, it overwrites (thus destroying) any information that was previously stored there.

Main memory is usually volatile. The information stored in it will be lost if its

electrical power is turned off. You should frequently save your work onto a

secondary memory device such as a disk in case the power goes off.

15

Secondary Memory Devices

A disk is a direct access (also referred to as random access) device since the read/write head can move very quickly to the information needed.

A tape must be rewound or fast-forwarded to get to the desired information. A tape is therefore considered a sequential

access device.

16

Two Other Common Termsfor Memory Devices

Random Access Memory (RAM) The terms RAM and main memory are basically

interchangeable. RAM should probably be called read-write memory

since data can be both written to it and read from it. Read-Only Memory (ROM)

After information is stored on ROM, it cannot be altered (easily).

ROM chips are often embedded into the main circuit board to provide the preliminary instructions when the computer is initially turned on.

17

Other Memory Devices

Additional memory devices include CD-ROM, CD-Recordable (CD-R) drives, CD Rewritable (CD-RW) discs, and DVD devices. The speed of a CD drive is expressed in

multiples of x, which represents a data transfer speed of 153,600 bytes per second.

Due to improvements in technology, storage capacity approximately doubles every 18 months.

18

Networks

A network is two or more computers connected together so they can exchange information. Each computer of a network has its own

network address. One technique to improve network speed is to

divide large messages into segments, called packets.

Then send the individual packets across the network intermixed with pieces of other messages sent by other users.

19

Local Area Networks and Wide Area Networks

A local-area network (LAN) is designed to span short distances and connect a relatively small number of computers.

A wide-area network (WAN) connects two or more LANs, often across long distances. LANs connected by a WAN are often owned

by different companies or organizations, and might even be located in different countries.

20

The Internet

The Internet is a network of networks. A protocol is a set of rules that governs how

two things communicate. The software that controls the movement of

messages across the Internet must conform to a set of protocols called TCP/IP for Transmission Control Protocol/Internet Protocol.

Every computer connected to the Internet has an IP address that uniquely identifies it among all other computers on the Internet.

21

IP Address vs Internet Address

An IP address is of the form 204.192.116.2. Fortunately, the Internet allows each

computer to be given a unique name which is often referred to as its Internet Address. The first part of an Internet Address is the

local name of a specific computer. The rest of the name is the domain name.

The computer you will be using in this course is osprey.unf.edu. edu is the domain name.

22

The Domain Name System

The Internet uses another software program, the Domain Name System (DNS) to translate an Internet Name to its corresponding IP Address. Each organization connected to the Internet

operates a domain server that maintains a list of all computers in the organization and their IP addresses.

If the local domain server does not have the IP address for a name, it contacts another domain server that does.

23

The World Wide Web

The World Wide Web (also known as WWW or simply the Web) makes the exchange of information easy. Web software provides a common interface

through which many different types of information can be accessed with the click of a mouse.

The Web is based on the concepts of hypertext and hypermedia.

Hypermedia incorporates graphics, sound, animations, and video.

24

Hypertext and Browsers

Hypertext is a way to organize information so that the flow of ideas is not constrained to a linear progression. Documents are linked at various points

according to natural relationships so that the reader can jump from one document to another, following the appropriate path for that reader’s needs.

Browsers allow documents to be formatted using the HyperText Markup Language (HTML).

25

Uniform Resource Locators

Information on the Web is found by identifying a Uniform Resource Locator (URL) A URL uniquely specifies documents and other

information for a browser to obtain and display. An example URL is http://www.unf.edu

The first piece of a URL is a protocol (HyperText Transfer Protocol – HTTP in this example) which determines the way the browser should communicate.

The second piece is the Internet address of the machine on which the document is stored.

A possible third piece of information is the file name of the file you wish to view.

26

Another Sample URL

http://www.unf.edu/campus In this URL, the protocol is http (HyperText Transfer

Protocol). The machine referenced is www (a web server found

at unf.edu) The information is stored in a file named campus.

The Internet vs the World Wide Web The Internet makes it possible to communicate via

computers around the world. The Web makes that communication a straightforward

and enjoyable activity. The Web can be used on a LAN or even a single machine

that is not connected to any network.

27

Programming

The rest of this course explores the process of creating programs using Java. All of the software we have discussed

including the Internet and Web protocols are programs that some programmer has written.

This section discusses the purpose of programming in general and introduces the Java Programming Language.

28

Problem Solving

The purpose of writing a program is to solve a problem. Problem solving consists of multiple steps:

1. Understanding the problem.

2. Breaking the problem into manageable pieces.

3. Designing a solution.

4. Considering alternatives to the solution and refining the solution.

5. Implementing the solution.

6. Testing the solution and fixing any problems that exist.

29

How do these Steps Apply to You?

By the time you complete a degree in Computer and Information Sciences you will have experienced all of these problem solving steps and practiced them often.

In this course, the emphasis is on learning programming and much of the other work is done for you. However, you should be aware that even for beginning

students who are assigned a problem and provided a lot of information, the most common mistake is to begin coding before you have put some thought into understanding the problem and designing a solution.

30

The Java Programming Language

A programming language defines a set of rules: That determine exactly how a programmer can

combine words and symbols of the language into programming statements.

Programming statements are the instructions that are carried out when the program is executed.

Java is an object-oriented programming language.

Java is one of the fastest growing programming technologies of all time.

31

Reasons Why Java is Popular

Java is the first programming language to deliberately embrace the concept of writing programs that can be executed on the Web.

The Java language is accompanied by a library of extra software that we can use when developing programs. The library provides the ability to create

graphics, communicate over networks, and interact with databases.

The set of supporting libraries is huge and versatile.

32

A Java Program See Lincoln.java on page 30.

/* Lewis/Loftus 6/11/2003

Lincoln.java demonstrates the basic structure of a Java Application.*/

public class Lincoln{ // Prints a presidential quote. public static void main (String[ ] args) { System.out.println (“A quote by Abraham Lincoln:“); System.out.println (“Whatever you are, be a good one.”); }}

33

What to Notice in Lincoln.java

The first few lines between /* and */ are comments. Comments don’t affect what the program

does but are included to make the program easier to understand.

Comments included in a program are called inline documentation.

The comment symbols /* and */ can surround a multiline comment.

The comment symbol // can be used at the beginning of a single-line comment.

34

What to Notice (2)

The rest of the Lincoln.java program is a class name and a class definition. This class name is Lincoln. The class definition runs from the first

opening brace { to the final closing brace }. Inside the class definition is a single-line

comment describing the purpose of the main() method.

A method is a group of programming statements that are given a name (main).

35

The println() Method

The two lines of code in the main() method invoke another method called println() (pronounced print line). Our program must invoke, or call, a method at the

point where we want the method to execute. The println() method prints the specified characters to

the screen. The characters to be printed are represented as a

character string, enclosed in double quote characters (“The string you want to print. “).

The code for the println() method definition is in a library. We just need to call the println() method and pass it the String you want to display.

36

Another Look at Lincoln.java See the Lincoln program on page 30.

/* Lewis/Loftus 6/11/2003

Lincoln.java demonstrates the basic structure of a Java Application.*/// The next line names the class and the { begins the class definitionpublic class Lincoln // The class name is the same as the file name, but{ // without the .java extension. // Prints a presidential quote. public static void main (String[] args) // This line is the method header { // The method body starts with the left bracket.

// The println() method is found in the System class in a Java library System.out.println(“A quote by Abraham Lincoln:“); System.out.println(“Whatever you are, be a good one.”); }}

37

The Method Named main()

All java applications have a method named main. This is where processing begins for every application

program. The main method definition in Java is always preceded by

the words public, static, and void. Each programming statement in the main method is

executed, one at a time in order, until the end of the method is reached.

Some of the statements in the main method may call another method. If so, that method executes and control then returns to the next statement in main.

After the last statement in main executes, the program terminates.

The use of String and args will be explained later.

38

Comments

Comments are notes entered into the text of a program to assist that programmer or any other programmer who must later modify or simply understand the code. The original programmer probably will not

remember later the details of the program he or she has written.

Comments within the text of a program are referred to as internal documentation. Good internal and external documentation

are both essential.

39

Two Types of Comments

A single-line comment in Java takes the form: // This is a comment.

A multi-line comment takes the form: /* This is one comment that spans several lines. */ It is also permissible to use the above notation

for a single-line comment. /* This is a comment */

40

External Documentationjavadoc

The Java SDK (Software Development Kit) also specifies a means of creating external documentation using a software tool called javadoc . Here is how it works: If you place a second asterisk following the /* at the

beginning of a comment, the content of the comment can be used to automatically generate external documentation about your program using a tool called javadoc .

Example: /** javadoc will print this comment

for use as external documentation.

*/

41

Good Programming Practices

Appendix G of our text presents guidelines for good programming practices and includes specific techniques for documenting programs. You can assume the reader is computer literate and

familiar with Java. However, you must assume the reader knows almost

nothing about what the program is supposed to do. A section of code that seems intuitive to you when you

write it might not seem so to another reader or to yourself later.

You will be expected to apply the coding guidelines in Appendix G (starts on page 705) as you write your programs.

42

Identifiers and Reserved Words

The words used when writing programs are called identifiers. There are three categories of identifiers:

1. Words that we make up (Lincoln and args in demo program 1.1)

2. Words that another programmer chose (String, System, out, println, and main). Often these are words that were chosen by a programmer

who wrote code that became part of a Java library and are now available for use by all Java programmers.

3. Words that are reserved for special purposes in the language (class, public, static, and void). The designers of the Java language chose these

words.

43

The Reserved Words Reserved words are identifiers that have special meaning in a programming

language. In the Lincoln program, they are class, public, static, and void.

//********************************************************************// Lincoln.java Author: Lewis/Loftus//// Demonstrates the basic structure of a Java application.//********************************************************************

public class Lincoln{

//-----------------------------------------------------------------// Prints a presidential quote. //-----------------------------------------------------------------public static void main (String[] args) { System.out.println ("A quote by Abraham Lincoln:");

System.out.println ("Whatever you are, be a good one."); }}

44

Reserved Words

The Java reserved words – these words cannot be used for any other purpose, such as naming a class or method.

abstractbooleanbreakbytecasecatchcharclassconstcontinuedefaultdodouble

elseextendsfalsefinalfinallyfloatforgotoifimplementsimportinstanceofint

interfacelongnativenewnullpackageprivateprotectedpublicreturnshortstaticstrictfp

superswitchsynchronizedthisthrowthrowstransienttruetryvoidvolatilewhile

45

Rules for Naming Identifiers

An identifier that we create for use in a program can be composed of any combination of letters, digits, the underscore character ( _ ), and the dollar sign ($), but it cannot begin with a digit.

Java is case sensitive, which means that two identifier names that differ only in the case of their letters are considered to be different identifiers. Total, Total, ToTaL, and TOTAL are all

different identifiers.

46

Conventions for Naming Identifiers

Using a consistent case format for each kind of identifier makes the different kinds of identifiers easier to distinguish. Title case (uppercase for the first letter of each word)

is conventional for class names. Example: GraduateStudent

Lowercase is used for the first letter of method names and variable names. However, the first letter of each additional word that is part of the identifier is capitalized.

Example: payRate Appendix G presents guidelines for naming identifiers.

You are to follow those guidelines in your programs.

47

Choosing Identifier Names

Identifier names should be descriptive but not verbose. Avoid meaningless names such as a or x.

Unless the name is actually descriptive, such as using x and y to represent (x, y) coordinates.

Avoid using unnecessarily long names. A “name” in Java is a series of identifiers

separated by the dot (period) character. The name System.out is the way we

designate the object through which we invoke the println method.

48

White Space

White space consists of blanks, tabs, and newline characters. White space is used to separate the words and

symbols used in Java programs. A programmer can use white space to emphasize parts

of the program and to make the program easier to read.

Except when it’s used to separate words, the computer ignores white space. This gives programmers great flexibility in how they

format a program. The guidelines in Appendix G discuss the appropriate

use of white space.

49

Programming Languages

Programming languages are often categorized into the following four groups:

1. Machine Language2. Assembly Language3. High-Level Languages4. Fourth-Generation Languages

In order for a program to run on a computer, it must be expressed in that computer’s machine language. Each type of CPU has its own language.

50

From Machine Languageto Assembly Language

Each machine language instruction can accomplish only one simple task. For example, copy a value into a register or

compare two values. Machine language code is expressed as a

series of binary digits and is extremely difficult for humans to read and write. This gave rise to Assembly Language, which

replaced binary digits with mnemonics, short English-like words that represent commands or data.

51

From Low-level Languages to High-level Languages

Both assembly language and machine language are considered low-level languages. Generally, each assembly language instruction

corresponds to an equivalent machine language instruction.

Today most programmers use a high-level language to write software. A high-level language is expressed in English-like

phrases, approaching natural language. A single high-level language programming statement

can accomplish the equivalent of many – perhaps hundreds – of machine language instructions.

52

Fourth-Generation Languages

Some programming languages operate at an even higher level. They might include automatic report

generation or interaction with a database. These languages are called fourth-

generation languages, or simply 4GLs, because they followed the first three generations of computer programming: machine, assembly, and high-level.

53

Compilers and Interpreters

Initially, you will use an editor as you type a program into a computer and store it in a file. The editor you will use will be either vi or pico. Each time you need to make a change to the

code in your program, you “open” the program in an editor.

After editing and saving your program, you attempt to translate it from high-level code into a form that can be executed.

The translation may result in errors, in which case you return to the editor to make changes to the code to fix the problem.

54

Editing and Running Your Program

Once the translation occurs successfully, you can execute the program and evaluate the results. If your results are not what you want (or if you

want to enhance your existing program), you again return to the editor to make changes.

Edit and

Save Program

Translate Program

into Executable Form

Execute Program

and Evaluate Results

errorserrors

55

Translation of Source Code

The translation of source code into (ultimately) machine language for a particular type of CPU can occur in a variety of ways. By using a compiler. By using an interpreter. By using both a compiler and interpreter

This is how Java does it.

56

A Compiler

A compiler is a program that translates code in one language to an equivalent code in another language. The original code is called source-code. The language into which it is translated is

called the target language. For many traditional compilers, the source

code is translated directly into a particular machine language.

In that case, the translation process occurs once and the resulting executable program can be run whenever needed.

57

An Interpreter

An interpreter is similar to a compiler but has an important difference. An interpreter interweaves the translation and

execution activities. A small part of the source code, such as one

statement, is translated and executed. Then another part is translated and executed, and so

on. This eliminates the need for a separate compilation

phase; however, the program runs more slowly because the translation process occurs during each execution.

Each statement is translated, then executed immediately, rather than translating the entire program and then executing the translated code.

58

Java Uses Both a Compiler and Interpreter

The Java compiler translates Java source code into Java bytecode. Bytecode is a representation of the program

in a low-level code similar to machine language code.

The Java interpreter reads a short segment of Java bytecode, translates that segment into machine language and executes it. This process is repeated as the program executes.

59

The Advantage of Java Bytecode

The difference between Java bytecode and true machine language code is that Java bytecode is not tied to any particular processor type. This makes Java architecture neutral, and

therefore easily portable from one machine to another.

All you need is a Java interpreter or bytecode compiler for each processor type on which the bytecode is to be executed.

60

Compiling and Executing Programson Osprey

Assume you want to write the Lincoln.java program and execute it on Osprey.

At the Linux $ prompt you will create your source code by entering either:

$ pico Lincoln.java or$ vi Lincoln.java

Once you have saved your source code, you compile your source code by entering at the $ prompt:

$ javac Lincoln.java Correct any errors the compiler finds.

When you have a successful compile, you will translate your bytecode (execute your program) by entering at the $ prompt:

$ java Lincoln

61

The Java Software Development Kit (JDK)

The Java compiler and interpreter are part of the Java Software Development Kit (Java SDK or just JDK). The JDK can be downloaded for free from the Sun

Microsystem Web site ( java.sun.com ) or from our text’s website http://duke.csc.villanova.edu/jss1/javaResources/sdk.jsp .

Note: the standard JDK tools are executed on the command line and do not include an editor. The JDK is already on osprey, so you do not need to download it.

Any editor that can save a document as a simple text file can be used. We will use pico or vi initially.

62

Integrated Development Environments (IDEs)

IDEs combine an editor, compiler, and other Java support tools into a single program. There are many IDEs including Borland’s

JBuilder and Xinox Software’s JCreator.

63

Syntax and Semantics

Each programming language has its own unique syntax. The syntax rules of a language dictate exactly

how the vocabulary elements of the language can be combined to form statements.

During compilation, all syntax rules are checked.

If a program is not syntactically correct, the compiler will issue error messages and will not produce bytecode.

64

Java Syntax

Java has a similar syntax to C and C++. However, beyond the basic syntax issues,

there are many important differences between Java and these languages.

Appendix I contains a summary of the differences between Java and C++.

65

Semantics

The semantics of a statement in a programming language define what will happen when that statement is executed. Programming languages are generally

unambiguous, which means that there is one and only one interpretation for each statement.

The same exact English sentence can have multiple valid meanings.

A computer language cannot allow such ambiguities to exist.

66

Errors

You will encounter three kinds of errors as you develop programs:

1. Compile-time errors

2. Runtime errors

3. Logical errors

67

Compile-time and Run-time Errors

An error identified by the compiler is called a compile-time error. If a compile-time error occurs, an executable version of

the program is not created. Use your editor to correct the error, then recompile

your program. A runtime error causes the program to terminate

abnormally during execution. An example is an attempt to divide by zero. In Java, many runtime errors are represented as

exceptions that can be caught and dealt with (covered in Chapter 8).

68

Logical Errors

When your program has a logical error, it will compile and execute, but produces incorrect results. A logical error occurs when a value is

calculated incorrectly. A programmer must test the program

thoroughly, comparing the expected results to those that actually occur.

The process of finding and correcting defects in a program is called debugging.