Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Computer Systems and Networks: An Overview, Study notes of Design and Analysis of Algorithms

Operating SystemsComputer ArchitectureNetworkingProgramming Languages

An introduction to computer systems, focusing on computer processing, memory, input and output devices, and networks. It explains how programs and data are stored and executed, the role of the operating system, and the importance of networks in increasing productivity and sharing information. It also introduces the concept of Java programming and its relationship with the Web.

What you will learn

  • How does the operating system manage computer resources?
  • What are the different types of memory in a computer system?
  • What are the advantages of using networks in computer systems?
  • What is the role of main memory in a computer system?
  • What are the key pieces of a computer system and how do they interact?

Typology: Study notes

2021/2022

Uploaded on 08/01/2022

hal_s95
hal_s95 🇵🇭

4.4

(620)

8.6K documents

1 / 56

Toggle sidebar

Related documents


Partial preview of the text

Download Computer Systems and Networks: An Overview and more Study notes Design and Analysis of Algorithms in PDF only on Docsity! 2 CHAPTER 1 computer systems 1.0 introduction We begin our exploration of computer systems with an overview of computer processing, defining some fundamental terminology and showing how the key pieces of a computer system interact. basic computer processing A computer system is made up of hardware and software. The hardware compo- nents of a computer system are the physical, tangible pieces that support the com- puting effort. They include chips, boxes, wires, keyboards, speakers, disks, cables, plugs, printers, mice, monitors, and so on. If you can physically touch it and it can be considered part of a computer system, then it is computer hardware. The hardware components of a computer are essentially useless without instructions to tell them what to do. A program is a series of instructions that the hardware executes one after another. Software consists of programs and the data those programs use. Software is the intangible counterpart to the physical hardware components. Together they form a tool that we can use to solve problems. The key hardware components in a computer system are: ◗ central processing unit (CPU) ◗ input/output (I/O) devices ◗ main memory ◗ secondary memory devices Each of these hardware components is described in detail in the next section. For now, let’s simply examine their basic roles. The central processing unit (CPU) is the device that executes the individual commands of a program. Input/output (I/O) devices, such as the keyboard, mouse, and monitor, allow a human being to interact with the computer. Programs and data are held in storage devices called memory, which fall into two categories: main memory and secondary memory. Main memory is the stor- age device that holds the software while it is being processed by the CPU. Secondary memory devices store software in a relatively permanent manner. The most important secondary memory device of a typical computer system is the hard disk that resides inside the main computer box. A floppy disk is similar to a hard disk, but it cannot store nearly as much information as a hard disk. Floppy A computer system consists of hardware and software that work in concert to help us solve problems. ke y co nc ep t disks have the advantage of portability; they can be removed temporarily or moved from computer to computer as needed. Other portable secondary memory devices include zip disks and compact discs (CDs). Figure 1.1 shows how information moves among the basic hardware compo- nents of a computer. Suppose you have an executable program you wish to run. The program is stored on some secondary memory device, such as a hard disk.When you instruct the computer to execute your program, a copy of the program is brought in from secondary memory and stored in main memory. The CPU reads the individual program instructions from main memory. The CPU then executes the instructions one at a time until the program ends. The data that the instructions use, such as two numbers that will be added together, are also stored in main memory. They are either brought in from secondary memory or read from an input device such as the keyboard. During execution, the pro- gram may display information to an output device such as a monitor. The process of executing a program is fundamental to the operation of a com- puter. All computer systems basically work in the same way. software categories Software can be classified into many categories using various criteria. At this point we will simply differentiate between system programs and application programs. The operating system is the core software of a computer. It performs two important functions. First, it provides a user interface that allows the user to 1.0 introduction 3 figure 1.1 A simplified view of a computer system Hard disk Keyboard Main memory MonitorFloppy disk CPU To execute a program, the computer first copies the pro- gram from secondary memory to main memory. The CPU then reads the program instructions from main mem- ory, executing them one at a time until the program ends. key concept 6 CHAPTER 1 computer systems Digital technology breaks information into discrete pieces and represents those pieces as numbers. The music on a compact disc is stored digitally, as a series of numbers. Each number represents the voltage level of one specific instance of the recording. Many of these measurements are taken in a short period of time, per- haps 40,000 measurements every second. The number of measurements per sec- ond is called the sampling rate. If samples are taken often enough, the discrete voltage measurements can be used to generate a continuous analog signal that is “close enough” to the original. In most cases, the goal is to create a reproduction of the original signal that is good enough to satisfy the human ear. Figure 1.4 shows the sampling of an analog signal. When analog information is converted to a digital format by breaking it into pieces, we say it has been digitized. Because the changes that occur in a signal between samples are lost, the sampling rate must be sufficiently fast. Sampling is only one way to digitize information. For example, a sentence of text is stored on a computer as a series of numbers, where each num- ber represents a single character in the sentence. Every letter, digit, and punctua- tion symbol has been assigned a number. Even the space character is assigned a number. Consider the following sentence: Hi, Heather. figure 1.3 A sound wave and an electronic analog signal that represents the wave Sound wave Analog signal of the sound wave Digital computers store infor- mation by breaking it into pieces and representing each piece as a number. ke y co nc ep t 1.0 introduction 7 The characters of the sentence are represented as a series of 12 numbers, as shown in Fig. 1.5. When a character is repeated, such as the uppercase ‘H’, the same representation number is used. Note that the uppercase version of a letter is stored as a different number from the lowercase version, such as the ‘H’ and ‘h’ in the word Heather. They are considered separate and distinct characters. Modern electronic computers are digital. Every kind of information, including text, images, numbers, audio, video, and even program instructions, is broken into pieces. Each piece is represented as a number. The information is stored by storing those numbers. figure 1.4 Digitizing an analog signal by sampling Information can be lost between samples Analog signal Sampling process Sampled values 12 11 39 40 7 14 47 figure 1.5 Text is stored by mapping each character to a number 72 105 44 32 72 101 97 104 114116 101 46 H i , H e a t h e r . 8 CHAPTER 1 computer systems binary numbers A digital computer stores information as numbers, but those numbers are not stored as decimal values. All information in a computer is stored and managed as binary values. Unlike the decimal system, which has 10 digits (0 through 9), the binary number system has only two digits (0 and 1). A single binary digit is called a bit. All number systems work according to the same rules. The base value of a number system dictates how many digits we have to work with and indicates the place value of each digit in a number. The decimal number system is base 10, whereas the binary number system is base 2. Appendix B contains a detailed dis- cussion of number systems. Modern computers use binary numbers because the devices that store and move information are less expensive and more reliable if they have to represent only one of two possible values. Other than this char- acteristic, there is nothing special about the binary number system. Computers have been created that use other number systems to store information, but they aren’t as convenient. Some computer memory devices, such as hard drives, are magnetic in nature. Magnetic material can be polarized easily to one extreme or the other, but intermediate levels are difficult to distinguish. Therefore magnetic devices can be used to represent binary values quite efficiently—a magnetized area represents a binary 1 and a demagnetized area represents a binary 0. Other computer mem- ory devices are made up of tiny electrical circuits. These devices are easier to cre- ate and are less likely to fail if they have to switch between only two states. We’re better off reproducing millions of these simple devices than creating fewer, more complicated ones. Binary values and digital electronic signals go hand in hand. They improve our ability to transmit information reliably along a wire. As we’ve seen, analog signal has continuously varying voltage, but a digital signal is discrete, which means the voltage changes dramatically between one extreme (such as +5 volts) and the other (such as –5 volts). At any point, the voltage of a digital signal is considered to be either “high,” which represents a binary 1, or “low,” which represents a binary 0. Figure 1.6 compares these two types of signals. As a signal moves down a wire, it gets weaker and degrades due to environ- mental conditions. That is, the voltage levels of the original signal change slightly. The trouble with an analog signal is that as it fluctuates, it loses its original infor- mation. Since the information is directly analogous to the signal, any change in the signal changes the information. The changes in an analog signal cannot be Binary values are used to store all information in a computer because the devices that store and manipulate binary infor- mation are inexpensive and reliable. ke y co nc ep t computer architecture The architecture of a house defines its structure. Similarly, we use the term com- puter architecture to describe how the hardware components of a computer are put together. Figure 1.9 illustrates the basic architecture of a generic computer system. Information travels between components across a group of wires called a bus. The CPU and the main memory make up the core of a computer. As we men- tioned earlier, main memory stores programs and data that are in active use, and the CPU methodically executes program instructions one at a time. Suppose we have a program that computes the average of a list of numbers. The program and the numbers must reside in main memory while the program runs. The CPU reads one program instruction from main memory and executes it. If an instruction needs data, such as a number in the list, to perform its task, the CPU reads that information as well. This process repeats until the program ends. The average, when computed, is stored in main memory to await further processing or long-term storage in secondary memory. ■ 950 MHz Intel Pentium 4 processor ■ 512 MB RAM ■ 30 GB Hard Disk ■ CD-RW 24x/10x/40x ■ 17" Video Display with 1280 x 1024 resolution ■ 56 Kb/s modem 1.1 hardware components 11 figure 1.8 The hardware specification of a particular computer The core of a computer is made up of the CPU and the main memory. Main memory is used to store programs and data. The CPU executes a pro- gram’s instructions one at a time. key concept 12 CHAPTER 1 computer systems Almost all devices in a computer system other than the CPU and main mem- ory are called peripherals; they operate at the periphery, or outer edges, of the sys- tem (although they may be in the same box). Users don’t interact directly with the CPU or main memory. Although they form the essence of the machine, the CPU and main memory would not be useful without peripheral devices. Controllers are devices that coordinate the activities of specific peripherals. Every device has its own particular way of formatting and communicating data, and part of the controller’s role is to handle these idiosyncrasies and isolate them from the rest of the computer hardware. Furthermore, the controller often han- dles much of the actual transmission of information, allowing the CPU to focus on other activities. Input/output (I/O) devices and secondary memory devices are considered peripherals. Another category of peripherals includes data transfer devices, which allow information to be sent and received between computers. The computer specified in Fig. 1.8 includes a data transfer device called a modem, which allows information to be sent across a telephone line. The modem in the example can transfer data at a maximum rate of 56 kilobits (Kb) per second, or approximately 56,000 bits per second (bps). In some ways, secondary memory devices and data transfer devices can be thought of as I/O devices because they represent a source of information (input) figure 1.9 Basic computer architecture Other peripheral devices Main memory Central processing unit Controller Video controller Disk controller Controller Bus 1.1 hardware components 13 and a place to send information (output). For our discussion, however, we define I/O devices as those devices that allow the user to interact with the computer. input/output devices Let’s examine some I/O devices in more detail. The most common input devices are the keyboard and the mouse. Others include: ◗ bar code readers, such as the ones used at a grocery store checkout ◗ joysticks, often used for games and advanced graphical applications ◗ microphones, used by voice recognition systems that interpret simple voice commands ◗ virtual reality devices, such as gloves that interpret the movement of the user’s hand ◗ scanners, which convert text, photographs, and graphics into machine- readable form Monitors and printers are the most common output devices. Others include: ◗ plotters, which move pens across large sheets of paper (or vice versa) ◗ speakers, for audio output ◗ goggles, for virtual reality display Some devices can provide both input and output capabilities. A touch screen system can detect the user touching the screen at a particular place. Software can then use the screen to display text and graphics in response to the user’s touch. Touch screens are particularly useful in situations where the interface to the machine must be simple, such as at an information booth. The computer described in Fig. 1.8 includes a monitor with a 17-inch diago- nal display area. A picture is created by breaking it up into small pieces called pix- els, a term that stands for “picture elements.” The monitor can display a grid of 1280 by 1024 pixels. The last section of this chapter explores the representation of graphics in more detail. main memory and secondary memory Main memory is made up of a series of small, consecutive memory locations, as shown in Fig. 1.10. Associated with each memory location is a unique number called an address. 16 CHAPTER 1 computer systems Tapes are usually used only to store information when it is no longer used fre- quently, or to provide a backup copy of the information on a disk. Two other terms are used to describe memory devices: random access memory (RAM) and read-only memory (ROM). It’s important to understand these terms because they are used often, and their names can be misleading. The terms RAM and main memory are basically interchangeable. When contrasted with ROM, however, the term RAM seems to imply something it shouldn’t. Both RAM and ROM are direct (or random) access devices. RAM should probably be called read-write memory, since data can be both written to it and read from it. This fea- ture distinguishes it from ROM. After information is stored on ROM, it cannot be altered (as the term “read-only” implies). ROM chips are often embedded into the main circuit board of a computer and used to provide the preliminary instruc- tions needed when the computer is initially turned on. A CD-ROM is a portable secondary memory device. CD stands for compact disc. It is accurately called ROM because information is stored permanently when the CD is created and cannot be changed. Like its musical CD coun- terpart, a CD-ROM stores information in binary format. When the CD is initially created, a microscopic pit is pressed into the disc to repre- sent a binary 1, and the disc is left smooth to represent a binary 0. The bits are read by shining a low-intensity laser beam onto the spinning disc. The laser beam reflects strongly from a smooth area on the disc figure 1.12 A hard disk drive with multiple disks and read/write heads Disks Read/write head The surface of a CD has both smooth areas and small pits. A pit represents a binary 1 and a smooth area represents a binary 0. ke y co nc ep t 1.1 hardware components 17 but weakly from a pitted area. A sensor receiving the reflection determines whether each bit is a 1 or a 0 accordingly. A typical CD-ROM’s storage capacity is approximately 650 MB. Variations on basic CD technology have emerged quickly. It is now common for a home computer to be equipped with a CD-Recordable (CD-R) drive. A CD-R can be used to create a CD for music or for general computer storage. Once created, you can use a CD-R disc in a standard CD player, but you can’t change the information on a CD-R disc once it has been “burned.” Music CDs that you buy in a store are pressed from a mold, whereas CD-Rs are burned with a laser. A CD-Rewritable (CD-RW) disc can be erased and reused. They can be reused because the pits and flat surfaces of a normal CD are simu- lated on a CD-RW by coating the surface of the disc with a material that, when heated to one temperature becomes amorphous (and there- fore non-reflective) and when heated to a different temperature becomes crystalline (and therefore reflective). The CD-RW media doesn’t work in all players, but CD-Rewritable drives can create both CD-R and CD-RW discs. CDs were initially a popular format for music; they later evolved to be used as a general computer storage device. Similarly, the DVD format was originally cre- ated for video and is now making headway as a general format for computer data. DVD once stood for digital video disc or digital versatile disc, but now the acronym generally stands on its own. A DVD has a tighter format (more bits per square inch) than a CD and can therefore store much more information. It is likely that DVD-ROMs eventually will replace CD-ROMs completely because there is a compatible migration path, meaning that a DVD drive can read a CD- ROM. There are currently six different formats for recordable DVDs; some of these are essentially in competition with each other. The market will decide which formats will dominate. The speed of a CD drive is expressed in multiples of x, which represents a data transfer speed of 153,600 bytes of data per second. The CD-RW drive described in Fig. 1.8 is characterized as having 24x/10x/40x maximum speed, which means it can write data onto CD-R discs at 24x, it can write data onto CD-RW discs at 10x, and it reads data from a disc at 40x. The capacity of storage devices changes continually as technology improves. A general rule in the computer industry suggests that storage capacity approx- imately doubles every 18 months. However, this progress eventually will slow down as capacities approach absolute physical limits. A rewritable CD simulates the pits and smooth areas of a regular CD using a coating that can be made amorphous or crystalline as needed. key concept 18 CHAPTER 1 computer systems the central processing unit The central processing unit (CPU) interacts with main memory to perform all fundamental processing in a computer. The CPU interprets and executes instruc- tions, one after another, in a continuous cycle. It is made up of three important components, as shown in Fig. 1.13. The control unit coordinates the processing steps, the registers provide a small amount of storage space in the CPU itself, and the arithmetic/logic unit performs calculations and makes decisions. The control unit coordinates the transfer of data and instructions between main memory and the registers in the CPU. It also coordinates the execution of the circuitry in the arithmetic/logic unit to perform operations on data stored in particular registers. In most CPUs, some registers are reserved for special purposes. For example, the instruction register holds the current instruction being executed. The program counter is a register that holds the address of the next instruction to be executed. In addition to these and other special-purpose registers, the CPU also contains a set of general-purpose registers that are used for temporary storage of values as needed. The concept of storing both program instructions and data together in main memory is the underlying principle of the von Neumann architecture of computer design, named after John von Neumann, who first advanced this programming concept in 1945. These computers continually follow the fetch-decode-execute cycle depicted in Fig. 1.14. An instruction is fetched from main memory at the address stored in the program counter and is put into the instruction register. The figure 1.13 CPU components and main memory Bus CPU Registers Arithmetic/logic unit Main memory Control unit 1.2 networks 21 has its own network address, which uniquely identifies it. These addresses are similar in concept to the addresses in main memory except that they identify indi- vidual computers on a network instead of individual memory locations inside a single computer. A message is sent across the line from one computer to another by specifying the network address of the computer for which it is intended. Sharing a communication line is cost effective and makes adding new computers to the network relatively easy. However, a shared line introduces delays. The computers on the network cannot use the com- munication line at the same time. They have to take turns sending information, which means they have to wait when the line is busy. One technique to improve network delays is to divide large mes- sages into segments, called packets, and then send the individual packets across the network intermixed with pieces of other messages sent by other users. The packets are collected at the destination and reassembled into the original message. This situation is similar to a group of people using a conveyor belt to move a set of boxes from one place to another. If only one person were allowed to use the conveyor belt at a time, and that person had a large number of boxes to move, the others would be waiting a long time before they could use it. By taking turns, each person can put one box on at a time, and they all can get their work done. It’s not as fast as having a conveyor belt of your own, but it’s not as slow as hav- ing to wait until everyone else is finished. local-area networks and wide-area networks A local-area network (LAN) is designed to span short distances and connect a rel- atively small number of computers. Usually a LAN connects the machines in only figure 1.16 Point-to-point connections Sharing a communication line creates delays, but it is cost effective and simplifies adding new computers to the network. key concept 22 CHAPTER 1 computer systems one building or in a single room. LANs are convenient to install and manage and are highly reliable. As computers became increasingly small and versatile, LANs became an inexpensive way to share information throughout an organ- ization. However, having a LAN is like having a telephone system that allows you to call only the people in your own town. We need to be able to share information across longer distances. A wide-area network (WAN) connects two or more LANs, often across long distances. Usually one computer on each LAN is dedicated to handling the communication across a WAN. This technique relieves the other computers in a LAN from having to perform the details of long-distance communication. Figure 1.17 shows several LANs connected into a WAN. The LANs connected by a WAN are often owned by different companies or organizations, and might even be located in different countries. The impact of networks on computer systems has been dramatic. Computing resources can now be shared among many users, and computer-based communi- cation across the entire world is now possible. In fact, the use of networks is now so pervasive that some computers require network resources in order to operate. figure 1.17 LANs connected into a WAN LAN Long-distance connection One computer in a LAN A local-area network (LAN) is an inexpensive way to share information and resources throughout an organization. ke y co nc ep t 1.2 networks 23 the Internet Throughout the 1970s, a United States government organization called the Advanced Research Projects Agency (ARPA) funded several projects to explore network technology. One result of these efforts was the ARPANET, a WAN that eventually became known as the Internet. The Internet is a network of networks. The term Internet comes from the WAN concept of internetworking—connecting many smaller networks together. From the mid 1980s through the present day, the Internet has grown incredi- bly. In 1983, there were fewer than 600 computers connected to the Internet. By the year 2000, that number had reached over 10 million. As more and more com- puters connect to the Internet, the task of keeping up with the larger number of users and heavier traffic has been difficult. New technologies have replaced the ARPANET several times since the initial development, each time providing more capacity and faster processing. 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 con- form to a set of protocols called TCP/IP (pronounced by spelling out the letters, T-C-P-I-P). TCP stands for Transmission Control Protocol, and IP stands for Internet Protocol. The IP software defines how information is formatted and transferred from the source to the destination. The TCP software handles problems such as pieces of information arriving out of their original order or information getting lost, which can hap- pen if too much information converges at one location at the same time. Every computer connected to the Internet has an IP address that uniquely iden- tifies it among all other computers on the Internet. An example of an IP address is 204.192.116.2. Fortunately, the users of the Internet rarely have to deal with IP addresses. The Internet allows each computer to be given a name. Like IP addresses, the names must be unique. The Internet name of a computer is often referred to as its Internet address. Two examples of Internet addresses are spencer.villanova.edu and kant.gestalt-llc.com. The first part of an Internet address is the local name of a specific computer. The rest of the address is the domain name, which indicates the organization to which the computer belongs. For example, villanova.edu is the domain name for the network of computers at Villanova University, and spencer is the name of a particular computer on that campus. Because the domain names are unique, many organizations can have a computer The Internet is a wide-area network (WAN) that spans the globe. key concept TCP/IP is the set of software protocols that govern the movement of messages across the Internet. key concept Every computer connected to the Internet has an IP address that uniquely identifies it. key concept 26 CHAPTER 1 computer systems interest. If no file name is given, as is the case with the Yahoo URL, browsers make a default selection (such as index.html). Let’s look at another example URL: http://www.gestalt-llc.com/vision.html In this URL, the protocol is http, which stands for HyperText Transfer Protocol. The machine referenced is www (a typical reference to a Web server), found at domain gestalt-llc.com. Finally, vision.html is a file to be transferred to the browser for viewing. Many other forms for URLs exist, but this form is the most common. figure 1.18 Netscape Navigator browsing an HTML document (used with permission of ACM) the Internet vs. the World Wide Web The terms Internet and World Wide Web are sometimes used interchangeably, but there are important differences between the two. The Internet makes it possible to communicate via computers around the world. The Web makes that commu- nication a straightforward and enjoyable activity. The Web is essentially a distributed information service and is based on a set of software applications. It is not a network. Although it is used effectively with the Internet, it is not inherently bound to it. The Web can be used on a LAN that is not connected to any other network or even on a single machine to display HTML documents. 1.3 programming The Java programming language was another important evolutionary step that allowed software to be easily exchanged and executed via the Web. The rest of this book explores the process of creating programs using Java. This section dis- cusses the purpose of programming in general and introduces the Java program- ming language. problem solving The purpose of writing a program is to solve a problem. Problem solving, in gen- eral, 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. Although this approach applies to any kind of problem solving, it works particularly well when developing software. We refine this series 1.3 programming 27 The purpose of writing a pro- gram is to solve a problem. key concept 2 CHAPTER 1 computer systems 1.0 introduction We begin our exploration of computer systems with an overview of computer processing, defining some fundamental terminology and showing how the key pieces of a computer system interact. basic computer processing A computer system is made up of hardware and software. The hardware compo- nents of a computer system are the physical, tangible pieces that support the com- puting effort. They include chips, boxes, wires, keyboards, speakers, disks, cables, plugs, printers, mice, monitors, and so on. If you can physically touch it and it can be considered part of a computer system, then it is computer hardware. The hardware components of a computer are essentially useless without instructions to tell them what to do. A program is a series of instructions that the hardware executes one after another. Software consists of programs and the data those programs use. Software is the intangible counterpart to the physical hardware components. Together they form a tool that we can use to solve problems. The key hardware components in a computer system are: ◗ central processing unit (CPU) ◗ input/output (I/O) devices ◗ main memory ◗ secondary memory devices Each of these hardware components is described in detail in the next section. For now, let’s simply examine their basic roles. The central processing unit (CPU) is the device that executes the individual commands of a program. Input/output (I/O) devices, such as the keyboard, mouse, and monitor, allow a human being to interact with the computer. Programs and data are held in storage devices called memory, which fall into two categories: main memory and secondary memory. Main memory is the stor- age device that holds the software while it is being processed by the CPU. Secondary memory devices store software in a relatively permanent manner. The most important secondary memory device of a typical computer system is the hard disk that resides inside the main computer box. A floppy disk is similar to a hard disk, but it cannot store nearly as much information as a hard disk. Floppy A computer system consists of hardware and software that work in concert to help us solve problems. ke y co nc ep t disks have the advantage of portability; they can be removed temporarily or moved from computer to computer as needed. Other portable secondary memory devices include zip disks and compact discs (CDs). Figure 1.1 shows how information moves among the basic hardware compo- nents of a computer. Suppose you have an executable program you wish to run. The program is stored on some secondary memory device, such as a hard disk.When you instruct the computer to execute your program, a copy of the program is brought in from secondary memory and stored in main memory. The CPU reads the individual program instructions from main memory. The CPU then executes the instructions one at a time until the program ends. The data that the instructions use, such as two numbers that will be added together, are also stored in main memory. They are either brought in from secondary memory or read from an input device such as the keyboard. During execution, the pro- gram may display information to an output device such as a monitor. The process of executing a program is fundamental to the operation of a com- puter. All computer systems basically work in the same way. software categories Software can be classified into many categories using various criteria. At this point we will simply differentiate between system programs and application programs. The operating system is the core software of a computer. It performs two important functions. First, it provides a user interface that allows the user to 1.0 introduction 3 figure 1.1 A simplified view of a computer system Hard disk Keyboard Main memory MonitorFloppy disk CPU To execute a program, the computer first copies the pro- gram from secondary memory to main memory. The CPU then reads the program instructions from main mem- ory, executing them one at a time until the program ends. key concept 6 CHAPTER 1 computer systems Digital technology breaks information into discrete pieces and represents those pieces as numbers. The music on a compact disc is stored digitally, as a series of numbers. Each number represents the voltage level of one specific instance of the recording. Many of these measurements are taken in a short period of time, per- haps 40,000 measurements every second. The number of measurements per sec- ond is called the sampling rate. If samples are taken often enough, the discrete voltage measurements can be used to generate a continuous analog signal that is “close enough” to the original. In most cases, the goal is to create a reproduction of the original signal that is good enough to satisfy the human ear. Figure 1.4 shows the sampling of an analog signal. When analog information is converted to a digital format by breaking it into pieces, we say it has been digitized. Because the changes that occur in a signal between samples are lost, the sampling rate must be sufficiently fast. Sampling is only one way to digitize information. For example, a sentence of text is stored on a computer as a series of numbers, where each num- ber represents a single character in the sentence. Every letter, digit, and punctua- tion symbol has been assigned a number. Even the space character is assigned a number. Consider the following sentence: Hi, Heather. figure 1.3 A sound wave and an electronic analog signal that represents the wave Sound wave Analog signal of the sound wave Digital computers store infor- mation by breaking it into pieces and representing each piece as a number. ke y co nc ep t 1.0 introduction 7 The characters of the sentence are represented as a series of 12 numbers, as shown in Fig. 1.5. When a character is repeated, such as the uppercase ‘H’, the same representation number is used. Note that the uppercase version of a letter is stored as a different number from the lowercase version, such as the ‘H’ and ‘h’ in the word Heather. They are considered separate and distinct characters. Modern electronic computers are digital. Every kind of information, including text, images, numbers, audio, video, and even program instructions, is broken into pieces. Each piece is represented as a number. The information is stored by storing those numbers. figure 1.4 Digitizing an analog signal by sampling Information can be lost between samples Analog signal Sampling process Sampled values 12 11 39 40 7 14 47 figure 1.5 Text is stored by mapping each character to a number 72 105 44 32 72 101 97 104 114116 101 46 H i , H e a t h e r . 8 CHAPTER 1 computer systems binary numbers A digital computer stores information as numbers, but those numbers are not stored as decimal values. All information in a computer is stored and managed as binary values. Unlike the decimal system, which has 10 digits (0 through 9), the binary number system has only two digits (0 and 1). A single binary digit is called a bit. All number systems work according to the same rules. The base value of a number system dictates how many digits we have to work with and indicates the place value of each digit in a number. The decimal number system is base 10, whereas the binary number system is base 2. Appendix B contains a detailed dis- cussion of number systems. Modern computers use binary numbers because the devices that store and move information are less expensive and more reliable if they have to represent only one of two possible values. Other than this char- acteristic, there is nothing special about the binary number system. Computers have been created that use other number systems to store information, but they aren’t as convenient. Some computer memory devices, such as hard drives, are magnetic in nature. Magnetic material can be polarized easily to one extreme or the other, but intermediate levels are difficult to distinguish. Therefore magnetic devices can be used to represent binary values quite efficiently—a magnetized area represents a binary 1 and a demagnetized area represents a binary 0. Other computer mem- ory devices are made up of tiny electrical circuits. These devices are easier to cre- ate and are less likely to fail if they have to switch between only two states. We’re better off reproducing millions of these simple devices than creating fewer, more complicated ones. Binary values and digital electronic signals go hand in hand. They improve our ability to transmit information reliably along a wire. As we’ve seen, analog signal has continuously varying voltage, but a digital signal is discrete, which means the voltage changes dramatically between one extreme (such as +5 volts) and the other (such as –5 volts). At any point, the voltage of a digital signal is considered to be either “high,” which represents a binary 1, or “low,” which represents a binary 0. Figure 1.6 compares these two types of signals. As a signal moves down a wire, it gets weaker and degrades due to environ- mental conditions. That is, the voltage levels of the original signal change slightly. The trouble with an analog signal is that as it fluctuates, it loses its original infor- mation. Since the information is directly analogous to the signal, any change in the signal changes the information. The changes in an analog signal cannot be Binary values are used to store all information in a computer because the devices that store and manipulate binary infor- mation are inexpensive and reliable. ke y co nc ep t computer architecture The architecture of a house defines its structure. Similarly, we use the term com- puter architecture to describe how the hardware components of a computer are put together. Figure 1.9 illustrates the basic architecture of a generic computer system. Information travels between components across a group of wires called a bus. The CPU and the main memory make up the core of a computer. As we men- tioned earlier, main memory stores programs and data that are in active use, and the CPU methodically executes program instructions one at a time. Suppose we have a program that computes the average of a list of numbers. The program and the numbers must reside in main memory while the program runs. The CPU reads one program instruction from main memory and executes it. If an instruction needs data, such as a number in the list, to perform its task, the CPU reads that information as well. This process repeats until the program ends. The average, when computed, is stored in main memory to await further processing or long-term storage in secondary memory. ■ 950 MHz Intel Pentium 4 processor ■ 512 MB RAM ■ 30 GB Hard Disk ■ CD-RW 24x/10x/40x ■ 17" Video Display with 1280 x 1024 resolution ■ 56 Kb/s modem 1.1 hardware components 11 figure 1.8 The hardware specification of a particular computer The core of a computer is made up of the CPU and the main memory. Main memory is used to store programs and data. The CPU executes a pro- gram’s instructions one at a time. key concept 12 CHAPTER 1 computer systems Almost all devices in a computer system other than the CPU and main mem- ory are called peripherals; they operate at the periphery, or outer edges, of the sys- tem (although they may be in the same box). Users don’t interact directly with the CPU or main memory. Although they form the essence of the machine, the CPU and main memory would not be useful without peripheral devices. Controllers are devices that coordinate the activities of specific peripherals. Every device has its own particular way of formatting and communicating data, and part of the controller’s role is to handle these idiosyncrasies and isolate them from the rest of the computer hardware. Furthermore, the controller often han- dles much of the actual transmission of information, allowing the CPU to focus on other activities. Input/output (I/O) devices and secondary memory devices are considered peripherals. Another category of peripherals includes data transfer devices, which allow information to be sent and received between computers. The computer specified in Fig. 1.8 includes a data transfer device called a modem, which allows information to be sent across a telephone line. The modem in the example can transfer data at a maximum rate of 56 kilobits (Kb) per second, or approximately 56,000 bits per second (bps). In some ways, secondary memory devices and data transfer devices can be thought of as I/O devices because they represent a source of information (input) figure 1.9 Basic computer architecture Other peripheral devices Main memory Central processing unit Controller Video controller Disk controller Controller Bus 1.1 hardware components 13 and a place to send information (output). For our discussion, however, we define I/O devices as those devices that allow the user to interact with the computer. input/output devices Let’s examine some I/O devices in more detail. The most common input devices are the keyboard and the mouse. Others include: ◗ bar code readers, such as the ones used at a grocery store checkout ◗ joysticks, often used for games and advanced graphical applications ◗ microphones, used by voice recognition systems that interpret simple voice commands ◗ virtual reality devices, such as gloves that interpret the movement of the user’s hand ◗ scanners, which convert text, photographs, and graphics into machine- readable form Monitors and printers are the most common output devices. Others include: ◗ plotters, which move pens across large sheets of paper (or vice versa) ◗ speakers, for audio output ◗ goggles, for virtual reality display Some devices can provide both input and output capabilities. A touch screen system can detect the user touching the screen at a particular place. Software can then use the screen to display text and graphics in response to the user’s touch. Touch screens are particularly useful in situations where the interface to the machine must be simple, such as at an information booth. The computer described in Fig. 1.8 includes a monitor with a 17-inch diago- nal display area. A picture is created by breaking it up into small pieces called pix- els, a term that stands for “picture elements.” The monitor can display a grid of 1280 by 1024 pixels. The last section of this chapter explores the representation of graphics in more detail. main memory and secondary memory Main memory is made up of a series of small, consecutive memory locations, as shown in Fig. 1.10. Associated with each memory location is a unique number called an address. 16 CHAPTER 1 computer systems Tapes are usually used only to store information when it is no longer used fre- quently, or to provide a backup copy of the information on a disk. Two other terms are used to describe memory devices: random access memory (RAM) and read-only memory (ROM). It’s important to understand these terms because they are used often, and their names can be misleading. The terms RAM and main memory are basically interchangeable. When contrasted with ROM, however, the term RAM seems to imply something it shouldn’t. Both RAM and ROM are direct (or random) access devices. RAM should probably be called read-write memory, since data can be both written to it and read from it. This fea- ture distinguishes it from ROM. After information is stored on ROM, it cannot be altered (as the term “read-only” implies). ROM chips are often embedded into the main circuit board of a computer and used to provide the preliminary instruc- tions needed when the computer is initially turned on. A CD-ROM is a portable secondary memory device. CD stands for compact disc. It is accurately called ROM because information is stored permanently when the CD is created and cannot be changed. Like its musical CD coun- terpart, a CD-ROM stores information in binary format. When the CD is initially created, a microscopic pit is pressed into the disc to repre- sent a binary 1, and the disc is left smooth to represent a binary 0. The bits are read by shining a low-intensity laser beam onto the spinning disc. The laser beam reflects strongly from a smooth area on the disc figure 1.12 A hard disk drive with multiple disks and read/write heads Disks Read/write head The surface of a CD has both smooth areas and small pits. A pit represents a binary 1 and a smooth area represents a binary 0. ke y co nc ep t 1.1 hardware components 17 but weakly from a pitted area. A sensor receiving the reflection determines whether each bit is a 1 or a 0 accordingly. A typical CD-ROM’s storage capacity is approximately 650 MB. Variations on basic CD technology have emerged quickly. It is now common for a home computer to be equipped with a CD-Recordable (CD-R) drive. A CD-R can be used to create a CD for music or for general computer storage. Once created, you can use a CD-R disc in a standard CD player, but you can’t change the information on a CD-R disc once it has been “burned.” Music CDs that you buy in a store are pressed from a mold, whereas CD-Rs are burned with a laser. A CD-Rewritable (CD-RW) disc can be erased and reused. They can be reused because the pits and flat surfaces of a normal CD are simu- lated on a CD-RW by coating the surface of the disc with a material that, when heated to one temperature becomes amorphous (and there- fore non-reflective) and when heated to a different temperature becomes crystalline (and therefore reflective). The CD-RW media doesn’t work in all players, but CD-Rewritable drives can create both CD-R and CD-RW discs. CDs were initially a popular format for music; they later evolved to be used as a general computer storage device. Similarly, the DVD format was originally cre- ated for video and is now making headway as a general format for computer data. DVD once stood for digital video disc or digital versatile disc, but now the acronym generally stands on its own. A DVD has a tighter format (more bits per square inch) than a CD and can therefore store much more information. It is likely that DVD-ROMs eventually will replace CD-ROMs completely because there is a compatible migration path, meaning that a DVD drive can read a CD- ROM. There are currently six different formats for recordable DVDs; some of these are essentially in competition with each other. The market will decide which formats will dominate. The speed of a CD drive is expressed in multiples of x, which represents a data transfer speed of 153,600 bytes of data per second. The CD-RW drive described in Fig. 1.8 is characterized as having 24x/10x/40x maximum speed, which means it can write data onto CD-R discs at 24x, it can write data onto CD-RW discs at 10x, and it reads data from a disc at 40x. The capacity of storage devices changes continually as technology improves. A general rule in the computer industry suggests that storage capacity approx- imately doubles every 18 months. However, this progress eventually will slow down as capacities approach absolute physical limits. A rewritable CD simulates the pits and smooth areas of a regular CD using a coating that can be made amorphous or crystalline as needed. key concept 18 CHAPTER 1 computer systems the central processing unit The central processing unit (CPU) interacts with main memory to perform all fundamental processing in a computer. The CPU interprets and executes instruc- tions, one after another, in a continuous cycle. It is made up of three important components, as shown in Fig. 1.13. The control unit coordinates the processing steps, the registers provide a small amount of storage space in the CPU itself, and the arithmetic/logic unit performs calculations and makes decisions. The control unit coordinates the transfer of data and instructions between main memory and the registers in the CPU. It also coordinates the execution of the circuitry in the arithmetic/logic unit to perform operations on data stored in particular registers. In most CPUs, some registers are reserved for special purposes. For example, the instruction register holds the current instruction being executed. The program counter is a register that holds the address of the next instruction to be executed. In addition to these and other special-purpose registers, the CPU also contains a set of general-purpose registers that are used for temporary storage of values as needed. The concept of storing both program instructions and data together in main memory is the underlying principle of the von Neumann architecture of computer design, named after John von Neumann, who first advanced this programming concept in 1945. These computers continually follow the fetch-decode-execute cycle depicted in Fig. 1.14. An instruction is fetched from main memory at the address stored in the program counter and is put into the instruction register. The figure 1.13 CPU components and main memory Bus CPU Registers Arithmetic/logic unit Main memory Control unit 1.2 networks 21 has its own network address, which uniquely identifies it. These addresses are similar in concept to the addresses in main memory except that they identify indi- vidual computers on a network instead of individual memory locations inside a single computer. A message is sent across the line from one computer to another by specifying the network address of the computer for which it is intended. Sharing a communication line is cost effective and makes adding new computers to the network relatively easy. However, a shared line introduces delays. The computers on the network cannot use the com- munication line at the same time. They have to take turns sending information, which means they have to wait when the line is busy. One technique to improve network delays is to divide large mes- sages into segments, called packets, and then send the individual packets across the network intermixed with pieces of other messages sent by other users. The packets are collected at the destination and reassembled into the original message. This situation is similar to a group of people using a conveyor belt to move a set of boxes from one place to another. If only one person were allowed to use the conveyor belt at a time, and that person had a large number of boxes to move, the others would be waiting a long time before they could use it. By taking turns, each person can put one box on at a time, and they all can get their work done. It’s not as fast as having a conveyor belt of your own, but it’s not as slow as hav- ing to wait until everyone else is finished. local-area networks and wide-area networks A local-area network (LAN) is designed to span short distances and connect a rel- atively small number of computers. Usually a LAN connects the machines in only figure 1.16 Point-to-point connections Sharing a communication line creates delays, but it is cost effective and simplifies adding new computers to the network. key concept 22 CHAPTER 1 computer systems one building or in a single room. LANs are convenient to install and manage and are highly reliable. As computers became increasingly small and versatile, LANs became an inexpensive way to share information throughout an organ- ization. However, having a LAN is like having a telephone system that allows you to call only the people in your own town. We need to be able to share information across longer distances. A wide-area network (WAN) connects two or more LANs, often across long distances. Usually one computer on each LAN is dedicated to handling the communication across a WAN. This technique relieves the other computers in a LAN from having to perform the details of long-distance communication. Figure 1.17 shows several LANs connected into a WAN. The LANs connected by a WAN are often owned by different companies or organizations, and might even be located in different countries. The impact of networks on computer systems has been dramatic. Computing resources can now be shared among many users, and computer-based communi- cation across the entire world is now possible. In fact, the use of networks is now so pervasive that some computers require network resources in order to operate. figure 1.17 LANs connected into a WAN LAN Long-distance connection One computer in a LAN A local-area network (LAN) is an inexpensive way to share information and resources throughout an organization. ke y co nc ep t 1.2 networks 23 the Internet Throughout the 1970s, a United States government organization called the Advanced Research Projects Agency (ARPA) funded several projects to explore network technology. One result of these efforts was the ARPANET, a WAN that eventually became known as the Internet. The Internet is a network of networks. The term Internet comes from the WAN concept of internetworking—connecting many smaller networks together. From the mid 1980s through the present day, the Internet has grown incredi- bly. In 1983, there were fewer than 600 computers connected to the Internet. By the year 2000, that number had reached over 10 million. As more and more com- puters connect to the Internet, the task of keeping up with the larger number of users and heavier traffic has been difficult. New technologies have replaced the ARPANET several times since the initial development, each time providing more capacity and faster processing. 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 con- form to a set of protocols called TCP/IP (pronounced by spelling out the letters, T-C-P-I-P). TCP stands for Transmission Control Protocol, and IP stands for Internet Protocol. The IP software defines how information is formatted and transferred from the source to the destination. The TCP software handles problems such as pieces of information arriving out of their original order or information getting lost, which can hap- pen if too much information converges at one location at the same time. Every computer connected to the Internet has an IP address that uniquely iden- tifies it among all other computers on the Internet. An example of an IP address is 204.192.116.2. Fortunately, the users of the Internet rarely have to deal with IP addresses. The Internet allows each computer to be given a name. Like IP addresses, the names must be unique. The Internet name of a computer is often referred to as its Internet address. Two examples of Internet addresses are spencer.villanova.edu and kant.gestalt-llc.com. The first part of an Internet address is the local name of a specific computer. The rest of the address is the domain name, which indicates the organization to which the computer belongs. For example, villanova.edu is the domain name for the network of computers at Villanova University, and spencer is the name of a particular computer on that campus. Because the domain names are unique, many organizations can have a computer The Internet is a wide-area network (WAN) that spans the globe. key concept TCP/IP is the set of software protocols that govern the movement of messages across the Internet. key concept Every computer connected to the Internet has an IP address that uniquely identifies it. key concept 26 CHAPTER 1 computer systems interest. If no file name is given, as is the case with the Yahoo URL, browsers make a default selection (such as index.html). Let’s look at another example URL: http://www.gestalt-llc.com/vision.html In this URL, the protocol is http, which stands for HyperText Transfer Protocol. The machine referenced is www (a typical reference to a Web server), found at domain gestalt-llc.com. Finally, vision.html is a file to be transferred to the browser for viewing. Many other forms for URLs exist, but this form is the most common. figure 1.18 Netscape Navigator browsing an HTML document (used with permission of ACM) the Internet vs. the World Wide Web The terms Internet and World Wide Web are sometimes used interchangeably, but there are important differences between the two. The Internet makes it possible to communicate via computers around the world. The Web makes that commu- nication a straightforward and enjoyable activity. The Web is essentially a distributed information service and is based on a set of software applications. It is not a network. Although it is used effectively with the Internet, it is not inherently bound to it. The Web can be used on a LAN that is not connected to any other network or even on a single machine to display HTML documents. 1.3 programming The Java programming language was another important evolutionary step that allowed software to be easily exchanged and executed via the Web. The rest of this book explores the process of creating programs using Java. This section dis- cusses the purpose of programming in general and introduces the Java program- ming language. problem solving The purpose of writing a program is to solve a problem. Problem solving, in gen- eral, 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. Although this approach applies to any kind of problem solving, it works particularly well when developing software. We refine this series 1.3 programming 27 The purpose of writing a pro- gram is to solve a problem. key concept 28 CHAPTER 1 computer systems of activities and apply it to writing programs at various points throughout this text. The first step, understanding the problem, may sound obvious, but a lack of attention to this step has been the cause of many misguided efforts. If we attempt to solve a problem we don’t completely understand, we often end up solving the wrong problem or at least going off on improper tangents. We must understand the needs of the people who will use the solution. These needs often include sub- tle nuances that will affect our overall approach to the solution. After we thoroughly understand the problem, we then break the problem into manageable pieces and design a solution. These steps go hand in hand. A solution to any problem can rarely be expressed as one big activity. Instead, it is a series of small cooperating tasks that interact to perform a larger task. When develop- ing software, we don’t write one big program. We design separate pieces that are responsible for certain parts of the solution, subsequently integrating them with the other parts. Our first inclination toward a solution may not be the best one. We must always consider alternatives and refine the solution as necessary. The earlier we consider alternatives, the easier it is to modify our approach. Implementing the solution is the act of taking the design and putting it in a usable form. When developing a software solution to a problem, the implemen- tation stage is the process of actually writing the program. Too often program- ming is thought of as writing code. But in most cases, the final implementation of the solution is one of the last and easiest steps. The act of designing the program should be more interesting and creative than the process of implementing the design in a particular programming language. Finally, we test our solution to find any errors that exist so that we can fix them and improve the quality of the software. Testing efforts attempt to verify that the program correctly represents the design, which in turn provides a solu- tion to the problem. Throughout this text we explore programming techniques that allow us to ele- gantly design and implement solutions to problems. Although we will often delve into these specific techniques in detail, we should not forget that they are just tools to help us solve problems. The first solution we design to solve a problem may not be the best one. ke y co nc ep t 1.3 programming 31 The first few lines of the program are comments, which start with the // sym- bols and continue to the end of the line. Comments don’t affect what the program does but are included to make the program easier to understand by humans. Programmers can and should include comments as needed throughout a program to clearly identify the purpose of the program and describe any special processing. Any written comments or docu- ments, including a user’s guide and technical references, are called documentation. Comments included in a program are called inline documentation. The rest of the program is a class definition. This class is called Lincoln, though we could have named it just about anything we wished. The class defini- tion runs from the first opening brace ({) to the final closing brace (}) on the last line of the program. All Java programs are defined using class definitions. Inside the class definition are some more comments describing the purpose of the main method, which is defined directly below the comments. A method is a group of programming statements that are given a name. In this case, the name of the method is main and it contains only two programming statements. Like a class definition, a method is also delimited by braces. All Java applications have a main method, which is where processing begins. Each programming statement in the main method is executed, one at a time in order, until the end of the method is reached. Then the program ends, or termi- nates. The main method definition in a Java program is always preceded by the words public, static, and void, which we examine later in the text. The use of String and args does not come into play in this particu- lar program. We describe these later also. The two lines of code in the main method invoke another method called println (pronounced print line). We invoke, or call, a method when we want it 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 (“). When the program is executed, it calls the println method to print the first statement, calls it again to print the second statement, and then, because that is the last line in the program, the program terminates. The code executed when the println method is invoked is not defined in this program. The println method is part of the System.out object, which we explore in more detail in Chapter 2. Comments do not affect a pro- gram’s processing; instead, they serve to facilitate human comprehension. key concept The main method must always be defined using the words public, static, and void. key concept 32 CHAPTER 1 computer systems comments Let’s examine comments in more detail. Comments are the only language feature that allow programmers to compose and communicate their thoughts independ- ent of the code. Comments should provide insight into the programmer’s original intent. A program is often used for many years, and often many modifications are made to it over time. The original programmer often will not remember the details of a particular program when, at some point in the future, modifications are required. Furthermore, the original programmer is not always available to make the changes; thus, someone completely unfamiliar with the program will need to understand it. Good documentation is therefore essential. As far as the Java programming language is concerned, comments can be writ- ten using any content whatsoever. Comments are ignored by the computer; they do not affect how the program executes. The comments in the Lincoln program represent one of two types of com- ments allowed in Java. The comments in Lincoln take the following form: // This is a comment. This type of comment begins with a double slash (//) and continues to the end of the line. You cannot have any characters between the two slashes. The com- puter ignores any text after the double slash and to the end of the line. A com- ment can follow code on the same line to document that particular line, as in the following example: System.out.println (“Monthly Report”); // always use this title The second form a Java comment may have is: /* This is another comment. */ This comment type does not use the end of a line to indicate the end of the com- ment. Anything between the initiating slash-asterisk (/*) and the terminating asterisk-slash (*/) is part of the comment, including the invisible newline charac- ter that represents the end of a line. Therefore, this type of comment can extend over multiple lines. No space can be between the slash and the asterisk. If there is a second asterisk following the /* at the beginning of a comment, the content of the comment can be used to automatically generate external doc- umentation about your program using a tool called javadoc. (We do not discuss this feature in this book, but we do include a description and examples of this process on the book’s Web site. Throughout the book, we highlight additional information and examples that you can find on the Web site.) 1.3 programming 33 The two basic comment types can be used to create various documentation styles, such as: // This is a comment on a single line. //------------------------------------------------------------ // Some comments such as those above methods or classes // deserve to be blocked off to focus special // attention on a particular aspect of your code. Note // that each of these lines is technically a separate comment. //------------------------------------------------------------ /* This is one comment that spans several lines. */ Programmers often concentrate so much on writing code that they focus too little on documentation. You should develop good commenting practices and fol- low them habitually. Comments should be well written, often in complete sen- tences. They should not belabor the obvious but should provide appropriate insight into the intent of the code. The following examples are not good com- ments: System.out.println (“hello”); // prints hello System.out.println (“test”); // change this later The first comment paraphrases the obvious purpose of the line and does not add any value to the statement. It is better to have no comment than a useless one. The second comment is ambiguous. What should be changed later? When is later? Why should it be changed? It is considered good programming style to use comments in a con- sistent way throughout an entire program. Appendix G presents guide- lines for good programming practices and includes specific techniques for documenting programs. The Web site supporting this text describes how you can generate automatic program documentation using a special form of Java comments and a soft- ware tool called javadoc. Inline documentation should provide insight into your code. It should not be ambigu- ous or belabor the obvious. key concept web bonus 36 CHAPTER 1 computer systems identifiers. Therefore total, Total, ToTaL, and TOTAL are all different identi- fiers. As you can imagine, it is not a good idea to use multiple identifiers that dif- fer only in their case because they can be easily confused. Although the Java language doesn’t require it, using a consistent case format for each kind of identifier makes your identifiers easier to under- stand. For example, we use title case (uppercase for the first letter of each word) for class names. That is a Java convention, although it does not technically have to be followed. Throughout the text, we describe the preferred case style for each type of identifier as they are encoun- tered. Appendix G presents various guidelines for naming identifiers. While an identifier can be of any length, you should choose your names care- fully. They should be descriptive but not verbose. You should avoid meaning- less names such as a or x. An exception to this rule can be made if the short name is actually descriptive, such as using x and y to represent (x, y) coordi- nates on a two-dimensional grid. Likewise, you should not use unnecessarily long names, such as the identifier theCurrentItemBeingProcessed. The name currentItem would serve just as well. As you might imagine, the use of identifiers that are verbose is a much less prevalent problem than the use of names that are not descriptive. If you must err, you should err on the side of readability, but a reasonable bal- ance can almost always be found. Also, you should always be careful when abbreviating words. You might think curStVal is a good name to represent the current stock value, but another person trying to understand the code may have trouble figuring out what you meant. It might not even be clear to you two months after writing it. A name in Java is a series of identifiers separated by the dot (period) charac- ter. The name System.out is the way we designate the object through which we invoked the println method. Names appear quite regularly in Java programs. white space All Java programs use white space to separate the words and symbols used in a program. White space consists of blanks, tabs, and newline characters. The phrase white space refers to the fact that, on a white sheet of paper with black printing, the space between the words and symbols is white. The way a programmer uses white space is important because it can be used to emphasize parts of the code and can make a program easier to read. Java is case sensitive. The uppercase and lowercase ver- sions of a letter are distinct. You should use a consistent case convention for different types of identifiers. ke y co nc ep t Identifier names should be descriptive and readable.ke y co nc ep t Appropriate use of white space makes a program easier to read and understand. ke y co nc ep t Except when it’s used to separate words, the computer ignores white space. It does not affect the execution of a program. This fact gives programmers a great deal of flexibility in how they format a program. The lines of a program should be divided in logical places and certain lines should be indented and aligned so that the program’s underlying structure is clear. Because white space is ignored, we can write a program in many different ways. For example, taking white space to one extreme, we could put as many words as possible on each line. The code in Listing 1.2, the Lincoln2 program, is formatted quite differently from Lincoln but prints the same message. Taking white space to the other extreme, we could write almost every word and symbol on a different line, such as Lincoln3, shown in Listing 1.3. All three versions of Lincoln are technically valid and will execute in the same way, but they are radically different from a reader’s point of view. Both of the lat- ter examples show poor style and make the program difficult to under- stand. The guidelines for writing Java programs presented in Appendix G include the appropriate use of white space. You may be asked to adhere to these or similar guidelines when you write your programs. In any case, you should adopt and consistently use a set of style guide- lines that increase the readability of your code. 1.3 programming 37 listing 1.2 //******************************************************************** // Lincoln2.java Author: Lewis/Loftus // // Demonstrates a poorly formatted, though valid, program. //******************************************************************** public class Lincoln2{public static void main(String[]args){ System.out.println("A quote by Abraham Lincoln:"); System.out.println("Whatever you are, be a good one.");}} A quote by Abraham Lincoln: Whatever you are, be a good one. You should always adhere to a set of guidelines that establish the way you format and docu- ment your programs. key concept output 1.4 programming languages Suppose a particular person is giving travel directions to a friend. That person might explain those directions in any one of several languages, such as English, French, or Italian. The directions are the same no matter which language is used to explain them, but the manner in which the directions are expressed is differ- ent. Furthermore, the friend must be able understand the language being used in order to follow the directions. 38 CHAPTER 1 computer systems listing 1.3 //******************************************************************** // Lincoln3.java Author: Lewis/Loftus // // Demonstrates another valid program that is poorly formatted. //******************************************************************** public class Lincoln3 { public static void main ( String [] args ) { System.out.println ( “A quote by Abraham Lincoln:” ) ; System.out.println ( “Whatever you are, be a good one.” ) ; } } A quote by Abraham Lincoln: Whatever you are, be a good one. output 1.4 programming languages 41 compilers and interpreters Several special-purpose programs are needed to help with the process of devel- oping new programs. They are sometimes called software tools because they are used to build programs. Examples of basic software tools include an editor, a compiler, and an interpreter. Initially, you use an editor as you type a program into a computer and store it in a file. There are many different editors with many different features. You should become familiar with the editor you will use regularly because it can dra- matically affect the speed at which you enter and modify your programs. Each time you need to make a change to the code of your program, you open it in an editor. Figure 1.21 shows a very basic view of the program development process. After editing and saving your program, you attempt to translate it from high-level code into a form that can be executed. That translation may result in errors, in which case you return to the editor to make changes to the code to fix the problems. Once the translation occurs successfully, you can execute the pro- gram and evaluate the results. If the results are not what you want (or if you want to enhance your existing program), you again return to the editor to make changes. The translation of source code into (ultimately) machine language for a par- ticular type of CPU can occur in a variety of ways. A compiler is a program that translates code in one language to equivalent code in another language. The orig- inal code is called source code, and the language into which it is translated is figure 1.20 A high-level expression and its assembly language and machine language equivalent High-Level Language Assembly Language Machine Language a + b 1d [%fp–20], %o0 1d [%fp–24], %o1 add %o0, %o1, %o0 ... 1101 0000 0000 0111 1011 1111 1110 1000 1101 0010 0000 0111 1011 1111 1110 1000 1001 0000 0000 0000 ... 42 CHAPTER 1 computer systems called the target language. For many traditional compilers, the source code is translated directly into a particular machine language. In that case, the trans- lation process occurs once (for a given version of the program), and the resulting executable program can be run whenever needed. 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 statement is translated and executed, and so on. One advantage of this technique is that it eliminates the need for a separate compilation phase. However, the pro- gram generally runs more slowly because the translation process occurs during each execution. The process often used to translate and execute Java programs combines the use of a compiler and an interpreter. This process is pictured in Fig. 1.22. The Java compiler translates Java source code into Java bytecode, which is a representation of the program in a low-level form similar to machine language code. The Java interpreter reads Java bytecode and executes it on a specific machine. Another compiler could translate the bytecode into a particular machine language for efficient execution on that machine. The difference between Java bytecode and true machine language code is that Java bytecode is not tied to any particular processor type. This approach has the distinct advantage of making Java architecture neutral, and therefore easily portable from one machine type to another. The only restriction is that there must be a Java interpreter or a bytecode compiler for each processor type on which the Java bytecode is to be executed. Since the compilation process translates the high-level Java source code into a low-level representation, the interpretation process is more efficient than figure 1.21 Editing and running a program Edit and save program Translate program into executable form errors errors Execute program and evaluate results A Java compiler translates Java source code into Java byte- code. A Java interpreter trans- lates and executes the byte- code. ke y co nc ep t 1.4 programming languages 43 interpreting high-level code directly. Executing a program by inter- preting its bytecode is still slower than executing machine code directly, but it is fast enough for most applications. Note that for effi- ciency, Java bytecode could be compiled into machine code. The Java compiler and interpreter are part of the Java Software Development Kit (SDK), which is sometimes referred to simply as the Java Development Kit (JDK). This kit also contains several other software tools that may be useful to a programmer. The JDK can be downloaded for free from the Sun Microsystem Web site (java.sun.com) or from this book’s Web site. Note that the standard JDK tools are executed on the command line. That is, they are not graphical programs with menus and buttons. The standard JDK tools also do not include an editor, although any editor that can save a document as simple text can be used. Other programs, called Integrated Development Environments (IDEs), have been created to support the development of Java programs. IDEs combine an edi- tor, compiler, and other Java support tools into a single application. The specific tools you will use to develop your programs depend on your environment. figure 1.22 The Java translation and execution process Java source code Java bytecodeJava compiler Java interpreter Bytecode compiler Machine code Java is architecture neutral because Java bytecode is not associated with any particular hardware platform. key concept 46 CHAPTER 1 computer systems The second kind of problem occurs during program execution. It is called a runtime error, and it causes the program to terminate abnor- mally. For example, if we attempt to divide by zero, the program will “crash” and halt execution at that point. Because the requested opera- tion is undefined, the system simply abandons its attempt to continue processing your program. The best programs are robust; that is, they avoid as many run time errors as possible. For example, the program code could guard against the possibility of dividing by zero and handle the situation appropriately if it arises. In Java, many runtime errors are represented as exceptions that can be caught and dealt with accordingly. We discuss exceptions in Chapter 8. The third kind of software problem is a logical error. In this case, the software compiles and executes without complaint, but it produces incorrect results. For example, a logical error occurs when a value is calculated incorrectly or when a graphical button does not appear in the correct place. A programmer must test the program thoroughly, comparing the expected results to those that actually occur. When defects are found, they must be traced back to the source of the problem in the code and corrected. The process of finding and correcting defects in a program is called debugging. Logical errors can manifest themselves in many ways, and the actual root cause might be quite difficult to discover. language evolution As computer technology evolves, so must the languages we use to program them. The Java programming language has undergone various changes since its cre- ation. This text uses the most recent Java technology. Specifically, this book uses the Java 2 Platform, which simply refers to the most advanced collection of Java language features, software libraries, and tools. Several important advances have been made since the previous version. The Java 2 Platform is organized into three major groups: ◗ Java 2 Platform, Standard Edition (J2SE) ◗ Java 2 Platform, Enterprise Edition (J2EE) ◗ Java 2 Platform, Micro Edition (J2ME) This book focuses on the Standard Edition, which, as the name implies, is the mainstream version of the language and associated tools. As we discussed earlier in this chapter, the Java Development Kit (JDK) is the set of software tools provided by Sun Microsystems that can be used for creating A Java program must be syntactically correct or the compiler will not produce bytecode. ke y co nc ep t Java software. These tools include a compiler and an interpreter, among others. The most recent version of the JDK (at the time of this printing), which corre- sponds to the latest version of the Standard Edition of the Java 2 Platform, is JDK 1.4. You might use the JDK to develop your programs, or you might use some other development environment. Some parts of early Java technologies have been deprecated, which means they are considered old-fashioned and should not be used. When it is important, we point out deprecated elements and discuss their state-of-the-art alternatives. One particular area in which Java has evolved is in the software libraries that support the development of graphical user interfaces (GUIs). Specifically, earlier releases of Java used the Abstract Windowing Toolkit (AWT). Included with the Java 2 Platform is a software library called Swing, which builds on the AWT and extends its capabilities. The Swing library contains many elements that replace older, less useful AWT elements. Whenever appropriate, we use Swing technology in this text. 1.5 graphics Graphics play a crucial role in computer systems. Throughout this book we explore various aspects of graphics and discuss how they are accomplished. In fact, the last one or two sections of each chapter are devoted to graphics topics. (These sections can be skipped without losing continuity through the rest of the text.) In this section, we explore the basic concepts of representing a picture in a computer and displaying it on a screen. A picture, like all other information stored on a computer, must be digitized by breaking the information into pieces and representing those pieces as numbers. In the case of pictures, we break the picture into pixels (picture elements), as we mentioned earlier in this chapter. A pixel is a tiny region that represents a very small piece of the picture. The complete picture is stored by storing the color of each individual pixel. A black and white picture can be stored by representing each pixel using a single bit. If the bit is zero, that pixel is white; if the bit is 1, it is black. The picture can be repro- duced when needed by reassembling its pixels. The more pixels used to represent a picture, the more realistic it looks when it is reproduced. Figure 1.23 shows a black and white picture that has been stored digitally and an enlargement of a portion of that picture, which shows the individual pixels. 1.5 graphics 47 The pixels of a black and white picture can be repre- sented using a single bit each, mapping 0 to white and 1 to black. key concept coordinate systems When drawn, each pixel of a picture is mapped to a pixel on the screen. Each computer system and programming language defines a coordinate system so that we can refer to particular pixels. A traditional two-dimensional Cartesian coordinate system has two axes that meet at the origin. Values on either axis can be negative or positive. The Java pro- gramming language has a relatively simple coordinate system in which all of the visible coordinates are positive. Figure 1.24 shows a traditional coordinate sys- tem and the Java coordinate system. Each point in the Java coordinate system is represented using an (x, y) pair of values. The top-left corner of any Java drawing area has coordinates (0, 0). The x-axis coordinates get larger as you move to the right, and the y-axis coordinates get larger as you move down. A Java program does not have to be graphical in nature. However, if it is, each graphical component in the program has its own coordinate system, with the ori- gin (0, 0) in the top-left corner. This consistent approach makes it relatively easy to manage various graphical elements. figure 1.23 A digitized picture with a small portion magnified 48 CHAPTER 1 computer systems summary of key concepts 51 ◗ The speed of the system clock indicates how fast the CPU executes instructions. ◗ A network consists of two or more computers connected together so they can exchange information. ◗ Sharing a communication line creates delays, but it is cost effective and simplifies adding new computers to the network. ◗ A local-area network (LAN) is an inexpensive way to share information and resources throughout an organization. ◗ The Internet is a wide-area network (WAN) that spans the globe. ◗ TCP/IP is the set of software protocols that govern the movement of mes- sages across the Internet. ◗ Every computer connected to the Internet has an IP address that uniquely identifies it. ◗ The World Wide Web is software that makes sharing information across a network easy. ◗ A browser is a software tool that loads and formats Web documents for viewing. These documents are often written using the HyperText Markup Language (HTML). ◗ A URL uniquely specifies documents and other information found on the Web for a browser to obtain and display. ◗ The purpose of writing a program is to solve a problem. ◗ The first solution we design to solve a problem may not be the best one. ◗ This book focuses on the principles of object-oriented programming. ◗ Comments do not affect a program’s processing; instead, they serve to facilitate human comprehension. ◗ The main method must always be defined using the words public, static, and void. ◗ Inline documentation should provide insight into your code. It should not be ambiguous or belabor the obvious. ◗ Java is case sensitive. The uppercase and lowercase versions of a letter are distinct. You should use a consistent case convention for different types of identifiers. ◗ Identifier names should be descriptive and readable. ◗ Appropriate use of white space makes a program easier to read and understand. 52 CHAPTER 1 computer systems ◗ You should always adhere to a set of guidelines that establish the way you format and document your programs. ◗ All programs must be translated to a particular CPU’s machine language in order to be executed. ◗ Working with high-level languages allows the programmer to ignore the underlying details of machine language. ◗ A Java compiler translates Java source code into Java bytecode. A Java interpreter translates and executes the bytecode. ◗ Java is architecture neutral because Java bytecode is not associated with any particular hardware platform. ◗ The syntax rules of a programming language dictate the form of a pro- gram. The semantics dictate the meaning of the program statements. ◗ A computer follows our instructions exactly. The programmer is responsi- ble for the accuracy and reliability of a program. ◗ A Java program must be syntactically correct or the compiler will not pro- duce bytecode. ◗ The pixels of a black and white picture can be represented using a single bit each, mapping 0 to white and 1 to black. ◗ The pixels of a color picture can be represented using three numbers, collectively called the RGB value, which represent the relative contribu- tions of three primary colors: red, green, and blue. self-review questions 1.1 What is hardware? What is software? 1.2 What are the two primary functions of an operating system? 1.3 What happens to information when it is stored digitally? 1.4 How many unique items can be represented with the following? ◗ 2 bits ◗ 4 bits ◗ 5 bits ◗ 7 bits self-review questions 53 1.5 How many bits are there in each of the following? ◗ 8 bytes ◗ 2 KB ◗ 4 MB 1.6 What are the two primary hardware components in a computer? How do they interact? 1.7 What is a memory address? 1.8 What does volatile mean? Which memory devices are volatile and which are nonvolatile? 1.9 What is a file server? 1.10 What is the total number of communication lines needed for a fully connected point-to-point network of five computers? Six computers? 1.11 What is the origin of the word Internet? 1.12 Explain the parts of the following URLs: ◗ duke.csc.villanova.edu/jss/examples.html ◗ java.sun.com/products/index.html 1.13 What is the relationship between a high-level language and machine language? 1.14 What is Java bytecode? 1.15 What is white space? How does it affect program execution? How does it affect program readability? 1.16 Which of the following are not valid Java identifiers? Why? ◗ RESULT ◗ result ◗ 12345 ◗ x12345y ◗ black&white ◗ answer_7 1.17 What do we mean by the syntax and semantics of a programming language? 1.18 How can a black and white picture be represented using 1s and 0s? 56 CHAPTER 1 computer systems 1.18 Java is case sensitive. What does that mean? 1.19 What do we mean when we say that the English language is ambiguous? Give two examples of English ambiguity (other than the example used in this chapter) and explain the ambiguity. Why is ambiguity a problem for programming languages? 1.20 Categorize each of the following situations as a compile-time error, runtime error, or logical error. ◗ multiplying two numbers when you meant to add them ◗ dividing by zero ◗ forgetting a semicolon at the end of a programming statement ◗ spelling a word wrong in the output ◗ producing inaccurate results ◗ typing a { when you should have typed ( 1.21 Compare and contrast a traditional coordinate system and the coor- dinate system used by Java graphical components. 1.22 How many bits are needed to store a color picture that is 400 pixels wide and 250 pixels high? Assume color is represented using the RGB technique described in this chapter and that no special com- pression is done. programming projects 1.1 Enter, compile, and run the following application: public class Test { public static void main (String[] args) { System.out.println (“An Emergency Broadcast”); } } 1.2 Introduce the following errors, one at a time, to the program from the programming project 1.1. Record any error messages that the programming projects 57 compiler produces. Fix the previous error each time before you introduce a new one. If no error messages are produced, explain why. Try to predict what will happen before you make each change. ◗ change Test to test ◗ change Emergency to emergency ◗ remove the first quotation mark in the string ◗ remove the last quotation mark in the string ◗ change main to man ◗ change println to bogus ◗ remove the semicolon at the end of the println statement ◗ remove the last brace in the program 1.3 Write an application that prints, on separate lines, your name, your birthday, your hobbies, your favorite book, and your favorite movie. Label each piece of information in the output. 1.4 Write an application that prints the phrase Knowledge is Power: ◗ on one line ◗ on three lines, one word per line, with the words centered relative to each other ◗ inside a box made up of the characters = and | 1.5 Write an application that prints the following diamond shape. Don’t print any unneeded characters. (That is, don’t make any character string longer than it has to be.) * *** ***** ******* ********* ******* ***** *** *
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved