© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION CHAPTER 1

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE IntroductionOR DISTRIBUTION to ProgrammingNOT FOR SALE OR DISTRIBUTION and the Java Language © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION CHAPTER CONTENTS

Introduction 1.4 Programming Languages © Jones1.1 Basic & Bartlett Computer Learning, Concepts LLC 1.4.1© JonesHigh- an &d BartlettLow-Level Learning, Languages LLC NOT FOR1.1.1 SALE Hardw ORare DISTRIBUTION 1.4.2NOT An FOR Introduction SALE toOR Object- DISTRIBUTION 1.1.2 Operating Systems Oriented Programming 1.1.3 Application Software 1.4.3 The Java Language 1.1.4 Computer Networks and the 1.5 An Introduction to Programming © Jones & Bartlett Learning,Internet LLC © Jones &1.5.1 Bartlett Progra Learning,mming Basics LLC NOT FOR SALE 1.2OR DISTRIBUTIONPractice Activity: Displaying System NOT FOR 1.5.2SALE Progra OR DISTRIBUTIONm Design with Pseudocode Configuration 1.5.3 Developing a Java Application 1.2.1 Displaying Windows Configuration 1.5.4 Programming Activity 1: Writing Information a First Java Application 1.2.2 ©Displ Jonesaying & Mac Bartlett OS Configuration Learning, LLC 1.5.5 Making a JAR© File Jones & Bartlett Learning, LLC NOTInformation FOR SALE OR DISTRIBUTION1.6 Chapter Summary NOT FOR SALE OR DISTRIBUTION 1.3 Data Representation 1.7 Exercises, Problems, and Projects 1.3.1 Binary Numbers 1.7.1 Multiple Choice Exercises 1.3.2 Using Hexadecimal Numbers to 1.7.2 Converting Numbers © Jones & BartlettRepresent Learning, Binary LLC Numbers 1.7.3© JonesGeneral & Q Bartlettuestions Learning, LLC NOT FOR1.3.3 SALE Repres ORenting DISTRIBUTION Characters with the 1.7.4NOT Technic FORal SALE Writing OR DISTRIBUTION Unicode Character Set 1.7.5 Group Project

© Jones & Bartlett Learning, LLC© Jones & Bartlett Learning, LLC.© NOTJones FOR SALE& Bartlett OR DISTRIBUTION Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9781284239935_CH01_Print.indd 1 23/11/17 10:13 AM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

2 CHAPTER 1 Introduction to Programming and the Java Language

©Introduction Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION Computer applications touch almost every aspect of our lives. They run automated teller machines, the grocery store’s checkout register, the appointment calendar at your doctor’s office, airport kiosks for flight check-in, a restaurant’s meal-ordering © Jones & Bartlettsystem, Learning, and online auctions, LLC just to name a few applications.© Jones & On Bartlett your personal Learning, com- LLC NOT FOR SALEputer, OR you DISTRIBUTION may run a word processor, virus detectionNOT software, FOR SALE a spreadsheet, OR DISTRIBUTION com- puter games, and an image processing system. Someone, usually a team of , wrote those applications. If you’re read- ing this text, you’re probably curious about what’s involved in writing applications, © Jones & Bartlett Learning,and LLC you would like to write a few© yourself.Jones &Perhaps Bartlett you haveLearning, an idea forLLC the world’s NOT FOR SALE OR DISTRIBUTIONnext great application or computerNOT game. FOR SALE OR DISTRIBUTION In this text, we’ll cover the basics of writing applications. Specifically, we’ll use the Java programming language. Keep in mind, however, that becoming a good pro- grammer requires more than mastering the rules, or syntax, of a programming lan- ©guage. Jones You & also Bartlett must master Learning, basic programming LLC techniques. These© Jones are established & Bartlett Learning, LLC NOTmethods FOR for SALE performing OR DISTRIBUTION common programming operations, suchNOT as calculatingFOR SALE a OR DISTRIBUTION total, finding an average, or arranging a group of items in order. You also must master good software engineering principles so that you design © Jones & Bartlettcode that Learning, is readable, LLC easily maintained, and reusable.© Jones By &readable, Bartlett we Learning,mean that LLC NOT FOR SALEsomeone OR DISTRIBUTION else should be able to read your programNOT FORand figure SALE out OR what DISTRIBUTION it does and how it does it. Writing readable code is especially important for program- mers who want to advance in their careers, because it allows someone else to take over the maintenance of your program while you move on to bigger and better responsibilities. Ease of maintenance is also an important aspect of programming, © Jones & Bartlett Learning,because LLC the specifications for any© Jones program & areBartlett continually Learning, changing. LLC How many NOT FOR SALE OR DISTRIBUTIONprograms can you name that haveNOT had FOR only oneSALE version? OR DISTRIBUTIONNot many. Well-designed code allows you and others to incorporate prewritten and pretested modules into your program, thus reducing the time to develop a program and yielding code that is more robust and has fewer bugs. One useful feature of the Java programming ©language Jones is& theBartlett large supply Learning, of prewritten LLC code that you are free© Jones to use in& yourBartlett Learning, LLC NOTprograms. FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION Programming is an exciting activity. It’s very satisfying to decompose a complex task into computer instructions and watch your program come alive. It can be frustrat- ing, however, when your program either doesn’t run at all or produces the wrong © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC output. NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION Writing correct programs is critical. Someone’s life or life savings may depend on the correctness of your program. Reusing code helps in developing correct programs,

© Jones & Bartlett Learning, LLC© Jones & Bartlett Learning, LLC.© NOTJones FOR SALE& Bartlett OR DISTRIBUTION Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9781284239935_CH01_Print.indd 2 23/11/17 10:13 AM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

1.1 Basic Computer Concepts 3

but you must© alsoJones master & Bartletteffective testingLearning, techniques LLC to verify that the output© Jones of & Bartlett Learning, LLC your programNOT is correct. FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION In this text, we’ll concentrate not only on the syntax of the Java language, but also on basic programming techniques, good software engineering principles, and effective testing techniques. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC Before you can write programs, however, it’s important to understand the platform NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION on which your program will run. A platform refers to the computer hardware and the operating system. Your program will use the hardware for inputting data, for performing calculations, and for outputting results. The operating system will start your program running and will provide your program with essential resources, such © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC as memory, and services, such as reading and writing files. NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 1.1 Basic Computer Concepts 1.1.1 Hardware © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC As shown inNOT Figure FOR 1.1, a SALE computer OR typically DISTRIBUTION includes the following components:NOT FOR SALE OR DISTRIBUTION

■■ a CPU, or central processing unit, which executes the instructions of a pro- gram

■■ a memory unit, which holds the instructions and data of a program while © Jones &it Bartlettis executing Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION ■■ a hard disk, used to store programs and data so that they can be loaded into memory and accessed by the CPU

■■ a keyboard used for input of data

© Jones & Bartlett Learning,■■ a monitor, LLC used to display output from© aJones program & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION ■■ an Ethernet port and wireless networking transceiver for connecting to the Internet or a Local Area Network (LAN)

■■ other components (not shown) such as a graphics card and a DVD drive For example,© if Jones you were & toBartlett go to a computerLearning, store LLC in search of the latest personal© Jones & Bartlett Learning, LLC computer, youNOT might FOR be shown SALE a computerOR DISTRIBUTION with this set of specifications: NOT FOR SALE OR DISTRIBUTION

■■ a 3.4-GHz Intel Core i3-3240 processor

■■ a touch screen

© Jones ■■ &3 BartlettMB of cache Learning, memory LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION ■■ 8 GB of RAM (Random Access Memory)

■■ a 1 TB (Terabyte) hard disk

© Jones & Bartlett Learning, LLC© Jones & Bartlett Learning, LLC.© NOTJones FOR SALE& Bartlett OR DISTRIBUTION Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9781284239935_CH01_Print.indd 3 23/11/17 10:13 AM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

4 CHAPTER 1 Introduction to Programming and the Java Language

Figure 1.1 © Jones Motherboard & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC A Typical Design NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION of a Personal Input Computer CPU (keyboard, LAN) with L1 cache © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION L2 cache Output (monitor, printer, LAN)

Main memory © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

Secondary storage © Jones & Bartlett(hard Learning, disk) LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION In these specifications, the Intel Core i3-3240 is the CPU. Other processors used as CPUs in personal computers and servers include the AMD Athlon, the Oracle SPARC, and the IBM POWER processor. © Jones & BartlettCPUs consistLearning, of an ArithmeticLLC Logic Unit (ALU)© [also Jones called & anBartlett Integer UnitLearning, (IU)], LLC NOT FOR SALEwhich OR performs DISTRIBUTION basic integer arithmetic and logicalNOT operations; FOR SALE a Floating OR Point DISTRIBUTION Unit (FPU), which performs floating-point arithmetic; a set of hardware registers for hold- ing data and memory addresses; and other supporting hardware, including a control unit to sequence the instructions. Each CPU comes with its own set of instructions, © Jones & Bartlett Learning,which LLC are the operations that it© canJones perform. & Bartlett The instructions Learning, typically LLC perform NOT FOR SALE OR DISTRIBUTIONarithmetic and logic operations, NOTmove dataFOR from SALE one locationOR DISTRIBUTION to another, and change the flow of the program (that is, determine which instruction is to be executed next). A program consists of many instructions. The first step in executing a program is loading it into memory. Each instruction in the program is executed using a ©Fetch-Decode-Execute Jones & Bartlett Learning, Cycle. The LLCProgram Counter keeps © track Jones of the & Bartlett next Learning, LLC NOTinstruction FOR toSALE be fetched. OR DISTRIBUTION The CPU fetches the next instruction NOTfrom memory FOR SALE and OR DISTRIBUTION places it into the Instruction Register. The instruction is decoded (is the instruction a move, load, store, etc.?). Then, the instruction is executed. This Fetch-­Decode- Execute Cycle repeats until the program ends. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALEThe OR speed DISTRIBUTION of a CPU is related to its clock cycle, typicallyNOT FOR rated SALE in GHz OR (gigahertz); DISTRIBUTION at the time of this edition, a high-end CPU speed would be rated at 3.4 GHz. The clock speed, measured in clock cycles per second, determines how fast a processor can per- form operations such as fetching, decoding, and executing instructions. A CPU rated

© Jones & Bartlett Learning, LLC© Jones & Bartlett Learning, LLC.© NOTJones FOR SALE& Bartlett OR DISTRIBUTION Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9781284239935_CH01_Print.indd 4 23/11/17 10:13 AM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

1.1 Basic Computer Concepts 5

at 2.5 GHz executes© Jones 2.5 billion& Bartlett clock cycles Learning, a second. LLC Current RISC processors feature© Jones & Bartlett Learning, LLC pipelining, whichNOT allows FOR the SALE CPU toOR process DISTRIBUTION several instructions at once, so that NOTwhile FOR SALE OR DISTRIBUTION one instruction is executing, the processor can decode the next instruction and fetch the next instruction after that. This greatly improves performance of applications. Memory or storage devices, such as L2 cache, memory, or hard disks, are typically © Jonesrated in & terms Bartlett of their Learning, capacity, LLCexpressed in bytes. A byte ©is eightJones binary & Bartlett digits, Learning, LLC NOTor FORbits. A SALE single ORbit’s valueDISTRIBUTION is 0 or 1. Depending on the type ofNOT memory FOR or SALE storage OR DISTRIBUTION device, the capacity will be stated in kilobytes, megabytes, gigabytes, or even tera- bytes. The sizes of these units are shown in Table 1.1. For the CPU to execute at its rated speed, however, instructions and data must be © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC available to the CPU at that speed as well. Instructions and data come directly from NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION the L1 cache, which is memory directly located on the CPU chip. Since the L1 cache is located on the CPU chip, it runs at the same speed as the CPU. However, the L1 cache, which can be several Mbytes, is typically much smaller than main memory, and eventually the CPU will need to process more instructions and data than can be held in the L1© cacheJones at one& Bartlett time. Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION At that point, the CPU typically brings data from what is called the L2 cache, which is located on separate memory chips connected to the CPU. A typical speed for the L2 cache would be a few nanoseconds (billionths of a second) access time, and this will considerably slow down the rate at which the CPU can execute instructions. L2 © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC cache size today is typically 3 to 8 Mbytes, and again, the CPU will eventually need NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION more space for instructions and data than the L2 cache can hold at one time. At that point, the CPU will bring data and instructions from main memory, also located outside, but connected to, the CPU chip. This will slow down the CPU even © Jones & Bartlettmore, Learning, because main LLC memory typically has an ©access Jones time & of Bartlettabout 20 toLearning, 50 nanosec -LLC NOT FOR SALE onds.OR DISTRIBUTION Main memory, though, is significantly largerNOT in FOR size than SALE the L1OR and DISTRIBUTION L2 caches, typically anywhere between 3 and 8 Gbytes. When the CPU runs out of space again, it will have to get its data from the hard disk, which is typically 1 Tbyte or more, but with an access time in the milliseconds (thousandths of a second) range. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC TABLE 1.1 MemoryNOT FOR Units and SALE Their SORizes DISTRIBUTION NOT FOR SALE OR DISTRIBUTION Memory Unit Size KB, or Kbytes, or kilobytes About 1,000 bytes (exactly 210 or 1,024 bytes) © JonesMB, or Mbytes,& Bartlett or megabytes Learning, About LLC 1 million bytes (exactly 220 or 1,048,576© Jones bytes) & Bartlett Learning, LLC NOTGB, FOR or Gbytes, SALE or gigabytes OR DISTRIBUTION About 1 billion bytes (exactly 230 or 1,073,741,824NOT FORbytes) SALE OR DISTRIBUTION TB, or Tbytes, or terabytes About 1 trillion bytes (exactly 240 or 1.09951 × 1012 bytes)

© Jones & Bartlett Learning, LLC© Jones & Bartlett Learning, LLC.© NOTJones FOR SALE& Bartlett OR DISTRIBUTION Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9781284239935_CH01_Print.indd 5 23/11/17 10:13 AM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

6 CHAPTER 1 Introduction to Programming and the Java Language

©As Jones you can & seeBartlett from these Learning, numbers, LLC a considerable amount of ©speed Jones is lost & whenBartlett Learning, LLC NOTthe CPU FOR goes SALE from mainOR DISTRIBUTION memory to disk, which is why having sufficientNOT FOR memory SALE is OR DISTRIBUTION very important for the overall performance of applications. Another factor that should be taken into consideration is cost per kilobyte. Typically the cost per kilobyte decreases significantly stepping down from L1 cache to hard © Jones & Bartlettdisk, so Learning, high performance LLC is often traded for low p© rice.Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION Main memory (also called RAM) uses DRAM, or Dynamic Random Access Memory technology, which maintains data only when power is applied to the memory and needs to be refreshed regularly in order to retain data. The L1 and L2 caches use SRAM, or Static Random Access Memory technology, which also needs power but © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC does not need to be refreshed in order to retain data. Memory capacities are typ- NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION ically stated in powers of 2. For instance, 256 Kbytes of memory is 218 bytes, or 262,144 bytes. Memory chips contain cells, each cell containing a bit, which can store either a 0 or ©a Jones1. Cells can& Bartlett be accessed Learning, individually LLC or as a group of typically 4,© 8, Jones or 16 cells. & Bartlett For Learning, LLC 13 NOTinstance, FOR a 32-Kbit SALE RAM OR chipDISTRIBUTION organized as 8K × 4 is composed of exactlyNOT FOR 2 , or SALE 8,192 OR DISTRIBUTION units, each unit containing four cells. This RAM chip will have four data output pins (or lines) and 13 access pins (or lines), enabling access to all 8,192 units because each access pin can have a value of 0 or 1. Table 1.2 compares the features of various memory types. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 1.1.2 Operating Systems An operating system (OS) is a software program that

© Jones & Bartlett Learning, LLC■■ controls the peripheral ©devices Jones (for & instance, Bartlett it manages Learning, the file LLC system) NOT FOR SALE OR DISTRIBUTION■■ supports multitasking, byNOT scheduling FOR SALE multiple OR programs DISTRIBUTION or tasks to execute during the same interval

■■ allocates memory to each program, so that there is no conflict among the memory of any programs running at the same time © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC ■■ prevents the user from damaging the system. For instance, it prevents user NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION programs from overwriting the operating system or another program’s memory The operating system loads, or boots, when the computer system is turned on and © Jones & Bartlettis intended Learning, to run as LLC long as the computer is running.© Jones & Bartlett Learning, LLC NOT FOR SALEExamples OR DISTRIBUTION of operating systems are macOS, MicrosoftNOT Windows, FOR SALE Linux, OR and DISTRIBUTION iOS and Android for mobile devices.

© Jones & Bartlett Learning, LLC© Jones & Bartlett Learning, LLC.© NOTJones FOR SALE& Bartlett OR DISTRIBUTION Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9781284239935_CH01_Print.indd 6 23/11/17 10:13 AM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

1.1 Basic Computer Concepts 7

TABLE 1.2 A© Comparison Jones & of BartlettMemory Types Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION Device Location Type Speed Capacity (MB) Cost/KB L1 cache On-chip SRAM Very fast Very small Very high L2 cache Off-chip SRAM Fast Small High © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOTMemory FOR SALE Off-chip OR DISTRIBUTIONDRAM Moderate ModerateNOT FOR Moderate SALE OR DISTRIBUTION Hard disk Separate Disk media Slow Large Small

1.1.3 Application Software © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE ApplicationOR DISTRIBUTION software consists of the programsNOT written FOR to SALE perform OR specific DISTRIBUTION tasks. These programs are run by the operating system, or as is typically said, they are run “on top of” the operating system. Examples of applications are word proces- sors, such as Microsoft Word or Apache OpenOffice; spreadsheets, such as Micro- soft Excel; database© Jones management & Bartlett systems, Learning, such as LLC Oracle or Microsoft SQL Server;© Jones & Bartlett Learning, LLC Internet browsers,NOT FORsuch as SALE Mozilla OR Firefox, DISTRIBUTION Google Chrome, or Microsoft Edge;NOT and FOR SALE OR DISTRIBUTION most of the programs you will write during your study of Computer Science.

1.1.4 Computer Networks and the Internet © JonesComputer & Bartlett Networks Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION Computer networks connect two or more computers. A common network used by many corporations and universities is a LAN, or Local Area Network. A typical LAN connects several computers that are geographically close to one another, often in the same building, and allows them to share resources, such as a printer, a data- © Jones & Bartlettbase, Learning, or a file system. LLC In a LAN, most user computers© Jones are & called Bartlett clients Learning,, and one or LLC NOT FOR SALE moreOR DISTRIBUTION computers act as servers. The server controlsNOT FOR access SALE to resources OR DISTRIBUTION on the net- work and can supply services to the clients, such as answering database requests, storing and serving files, or managing email. The computers on the ­network can exchange data through physical cables or through a wireless network. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC The InternetNOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION The Internet is a network of networks, connecting millions of computers around the world. The Internet evolved from ARPANET, a 1969 U.S. military research proj- ect whose goal was to design a method for computers to communicate. Most com- © Jonesputers &on Bartlett the Internet Learning, are clients, LLC typically requesting resources,© Jones such as & webpages, Bartlett Learning, LLC NOTthrough FOR SALEan Internet OR browser.DISTRIBUTION These resources are provided byNOT web FOR servers, SALE which OR DISTRIBUTION store webpages and respond to these requests.

© Jones & Bartlett Learning, LLC© Jones & Bartlett Learning, LLC.© NOTJones FOR SALE& Bartlett OR DISTRIBUTION Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9781284239935_CH01_Print.indd 7 23/11/17 10:13 AM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

8 CHAPTER 1 Introduction to Programming and the Java Language

©Every Jones machine & Bartlett on the InternetLearning, has aLLC unique ID called its IP address© Jones (IP stands & Bartlett for Learning, LLC NOTInternet FOR Protocol). SALE SpecialOR DISTRIBUTION computers called routers find a path NOTthrough FOR the SALEInter- OR DISTRIBUTION net from one computer to another using these IP addresses. A computer can have a static IP address, which is dedicated to that machine, or a dynamic IP address, which is assigned to the computer when it connects to the Internet. An IP address is © Jones & Bartlettmade up Learning, of four octets, LLC whose values in decimal notation© Jones are & between Bartlett 0 and Learning, 255. For LLC NOT FOR SALEinstance, OR DISTRIBUTION 58.203.151.103 could represent such anNOT IP address. FOR In SALE binary OR notation, DISTRIBUTION this IP address is 111010.11001011.10010111.1100111. Later in this chapter, we will learn how to convert a decimal number, such as 103, to its binary equivalent, 1100111. Most people are familiar with URL (Uniform Resource Locator) addresses that look © Jones & Bartlett Learning,like LLC http://www.oracle.com/technetwork/java/index.html© Jones & Bartlett . Learning,A URL is actually LLC an Inter- NOT FOR SALE OR DISTRIBUTIONnet domain name as well as theNOT path onFOR that SALEdomain OR to a DISTRIBUTIONspecific web page. In this URL, the domain name is www.oracle.com. The page requested is index.html, which is located in the java folder under the technetwork folder. Domain name resolution servers, which implement the Domain Name System ©(DNS), Jones convert & Bartlett domain Learning, names to IP LLC addresses, so that Internet users© Jones don’t need& Bartlett to Learning, LLC NOTknow FOR the IP SALE addresses OR of DISTRIBUTION websites they want to visit. The WorldNOT Wide FOR Web SALECon- OR DISTRIBUTION sortium (W3C), an international group developing standards for Internet access, prefers the term Uniform Resource Identifier (URI) rather than URL, because URI covers future Internet addressing schemes. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION Skill Practice with these end-of-chapter questions

1.7.1 Multiple Choice Exercises © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION Questions 1, 2, 3, 4NOT FOR SALE OR DISTRIBUTION 1.7.3 General Questions Questions 21, 22, 23 © Jones1.7.4 & BartlettTechnical Writing Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALEQuestions OR DISTRIBUTION 31, 32, 33 NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett1.2 Learning,Practice Activity: LLC Displaying System Configuration© Jones & Bartlett Learning, LLC NOT FOR SALEWe OR have DISTRIBUTION explored hardware and operating systemsNOT in FOR general. SALE Now, OR let’s DISTRIBUTION discover some information about the hardware and operating system on your computer. Depending on whether you’re using a Windows operating system or a Mac OS

© Jones & Bartlett Learning, LLC© Jones & Bartlett Learning, LLC.© NOTJones FOR SALE& Bartlett OR DISTRIBUTION Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9781284239935_CH01_Print.indd 8 23/11/17 10:13 AM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

1.2 Practice Activity: Displaying System Configuration 9

operating system,© Jones choose & Bartlett the appropriate Learning, directions LLC that follow to display© theJones & Bartlett Learning, LLC operating system’sNOT FORname, SALE the CPU OR type, DISTRIBUTION and how much memory the computerNOT has. FOR SALE OR DISTRIBUTION

1.2.1 Displaying Windows Configuration Information To display system configuration information on a computer running Windows 10, run © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC msinfo32.exe from the command line. From the Start menu, type cmd to start the Com- NOTmand FOR Prompt SALE program OR DISTRIBUTION and then type msinfo32 into the CommandNOT Prompt FOR SALEwindow. OR DISTRIBUTION You will get a display similar to the one in Figure 1.2, although the information displayed varies, depending on your hardware and the version of Windows you are running.

© Jones & BartlettItem Learning, LLC Value © Jones & Bartlett Learning, LLCFigure 1.2 NOT FOR SALE OROS Name DISTRIBUTION MicrosoftNOT Windows FOR 10 Enterprise SALE OR DISTRIBUTION Version 10.0.14393 Build 14393 Windows System Other OS Description Not Available Information OS Manufacturer Microsoft Corporation System Name PC07WK39 Screen shots reprinted with System ManufacturerLENOVO permission from Apple Inc. System Model © Jones & Bartlett Learning,20FAS3QE00 LLC © Jones & Bartlett Learning, LLC System Type x64-based PC System SKULNOT FOR SALE OR DISTRIBUTIONENOVO_MT_20FA_BU_Think_FM_ThinkPad NOT FOR SALE OR DISTRIBUTION T460s Processor Intel() Core(TM) i7-6600U CPU @ 2.60GHz, 2808 Mhz, 2 Core(s), 4 Logical Processor(s) BIOS Version/DateLENOVO N1CET47W (1.15 ), 8/8/2016 SMBIOS Version 2.8 © JonesEmbedded & ControllerBartlett Version Learning, LLC 1.09 © Jones & Bartlett Learning, LLC NOTBIOS FOR Mode SALE OR DISTRIBUTIONLegacy NOT FOR SALE OR DISTRIBUTION BaseBoard Manufacturer LENOVO BaseBoard Model Not Available BaseBoard Name Base Board Platform Role Mobile Secure Boot StateUnsupported PCR7 ConfigurationBinding Not Possible © Jones & BartlettWindows Learning, DirectoryC LLC :\windows© Jones & Bartlett Learning, LLC NOT FOR SALE ORSystem DISTRIBUTION DirectoryC:\windows\system32NOT FOR SALE OR DISTRIBUTION Boot Device \Device\HarddiskVolume1 Locale United States Hardware Abstraction Layer Version = "10.0.14393.206" User Name UNIVERSITY\USER Time Zone Eastern Daylight Time Installed Physical© Memory Jones (RAM) & Bartlett Learning,12.0 GB LLC © Jones & Bartlett Learning, LLC Total Physical Memory 11.4GB Available PhysicalNOT Memory FOR SALE OR DISTRIBUTION8.41 GB NOT FOR SALE OR DISTRIBUTION Total Virtual Memory 13.8GB Available Virtual Memory 10.2GB Page File Space 2.38 GB Page FileC:\pagefile.sys Device Guard Virtualization based security Not enabled © JonesHyper-V & - VM Bartlett Monitor Mode Learning, Extensions LLC Yes © Jones & Bartlett Learning, LLC NOTHyper-V FOR - SALESecond Level OR Address DISTRIBUTION Translation Yes NOT FOR SALE OR DISTRIBUTION Extensions Hyper-V - Virtualization Enabled in Firmware Yes Hyper-V - Data Execution Protection Yes

© Jones & Bartlett Learning, LLC© Jones & Bartlett Learning, LLC.© NOTJones FOR SALE& Bartlett OR DISTRIBUTION Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9781284239935_CH01_Print.indd 9 23/11/17 10:13 AM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

10 CHAPTER 1 Introduction to Programming and the Java Language

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION Figure 1.3 Mac Operating As you can see in Figure 1.2, this computer is running Intel™ Core™ i7-6600U CPU System processor running at 2.6 GHz, and the computer has 12 Gbytes of memory, 8.41 Information ©Gbytes Jones of which& Bartlett is not beingLearning, used at LLC the time of the display. © Jones & Bartlett Learning, LLC Screen shots reprinted with NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION permission from Apple Inc. 1.2.2 Displaying Mac OS Configuration Information To display system information on a Mac OS computer, click on the apple icon on the top left of the screen and select “About This Mac.” You will see something similar © Jones & Bartlettto the Learning,display in Figure LLC 1.3, which displays the© version Jones of &the Bartlett MAC OS Learning, operating LLC NOT FOR SALEsystem OR thatDISTRIBUTION is running on the computer. NOT FOR SALE OR DISTRIBUTION As you can see, this iMac is running macOS High Sierra version 10.13.1. The pro- cessor is an Intel Core i5 running at 3.2 GHz and the iMac has 16 Gbytes of memory. Your information may be different from that shown in the figure, depending on © Jones & Bartlett Learning,your LLC hardware and operating system© Jones version. & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

DISCUSSION QUESTIONS ? © Jones1. & CompareBartlett the Learning, system information LLC on several computers. Is it ©the Jones same or dif- & Bartlett Learning, LLC NOT FOR SALEferent from OR comput DISTRIBUTIONer to computer? Explain why the informationNOT is theFOR same SALE OR DISTRIBUTION or different. 2. In the sample display for Windows 10, the computer has 12 Gbytes of memory, but only 8.41 Gbytes of memory is available. Why do you think some memory © Jones & Bartlett Learning,is not available? LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION3. Compare your computer to the ones on theNOT previous FOR pages SALE shown OR here. DISTRIBUTION Which do you think would have better performance? Explain your answer.

© Jones & Bartlett Learning, LLC© Jones & Bartlett Learning, LLC.© NOTJones FOR SALE& Bartlett OR DISTRIBUTION Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9781284239935_CH01_Print.indd 10 23/11/17 10:46 AM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

1.3 Data Representation 11

1.3 Data© Representation Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 1.3.1 Binary Numbers As mentioned earlier, a CPU understands only binary numbers, whose digits con- sist of either 0 or 1. All data is stored in a computer’s memory as binary digits. A bit © Jonesholds one & Bartlettbinary digit. Learning, A byte holds LLC eight binary digits. © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION Binary numbers are expressed in the base 2 system, because there are only 2 values in that system, 0 and 1. By contrast, most people are used to the decimal, or base 10, system, which uses the values 0 through 9. © Jones & BartlettThere Learning, are other number LLC systems, such as the octal,© Jones or base & 8,Bartlett system, which Learning, uses the LLC NOT FOR SALE digitsOR DISTRIBUTION from 0 to 7, and the hexadecimal, or baseNOT 16, FOR system, SALE which OR uses DISTRIBUTION the digits 0 to 9 and the letters A to F. As we know it in the decimal system, the number 359 is composed of the following three digits: © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC 2 3, representingNOT FOR the hundreds,SALE OR or DISTRIBUTION10 NOT FOR SALE OR DISTRIBUTION 5, representing the tens, or 101 9, representing the ones, or 100

© JonesTherefore, & Bartlett we can write Learning, 359 as LLC © Jones & Bartlett Learning, LLC NOT359 FOR = 3*10 SALE2 + 5*10 OR1 + 9*10DISTRIBUTION0 NOT FOR SALE OR DISTRIBUTION

Thus, the decimal number 359 is written as a linear combination of powers of 10 with coefficients from the base 10 alphabet, that is, the digits from 0 to 9. Similarly, © Jones & Bartlettthe Learning,binary number LLC 11011 is written as a linear© Jones combination & Bartlett of powers Learning, of 2 with LLC coefficients from the base 2 alphabet, that is, the digits 0 and 1. NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION For example, the binary number 11011 can be written as

11011 = 1*24 + 1*23 + 0*22 + 1*21 + 1*20

Table 1.3 lists© theJones binary & equivalentsBartlett Learning, for the decimal LLC numbers 0 through 8, while© Jones & Bartlett Learning, LLC Table 1.4 listsNOT the decimal FOR SALE equivalents OR ofDISTRIBUTION the first 15 powers of 2. NOT FOR SALE OR DISTRIBUTION Note that in Table 1.3, as we count in increments of 1, the last digit alternates between 0 and 1. In fact, we can see that for even numbers, the last digit is always 0 and for odd numbers, the last digit is always 1. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC Because computers store numbers as binary, and people recognize numbers as deci- NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION mal values, conversion between the decimal and binary number systems often takes place inside a computer.

© Jones & Bartlett Learning, LLC© Jones & Bartlett Learning, LLC.© NOTJones FOR SALE& Bartlett OR DISTRIBUTION Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9781284239935_CH01_Print.indd 11 23/11/17 10:13 AM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

12 CHAPTER 1 Introduction to Programming and the Java Language

© TABLEJones 1.3 &Binary Bartlett Equivalents Learning, of Decimal LLC Numbers 0 Through 8 © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION Decimal Binary 0 0000 1 0001 © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC 2 0010 NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 3 0011 4 0100 5 0101 © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION6 0110 NOT FOR SALE OR DISTRIBUTION 7 0111 8 1000 © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION TABLE 1.4 Powers of 2 and Their Decimal Equivalents

214 213 212 211 210 29 28 27 26 25 24 23 22 21 20 © Jones & Bartlett16,384 Learning, 8,192 4,096 LLC 2,048 1,024 512 256© 128 Jones 64 32& Bartlett 16 8 4Learning, 2 1 LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

Let’s try a few conversions. To convert a binary number to a decimal number, mul- tiply each digit in the binary number by 2position − 1, counting the rightmost position © Jones & Bartlett Learning,as positionLLC 1 and moving left through© Jones the &binary Bartlett number. Learning, Then add LLC the products NOT FOR SALE OR DISTRIBUTIONtogether. NOT FOR SALE OR DISTRIBUTION Using this method, let’s calculate the equivalent of the binary number 11010 in our decimal system.

4 3 2 1 0 ©11010 Jones = 1*2 & +Bartlett 1*2 + 0*2 Learning, + 1*2 + 0*2 LLC © Jones & Bartlett Learning, LLC = 16 + 8 + 0 + 2 + 0 NOT FOR= 26 SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

Now let’s examine how to convert a decimal number to a binary number. Let’s con- vert the decimal number 359 into its binary number equivalent. As we can see from © Jones & Bartlettthe way Learning, we rewrote LLC 11011, a binary number can© be Jones written &as Bartletta sum of powersLearning, of 2 LLC NOT FOR SALEwith OR coefficients DISTRIBUTION 0 and 1. NOT FOR SALE OR DISTRIBUTION The strategy to decompose a decimal number into a sum of powers of 2 is simple: first find the largest power of 2 that is smaller than or equal to the decimal number,

© Jones & Bartlett Learning, LLC© Jones & Bartlett Learning, LLC.© NOTJones FOR SALE& Bartlett OR DISTRIBUTION Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9781284239935_CH01_Print.indd 12 23/11/17 10:13 AM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

1.3 Data Representation 13

subtract that© number Jones from & Bartlett the decimal Learning, number, then LLC do the same with the remain© Jones- & Bartlett Learning, LLC der, and so on,NOT until FOR you reachSALE 0. OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION The largest power of 2 that is smaller than 359 is 256, or 28 (the next larger power of 2 would be 512, which is larger than 359). Subtracting 256 from 359 gives us 103 (359 − 256 = 103), so we now have © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC 8 NOT359 FOR = 2 *1 SALE + 103 OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION Now we apply the same procedure to 103. The largest power of 2 that is smaller than 103 is 64, or 26. That means that there is no factor for 27, so that digit’s value is 0. Subtracting 64 from 103 gives us 39. © Jones & BartlettNow Learning, we have LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 359 = 28*1 + 27*0 + 26*1 + 39 Repeating the procedure for 39, we find that the largest power of 2 smaller than 39 is 32 or 25. Subtracting 32 from 39 gives us 7. So we now have© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 359 = 28*1 + 27*0 + 26*1 + 25*1 + 7 Repeating the procedure for 7, the largest power of 2 smaller than 7 is 22, or 4. That means that there are no factors for 24 or 23, so the value for each of those digits is 0. © JonesSubtracting & Bartlett 4 from 7Learning, gives us 3, so LLC we have © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 359 = 28*1 + 27*0 + 26*1 + 25*1 + 24*0 + 23*0 + 22*1 + 3 Repeating the procedure for 3, the largest power of 2 smaller than 3 is 2, or 21, and we have:

© Jones & Bartlett359 Learning,= 28*1 + 27*0 LLC+ 26*1 + 25*1 + 24*0 + 23*0 + ©22*1 Jones + 21*1 +& 1 Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 1 is a power of 2; it is 20, so we finally have

359 = 28*1 + 27*0 + 26*1 + 25*1 + 24*0 + 23*0 + 22*1 + 21*1 + 20*1 Removing the power of 2 multipliers, 359 can be represented in the binary system as © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 359 = 28*1 + 27*0 + 26*1 + 25*1 + 24*0 + 23*0 + 22*1 + 21*1 + 20*1 = 1 0 1 1 0 0 1 1 1

or © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT1 FOR0110 0111 SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC© Jones & Bartlett Learning, LLC.© NOTJones FOR SALE& Bartlett OR DISTRIBUTION Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9781284239935_CH01_Print.indd 13 23/11/17 10:13 AM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

14 CHAPTER 1 Introduction to Programming and the Java Language

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOTCODE FOR IN SALE ACTION OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

To see a step-by-step demonstration of converting between decimal and binary numbers, look for the movie within the online resources. Click on the link to start the movie. © Hemera Technologies/ © Hemera Photos.com/Thinkstock © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

In a computer program, we will use both positive and negative numbers. ­Appendix D explains how negative numbers, such as −34, are represented in the binary © Jones & Bartlett Learning,system. LLC In a computer program,© we Jones also use & floating-pointBartlett Learning, numbers, LLC such as 3.75. Appendix E explains how floating-point numbers are represented using the binary NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION system.

1.3.2 Using Hexadecimal Numbers to Represent Binary Numbers

©As Jones you can & see,Bartlett binary Learning, numbers can LLC become rather long. With© only Jones two possible& Bartlett Learning, LLC NOTvalues, FOR 0 and SALE 1, it takes OR 16 DISTRIBUTION binary digits to represent the decimal NOTvalue +32,768.FOR SALE For OR DISTRIBUTION that reason, the hexadecimal, or base 16, system is often used as a shorthand repre- sentation of binary numbers. The hexadecimal system uses 16 digits: 0 to 9 and A to F. The letters A to F represent the values 10, 11, 12, 13, 14, and 15. © Jones & BartlettThe maximum Learning, value LLC that can be represented in© four Jones binary & digitsBartlett is 24 Learning,− 1, or 15. LLC NOT FOR SALEThe OR maximum DISTRIBUTION value of a hexadecimal digit is alsoNOT 15, FOR which SALE is represented OR DISTRIBUTION by the letter F. So you can reduce the size of a binary number by using hexadecimal digits to represent each group of four binary digits. Table 1.5 displays the hexadecimal digits along with their binary equivalents. © Jones & Bartlett Learning,To representLLC the following binary© numberJones in & hexadecimal, Bartlett Learning, you simply LLC substitute the NOT FOR SALE OR DISTRIBUTIONappropriate hex digit for each setNOT of four FOR binary SALE digits. OR DISTRIBUTION

0001 1010 1111 1001 1011 0011 1011 1110 1 A F 9 B 3 B E ©Here’s Jones an interesting& Bartlett sequence Learning, of hexadecimal LLC numbers. The first© Jones32 bits of & every Bartlett Learning, LLC NOTJava appletFOR are:SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

1100 1010 1111 1110 1011 1010 1011 1110 Translated into hexadecimal, that binary number becomes:

© Jones & BartlettCAFE BABE Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC© Jones & Bartlett Learning, LLC.© NOTJones FOR SALE& Bartlett OR DISTRIBUTION Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9781284239935_CH01_Print.indd 14 23/11/17 10:13 AM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

1.3 Data Representation 15

TABLE 1.5 Hexadecimal© Jones &Digits Bartlett and Equivalent Learning, Binary Values LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION Hex Digit Binary Value Hex Digit Binary Value 0 0000 8 1000 1 0001 9 1001 © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT2 FOR SALE OR DISTRIBUTION0010 A NOT1010 FOR SALE OR DISTRIBUTION 3 0011 B 1011 4 0100 1100 © Jones & Bartlett5 Learning, LLC0101 D © Jones & Bartlett1101 Learning, LLC NOT FOR SALE OR6 DISTRIBUTION0110 E NOT FOR SALE1110 OR DISTRIBUTION 7 0111 F 1111

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC 1.3.3 RepresentingNOT FOR Characters SALE with OR the DISTRIBUTION Unicode Character Set NOT FOR SALE OR DISTRIBUTION Java represents characters using the Unicode Worldwide Character Standard, or simply Unicode. Each Unicode character is represented as 16 bits, or two bytes. This means that the Unicode character set can encode 65,536 characters. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOTThe FOR Unicode SALE character OR DISTRIBUTION set was developed by the Unicode Consortium,NOT FOR which SALE con OR- DISTRIBUTION sists of computer manufacturers, software vendors, the governments of several nations, and others. The consortium’s goal was to support an international character set, including the printable characters on the standard QWERTY keyboard, as well as international characters such as é or λ. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE ManyOR DISTRIBUTION programming languages store charactersNOT using FOR the SALE ASCII OR(American DISTRIBUTION Stan- dard Code for Information Interchange) character set, which uses 7 bits to encode each character, and thus, can represent only 128 characters. For compatibility with the ASCII character set, the first 128 characters in the Unicode character set are the same as the ASCII© Jones character & Bartlett set. Learning, LLC © Jones & Bartlett Learning, LLC Table 1.6 showsNOT FOR a few SALE examples OR ofDISTRIBUTION Unicode characters and their decimalNOT FOR SALE OR DISTRIBUTION equivalents. For more information on the Unicode character set, see Appendix C or visit the Unicode Consortium’s website at http://www.Unicode.org. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC© Jones & Bartlett Learning, LLC.© NOTJones FOR SALE& Bartlett OR DISTRIBUTION Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9781284239935_CH01_Print.indd 15 23/11/17 10:13 AM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

16 CHAPTER 1 Introduction to Programming and the Java Language

© TABLEJones 1.6 &Selected Bartlett Unicode Learning, Characters LLC and Their Decimal Equivalents © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION Unicode Character Decimal Value Hex Value NUL, the null character (a nonprintable character) 0 0000 * 42 002A © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC 1 49 0031 NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 2 50 0032 A 65 0041 B 66 0042 © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTIONa NOT FOR SALE97 OR DISTRIBUTION0061 b 98 0062 } 125 007D © deleteJones (a nonprintable & Bartlett character) Learning, LLC 127 © Jones007F & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION Skill Practice with these end-of-chapter questions © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR1.7.1 DISTRIBUTIONMultiple Choice Exercises NOT FOR SALE OR DISTRIBUTION Questions 5, 6, 7, 8 1.7.2 Converting Numbers

© Jones & Bartlett Learning, LLC Questions 15, 16, 17,© 18,Jones 19, 20 & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION1.7.3 General Questions NOT FOR SALE OR DISTRIBUTION Questions 24, 25, 26

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT1.4 FORProgramming SALE OR Languages DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 1.4.1 High- and Low-Level Languages Programming languages can be categorized into three types:

© Jones & Bartlett ■■ Learning,machine language LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION ■■ assembly language

■■ high-level language

© Jones & Bartlett Learning, LLC© Jones & Bartlett Learning, LLC.© NOTJones FOR SALE& Bartlett OR DISTRIBUTION Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9781284239935_CH01_Print.indd 16 23/11/17 10:13 AM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

1.4 Programming Languages 17

In the early© days Jones of computing, & Bartlett programmers Learning, oftenLLC used machine language© Jones or & Bartlett Learning, LLC assembly language.NOT FOR Machine SALE language OR DISTRIBUTION uses binary codes, or strings of 0s andNOT 1s, FOR SALE OR DISTRIBUTION to execute the instruction set of the CPU and to refer to memory addresses. This method of programming is extremely challenging and time consuming. Also, the code written in machine language is not portable to other computer architectures © Jonesbecause & each Bartlett CPU hasLearning, its own set LLC of instructions. Machine language’s© Jones early & Bartlett popu- Learning, LLC NOTlarity FOR can SALE be attributed OR DISTRIBUTION largely to the fact that programmers NOThad no FOR other SALE choices. OR DISTRIBUTION However, programmers rarely use machine language today. Assembly languages are one step above machine language, using symbolic names for memory addresses and mnemonics for processor instructions—for example: © Jones & BartlettBEQ Learning, (branch if equal), LLC SW (store), or LW (load).© Jones An Assembler & Bartlett program Learning, converts LLC NOT FOR SALE theOR code DISTRIBUTION to machine language before it is executed.NOT FORLike machine SALE ORlanguage, DISTRIBUTION assem- bly languages are also CPU-dependent and are not portable among computers with different processors (for instance, between Intel and SPARC). Assembly language is easier to write than machine language but still requires a significant effort and, thus, is usually used© Jones only when & Bartlett the program Learning, requires LLCfeatures, such as direct hardware© Jones & Bartlett Learning, LLC access, that areNOT not FOR supported SALE by aOR high-level DISTRIBUTION language. NOT FOR SALE OR DISTRIBUTION High-level languages, such as Java, C++, Swift, PHP, and Python, are closer to the English language than they are to machine language, making them a lot easier to use for software development and more portable among CPU architectures. For this reason, © Jonesprogrammers & Bartlett have embraced Learning, high-level LLC languages for more and© more Jones applications. & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION Characteristics of high-level languages, such as Java, are

■■ The languages are highly symbolic. Programmers write instructions using keywords and special characters and use symbolic names for data.

© Jones & Bartlett Learning,■■ The languages LLC are somewhat portable© Jones (some more & Bartlett portable Learning, than others) LLC NOT FOR SALE OR DISTRIBUTIONamong different CPUs. NOT FOR SALE OR DISTRIBUTION

■■ Some high-level programming languages, such as C++, Java, and Python, can be used for general-purpose applications, and some high-level pro- gramming languages can be used for specialized applications, such as Swift for iPhone© Jones development & Bartlett and Learning,PHP for Internet LLC applications. © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION High-level languages are compiled, interpreted, or a combination of both. A pro- gram written in a compiled language, such as C++, is converted by a compiler into machine code, then the machine code is executed. © JonesBy contrast, & Bartlett a program Learning, written using LLC an interpreted language,© suchJones as , & Bartlett is read Learning, LLC NOTand FOR converted SALE to OR machine DISTRIBUTION code, line by line, at execution time.NOT Typically, FOR a SALEprogram OR DISTRIBUTION written in an interpreted language will run more slowly than its equivalent written in a compiled language.

© Jones & Bartlett Learning, LLC© Jones & Bartlett Learning, LLC.© NOTJones FOR SALE& Bartlett OR DISTRIBUTION Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9781284239935_CH01_Print.indd 17 23/11/17 10:13 AM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

18 CHAPTER 1 Introduction to Programming and the Java Language

©Java Jones uses a& combination Bartlett Learning, of a compiler LLC and an interpreter. A Java© Jonesprogram & is Bartlettfirst Learning, LLC NOTcompiled FOR into SALE processor-independent OR DISTRIBUTION byte codes, then the byteNOT code FORfile is SALEinter- OR DISTRIBUTION preted at run time by software called the Java Virtual Machine (JVM).

1.4.2 An Introduction to Object-Oriented Programming © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC Initial high-level languages, such as or C, were procedural. Typically, NOT FOR SALEprogrammers OR DISTRIBUTION wrote task-specific code in separateNOT procedures, FOR SALE or functions,OR DISTRIBUTION and invoked these procedures from other sections of the program in order to perform various tasks. The program’s data was generally shared among the procedures. © Jones & Bartlett Learning,In theLLC mid-1970s, the first object-oriented© Jones & programming Bartlett Learning, language, Smalltalk,LLC was NOT FOR SALE OR DISTRIBUTIONintroduced, enabling programmersNOT to writeFOR code SALE with OR a different DISTRIBUTION approach. Whereas procedures or functions dealt mainly with basic data types such as integers, real numbers, or single characters, Smalltalk provided the with a new tool: classes and objects of those classes. ©A Jones class enables & Bartlett the programmer Learning, to encapsulateLLC data and the functions© Jones to manipu & Bartlett- Learning, LLC NOTlate that FOR data SALE into one OR package. DISTRIBUTION A class essentially defines a template,NOT or model,FOR SALE from OR DISTRIBUTION which objects are created. Creating an object is called instantiation. Thus, objects are created—instantiated—according to the design of the class. A class could represent something in real life, such as a person. The class could have © Jones & Bartlettvarious Learning, attributes such LLC as, in the example of a ©“person” Jones class, & Bartlett a first name, Learning, a last LLC NOT FOR SALEname, OR and DISTRIBUTION an age. The class would also provide code,NOT called FOR methods SALE ,OR that DISTRIBUTIONallow the creator of the object to set and retrieve the values of the attributes. One big advantage to object-oriented programming is that well-written classes can be reused in new programs, thereby reducing future development time. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC Smalltalk was somewhat successful, but had a major deficiency: its syntax was unlike NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION any syntax already known by most programmers. Most programmers who knew the procedural C programming language were attracted by the object-oriented features of Smalltalk but were reluctant to use it because its syntax was so different from C’s syntax. C++ added object-oriented features to C, but also added complexity. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOTMeanwhile, FOR SALEthe Internet OR DISTRIBUTIONwas growing by leaps and bounds and NOTgaining FOR popularity SALE OR DISTRIBUTION daily. Web developers used HTML to develop webpages and soon felt the need to incorporate programming features not only on the server side, but also directly on the client side. Fortunately, Java appeared on the scene. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE1.4.3 OR TheDISTRIBUTION Java Language NOT FOR SALE OR DISTRIBUTION On May 23, 1995, Sun Microsystems introduced Java, originally named Oak, as a free, object-oriented language targeted at embedded applications for consumer

© Jones & Bartlett Learning, LLC© Jones & Bartlett Learning, LLC.© NOTJones FOR SALE& Bartlett OR DISTRIBUTION Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9781284239935_CH01_Print.indd 18 23/11/17 10:13 AM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

1.4 Programming Languages 19

devices. A Java© Jones Virtual &Machine Bartlett was Learning, incorporated LLC immediately into the Netscape© Jones & Bartlett Learning, LLC Navigator InternetNOT FOR browser, SALE and asOR the DISTRIBUTION Internet grew, small Java programs, knownNOT FOR SALE OR DISTRIBUTION as applets, began to appear on webpages in increasing numbers. Java syntax is basi- cally identical (with some minor exceptions) to that of C++, and soon program- mers all over the world started to realize the benefits of using Java. Those benefits © Jonesinclude & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC

NOT FOR■■ syntaxSALE identical OR DISTRIBUTION to that of C++, except that Java eliminatesNOT FOR some SALEof C++’s OR DISTRIBUTION more complex features

■■ object orientation © Jones & Bartlett Learning,■■ Internet-related LLC features, such as servlets,© Jones which &are Bartlett run by the Learning, web server LLC NOT FOR SALE OR DISTRIBUTION■■ an extensive library of classes that canNOT be reusedFOR SALEreadily, ORincluding DISTRIBUTION classes for providing a Graphical User Interface and Java Database Connectivity (JDBC) for communicating with a database

■■ portability among every platform that supports a Java Virtual Machine © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC ■■ built-in networking NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION ■■ open source availability of the Java Development Kit As we mentioned earlier, a Java program is first compiled into processor-independen­ t byte codes, then the byte codes are interpreted at run time by the Java Virtual Machine © Jones(JVM). & As Bartlett its name Learning, implies, the LLC JVM simulates a virtual processor© Jones with & Bartlettits own Learning, LLC NOTinstruction FOR SALE set, registers,OR DISTRIBUTION and instruction pointer. Thus, toNOT run a FOR Java program,SALE OR DISTRIBUTION we only need a JVM. Fortunately, JVMs are available on every major computing platform. Because Java programs are interpreted at run time, they typically run more slowly © Jones & Bartlettthan Learning, their C++ LLC counterparts. However, many© Jones platforms & Bartlett provide Java Learning, compilers LLC NOT FOR SALE thatOR convertDISTRIBUTION source code directly to machine NOTcode. ThisFOR results SALE in greaterOR DISTRIBUTION execution speed, but with an accompanying loss of portability. Just-in-Time (JIT) compilers are also available. These JITs compile code at run time so that subsequent execution of the same code runs much faster. Java programs© canJones be written & Bartlett as servlets, Learning, Android LLCapps, or applications. © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION Java servlets are invoked by the web server and run on the server, without being downloaded to the client. Typically, servlets dynamically generate web content by reading and writing to a database using JDBC (Java Database Connectivity). © JonesThe Android & Bartlett Open SourceLearning, project LLC has chosen Java to be the language© Jones for developing& Bartlett Learning, LLC NOTapps FOR for SALEAndroid OR devices, DISTRIBUTION such as cell phones, tablets, and smartNOT TVs. FOR SALE OR DISTRIBUTION Java applications run standalone on a client computer. In this text, we will write Java applications.

© Jones & Bartlett Learning, LLC© Jones & Bartlett Learning, LLC.© NOTJones FOR SALE& Bartlett OR DISTRIBUTION Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9781284239935_CH01_Print.indd 19 23/11/17 10:13 AM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

20 CHAPTER 1 Introduction to Programming and the Java Language

©Oracle Jones Corporation, & Bartlett Learning, which acquired LLC Sun Microsystems in© Jones January & 2010,Bartlett Learning, LLC NOT­prov idesFOR a valuableSALE OR Java DISTRIBUTION website (www.oracle.com/technetwork/javaNOT ),FOR which SALE has OR DISTRIBUTION information on using the prewritten classes, a tutorial on Java, and many more resources for the Java programmer. We will refer you to that site often in this text. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 1.5 An Introduction to Programming 1.5.1 Programming Basics

© Jones & Bartlett Learning,In manyLLC ways, programming is ©like Jones solving & a puzzle.Bartlett We Learning,have a task to LLC perform and NOT FOR SALE OR DISTRIBUTIONwe know the operations that a computerNOT FOR can SALE perform OR (input, DISTRIBUTION calculations, compar- isons, rearranging of items, and output). As programmers, our job is to decompose a task into individual, ordered steps of inputting, calculating, comparing, rearrang- ing, and outputting. ©For Jones example, & Bartlettsuppose our Learning, task is to find LLC the sum of two numbers.© First, Jones our program & Bartlett Learning, LLC NOTneeds FOR to read SALE (input) OR the DISTRIBUTION numbers into the computer. Next, ourNOT program FOR SALE needs OR DISTRIBUTION to add the two numbers together (calculate). Finally, our program needs to write (output) the sum. Notice that this program consists of steps, called instructions, which are performed © Jones & Bartlettin order Learning, (“First,” “Next,”LLC “Finally”). Performing© Jones operations & Bartlett in order, Learning, one after LLC NOT FOR SALEanother, OR DISTRIBUTION is called sequential processing. NOT FOR SALE OR DISTRIBUTION The order in which instructions are executed by the computer is critical in pro- gramming. You can’t calculate the sum of two numbers before you have read the two numbers, and you can’t output a sum before you have calculated it. Programming, © Jones & Bartlett Learning,therefore, LLC requires the programmer© Jones to specify & Bartlett the ordering Learning, of instructions, LLC which NOT FOR SALE OR DISTRIBUTIONis called the flow of control of NOTthe program. FOR SALE There areOR four DISTRIBUTION different ways that the flow of control can progress through a program: sequential execution, method call, selection, and looping. We’ve just seen sequential execution, and we’ll discuss the other types of flow of control in the next section. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOTBecause FOR getting SALE the OR flow DISTRIBUTION of control correct is essential to gettingNOT FOR a program SALE OR DISTRIBUTION to produce correct output, programmers use a tool called pseudocode (pro- nounced sue dough code) to help them design the flow of control before writing the code. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE1.5.2 OR ProgramDISTRIBUTION Design with Pseudocode NOT FOR SALE OR DISTRIBUTION Pseudocode, from pseudo, which means “appearing like,” is a method for express- ing a program’s order of instructions in the English language, rather than a

© Jones & Bartlett Learning, LLC© Jones & Bartlett Learning, LLC.© NOTJones FOR SALE& Bartlett OR DISTRIBUTION Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9781284239935_CH01_Print.indd 20 23/11/17 10:13 AM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

1.5 An Introduction to Programming 21

programming© language.Jones & In Bartlett this way, Learning,the programmer LLC can concentrate on designing© Jones & Bartlett Learning, LLC a program withoutNOT FOR also being SALE bogged OR downDISTRIBUTION in the syntax of the particular programNOT- FOR SALE OR DISTRIBUTION ming language. The pseudocode for calculating the sum of two numbers would look like Example 1.1.

© Jonesread first & Bartlettnumber Learning, LLC © Jones & Bartlett Learning, LLC read second number NOTset FOR total SALE to (first OR number DISTRIBUTION + second number) NOT FOR SALE OR DISTRIBUTION output total EXAMPLE 1.1 Pseudocode for Summing Two Numbers

© Jones & BartlettFortunately, Learning, the rulesLLC for writing pseudocode© are Jones not rigid. & Bartlett Essentially, Learning, you can use LLC NOT FOR SALE anyOR wording DISTRIBUTION that works for you. NOT FOR SALE OR DISTRIBUTION Let’s look at another example. Suppose our program needs to calculate the square root of a number. The instructions for calculating a square root are rather complex; fortu- nately, Java provides prewritten code that computes the square root of a number. The prewritten code© Jones is called & a methodBartlett, and Learning, our program LLC can execute that code by calling© Jones & Bartlett Learning, LLC the method.NOT As part FOR of the SALE method OR call, DISTRIBUTION we tell the method which number’s squareNOT FOR SALE OR DISTRIBUTION root we want to calculate. This is called passing an argument to the method. When the method finishes executing its instructions, control is passed back to our program just after the method call and our program can use the square root of the number © Jonescalculated & Bartlett by the method. Learning, This is LLCcalled returning a value. Another© Jones way &of Bartlettlooking Learning, LLC NOTat FORmethod SALE calls isOR to considerDISTRIBUTION what happens when we’re readingNOT a bookFOR and SALE find aOR DISTRIBUTION word we don’t understand. We mark our place in the book and look up the word in a dictionary. When we’re finished looking up the word, we go back to our place in the book and continue reading. © Jones & BartlettExample Learning, 1.2 shows LLC the pseudocode for calculating© Jones the square & Bartlett root of anLearning, integer. LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION read a number call the square root method, passing the number and receiving the square root output the square root of the number EXAMPLE 1.2 Using a Method Call to Calculate a Square Root © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC The order ofNOT operations FOR SALE is still OR input, DISTRIBUTION calculate, and output, but we’re callingNOT a FOR SALE OR DISTRIBUTION method to perform the calculation for us. Now suppose our task is to determine whether a number is positive or negative. First, our program should input the number into the computer. Next, we need to © Jonesdetermine & Bartlett whether Learning, the number LLC is positive or negative. We© knowJones that & numbersBartlett Learning, LLC NOTgreater FOR than SALE or equal OR DISTRIBUTIONto 0 are positive and numbers less thanNOT 0 are negative,FOR SALE so our OR DISTRIBUTION program should compare the number to 0. Finally, our program should write a mes- sage indicating whether the number is positive or negative.

© Jones & Bartlett Learning, LLC© Jones & Bartlett Learning, LLC.© NOTJones FOR SALE& Bartlett OR DISTRIBUTION Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9781284239935_CH01_Print.indd 21 23/11/17 10:13 AM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

22 CHAPTER 1 Introduction to Programming and the Java Language

©Like Jones Examples & Bartlett 1.1 and Learning,1.2, the operations LLC are input, calculate, and© Jones output, & in Bartlett that Learning, LLC NOTorder. FOR However, SALE depending OR DISTRIBUTION on whether the number is positive orNOT negative, FOR our SALE pro- OR DISTRIBUTION gram should write a different message. If the number is greater than or equal to 0, the program should write a message that the number is positive, but if the number is less than 0, the program should write a message that the number is negative. Code © Jones & Bartlettused to Learning, handle this situationLLC is called selection;© the Jones program & selectsBartlett which Learning, code to LLC NOT FOR SALEexecute OR basedDISTRIBUTION on the value of the data. NOT FOR SALE OR DISTRIBUTION The pseudocode for this program could be written as that shown in Example 1.3.

read a number © Jones & Bartlett Learning,if theLLC number is greater than or© equal Jones to 0 & Bartlett Learning, LLC write “Number is positive.” NOT FOR SALE OR DISTRIBUTIONelse NOT FOR SALE OR DISTRIBUTION write “Number is negative.” EXAMPLE 1.3 Using Selection

©N Jonesotice the & indentation Bartlett forLearning, the code that LLC will be selected based on© the Jones comparison & Bartlett of Learning, LLC NOTthe number FOR SALEwith 0. ProgrammersOR DISTRIBUTION use indentation to make it easierNOT to see FOR the flow SALE of OR DISTRIBUTION control of the program. Now let’s get a little more complicated. Suppose our program needs to find the sum of a group of numbers. This is called accumulating. To accomplish this, we can take © Jones & Bartlettthe same Learning, approach asLLC if we were adding a group© of Jones numbers & using Bartlett a calculator. Learning, We LLC NOT FOR SALEstart OR with DISTRIBUTION a total of 0 and add each number, one atNOT a time, FOR to the SALE running OR total. DISTRIBUTION When we have no more numbers to add, the running total is the total of all the numbers. Translating this into pseudocode, we get the code shown in Example 1.4.

© Jones & Bartlett Learning,set LLCtotal to 0 © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTIONread a number NOT FOR SALE OR DISTRIBUTION while there was a number to read, repeat next two instructions add number to total read the next number write total ©EXAMPLE Jones 1.4 & BartlettAccumulating Learning, a Total LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION The indented code will be repeated for each number read until there are no more numbers. This repeated execution of the same code is called looping, or iteration, and is used extensively in programming whenever the same processing needs to be © Jones & Bartlettperformed Learning, on each LLCitem in a set. © Jones & Bartlett Learning, LLC NOT FOR SALEAccumulating OR DISTRIBUTION a total and determining whetherNOT a numberFOR SALE is positive OR DISTRIBUTIONor nega- tive are just two of many commonly performed operations. In programming,

© Jones & Bartlett Learning, LLC© Jones & Bartlett Learning, LLC.© NOTJones FOR SALE& Bartlett OR DISTRIBUTION Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9781284239935_CH01_Print.indd 22 23/11/17 10:13 AM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

1.5 An Introduction to Programming 23

we will often© performJones &tasks Bartlett for which Learning, there are LLC standard methods of process© Jones- & Bartlett Learning, LLC ing, called algorithmsNOT FOR. ForSALE example, OR DISTRIBUTION the algorithm for accumulation is toNOT set a FOR SALE OR DISTRIBUTION total to 0, use looping to add each item to the total, then output the total. More generally, we can think of an algorithm as a strategy to solve a problem. Earlier in the chapter, we used an algorithm to convert a decimal number to its binary © Jonesrepresentation. & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOTOther FOR common SALE programming OR DISTRIBUTION tasks are counting items, calculatingNOT an FOR average, SALE sort -OR DISTRIBUTION ing items into order, and finding the minimum and maximum values. In this text, we will learn the standard algorithms for performing these common operations. Once we learn these algorithms, our programming job will become easier. When © Jones & Bartlettwe recognizeLearning, that LLC a program requires any of ©these Jones tasks, & we Bartlett can simply Learning, plug in the LLC NOT FOR SALE appropriateOR DISTRIBUTION algorithm with some minor modifications.NOT FOR SALE OR DISTRIBUTION Programming, in large part, is simply reducing a complex task to a set of subtasks that can be implemented by combining standard algorithms that use sequential pro- cessing, method calls, selection, and looping. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC The most difficultNOT partFOR of programming,SALE OR DISTRIBUTION however, is recognizing which algorithmsNOT to FORSOFTWARE SALE OR DISTRIBUTION apply to the problem at hand. This requires analytical skills and the ability to see pat- ENGINEERING terns. Throughout this text, we will point out common patterns wherever possible. TIP Looking for 1.5.3 Developing a Java Application patterns will © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning,help you LLC NOTWriting FOR SALE a Java applicationOR DISTRIBUTION consists of several steps: writingNOT the FOR code, SALE compil OR- determineDISTRIBUTION the appropriate ing the code, and executing the application. Java source code is stored in a text algorithms for file with the extension .java. Compiling the code creates one or more .class files, your programs. which contain processor-independent byte codes. The Java Virtual Machine © Jones & Bartlett(JVM) Learning, translates LLC the byte codes into machine-level© Jones instructions & Bartlett for Learning,the processor LLC NOT FOR SALE onOR which DISTRIBUTION the Java application is running. Thus,NOT ifFOR a Java SALE application OR DISTRIBUTION is running on an Intel i7 processor, the JVM translates the byte codes into the i7’s instruc- tion set. Oracle provides a Java SE Development Toolkit (JDK) on its website (www.oracle .com/technetwork/java© Jones), & which Bartlett is downloadable Learning, free LLC of charge. The JDK contains© Jones a & Bartlett Learning, LLC compiler, JVM,NOT and FOR jshell, SALE which ORis a toolDISTRIBUTION for immediate execution of Java code.NOT In FOR SALE OR DISTRIBUTION addition, the JDK contains a broad range of prewritten Java classes that program- mers can use in their Java applications. If you are downloading and installing Java yourself, be sure to follow the directions © Joneson the Oracle& Bartlett website, Learning, including theLLC directions for setting the path© Jones for javac & , Bartlettthe Java Learning, LLC NOTcompiler. FOR SALE You need OR to DISTRIBUTION set the path correctly so that you can NOTrun the FOR Java SALEcompiler OR DISTRIBUTION from any folder on your computer.

© Jones & Bartlett Learning, LLC© Jones & Bartlett Learning, LLC.© NOTJones FOR SALE& Bartlett OR DISTRIBUTION Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9781284239935_CH01_Print.indd 23 23/11/17 10:13 AM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

24 CHAPTER 1 Introduction to Programming and the Java Language

©To Jones develop & an Bartlett application Learning, using the JDK,LLC write the source code using© Jones any text & editor, Bartlett Learning, LLC NOTsuch asFOR Notepad SALE on ORWindows, DISTRIBUTION TextEdit on Macs, or the vi editor NOTon Linux. FOR To SALE com- OR DISTRIBUTION pile the code, invoke the compiler from the command line:

javac ClassName.java

© Jones & Bartlettwhere ClassName.javaLearning, LLC is the name of the source ©file. Jones & Bartlett Learning, LLC NOT FOR SALEIf our OR program, DISTRIBUTION written in the file ClassName.javaNOT, compiles FOR SALE correctly, OR a DISTRIBUTIONnew file, ClassName.class, will be created in our current directory. To run the application, we invoke the JVM from the command line: © Jones & Bartlett Learning,java LLC ClassName © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTIONTypically, programmers use anNOT Integrated FOR SALE Development OR DISTRIBUTION Environment (IDE) to develop applications rather than invoking the compiler and JVM from the com- mand line. An IDE consists of a program editor, a compiler, and a run-time envi- ronment, integrated via a Graphical User Interface. The advantage to using an IDE ©is Jones that errors & Bartlett in the Java Learning, code that areLLC found by the compiler or© theJones JVM &can Bartlett be Learning, LLC NOTlinked FOR directly SALE to the OR program DISTRIBUTION editor at the line in the source fileNOT that FOR caused SALE the OR DISTRIBUTION error. Additionally, the Graphical User Interface enables the programmer to switch among the editor, compiler, and execution of the program without launching sepa- rate applications. © Jones & BartlettSome ofLearning, the many available LLC IDEs include Eclipse© from Jones the Eclipse & Bartlett Foundation, Learning, Inc.; LLC NOT FOR SALEJGRASP, OR DISTRIBUTION developed at Auburn University; NetBeans,NOT FORdownloadable SALE OR from DISTRIBUTION Oracle; and TextPad from Helios Software Solutions. Some IDEs are freely available, while others require a software license fee.

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTIONSkill Practice with these end-of-chapter questions

1.7.1 Multiple Choice Exercises © Jones & BartlettQuestions Learning, 9, 10, 11, 12, 13, LLC 14 © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 1.7.3 General Questions Questions 27, 28, 29, 30 1.7.4 Technical Writing © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTIONQuestion 34 NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC© Jones & Bartlett Learning, LLC.© NOTJones FOR SALE& Bartlett OR DISTRIBUTION Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9781284239935_CH01_Print.indd 24 23/11/17 10:13 AM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

1.5 An Introduction to Programming 25

1.5.4 Programming© Jones Activity & Bartlett 1: Writing Learning, a First Java ApplicationLLC © Jones & Bartlett Learning, LLC Let’s create ourNOT first FOR Java program.SALE OR This DISTRIBUTION program prints the message, “ProgrammingNOT FOR SALE OR DISTRIBUTION is not a spectator sport!” Start by launching your IDE and open a new editor window. This is where you will © Joneswrite the & codeBartlett for the Learning, program. LLC © Jones & Bartlett Learning, LLC NOTBefore FOR you SALE type anyOR code, DISTRIBUTION however, let’s name the document. NOTYou do FOR this bySALE saving OR DISTRIBUTION the document as FirstProgram.java. Be sure to capitalize the F and the P and keep the other letters lowercase. Java is case-sensitive, so Java considers firstprogram.java or even Firstprogram.java to be a different name. Also, note that there is no space © Jones & Bartlettbetween Learning, First and LLC Program. © Jones & Bartlett Learning, LLC NOT FOR SALE KeepingOR DISTRIBUTION case sensitivity in mind, type in the programNOT FOR shown SALE in Example OR DISTRIBUTION 1.5.

1 // First program in Java 2 // Anderson, Franceschi 3 4 public class© JonesFirstProgram & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC 5 { NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 6 public static void main( String [ ] args ) 7 { 8 System.out.println( “Programming is not a spectator sport!” ); 9 © Jones10 System& Bartlett.exit( 0Learning, ); LLC © Jones & Bartlett Learning, LLC NOT11 FOR } SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 12 } EXAMPLE 1.5 A First Program in Java

© Jones & BartlettAt this Learning, point, we askLLC that you just type the program© Jones as you see& Bartlettit here, except Learning, for the line LLC NOT FOR SALE numbers,OR DISTRIBUTION which are not part of the program. LineNOT numbers FOR areSALE displayed OR inDISTRIBUTION this exam- ple to allow easy reference to a particular line in the code. We’ll explain a little about the program now; additional details will become clear as the semester progresses. The first two lines, which start with two forward slashes, are comments. They will COMMON ERROR not be compiled© Jones or executed; & Bartlett they are Learning, simply information LLC for the programmer© and Jones TRAP & Bartlett Learning, LLC Java is case- are used to increaseNOT FOR the readability SALE OR of theDISTRIBUTION program. NOT FORsensitive. SALE The OR DISTRIBUTION Line 4 defines the class name as FirstProgram. Notice that the class name must be spelled class name and the file exactly the same way—including capitalization—as the file name, FirstProgram.java. name must The curly braces in lines 5 and 12 mark the beginning and ending of the FirstPro- match exactly, © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning,including LLC gram class, and the curly braces in lines 7 and 11 mark the beginning and ending NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTIONcapitalization. of the main method. Every Java application must define a class and a main method. Execution of a Java application always begins with the code inside main. So when this application begins, it will execute line 8, which writes the message “Programming

© Jones & Bartlett Learning, LLC© Jones & Bartlett Learning, LLC.© NOTJones FOR SALE& Bartlett OR DISTRIBUTION Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9781284239935_CH01_Print.indd 25 23/11/17 10:13 AM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

26 CHAPTER 1 Introduction to Programming and the Java Language

©is Jones not a spectator & Bartlett sport! ”Learning, to the system LLC console. Next, it executes line© Jones 10, System.exit( & Bartlett Learning, LLC NOT0 ), which FOR exits SALE the program.OR DISTRIBUTION Including this line is optional; if youNOT omit FORthis line, SALE the OR DISTRIBUTION application will still exit normally. As you type the program, notice that your IDE automatically colors your text to help you distinguish comments, String literals (“Programming is not a spectator © Jones & Bartlettsport!”), Learning, Java class names LLC (String, System), and keywords© Jones (public, & Bartlett class, static Learning,), which LLC NOT FOR SALEare ORreserved DISTRIBUTION for specific uses in Java. Curly braces,NOT brackets, FOR and SALE parentheses, OR DISTRIBUTION which have syntactical meaning in Java, are usually displayed in color as well. Your IDE may use different colors than those shown in Example 1.5. When you have completed typing the code in Example 1.5, compile it. If everything © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC is typed correctly, the compiler will create a FirstProgram.class file, which contains NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION the byte codes for the program. If you received any compiler errors, check that you have entered the code exactly as it is written in Example 1.5. We give you tips on finding and fixing the errors in the ©next Jones section. & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOTIf you FOR got a cleanSALE compile OR DISTRIBUTION with no errors, congratulations! You’re readyNOT to FOR execute SALE the OR DISTRIBUTION application. This will invoke the JVM and pass it the First Program.class file created by the compiler. If all is well, you will see the message, Programming is not a spectator

Figure© Jones 1.4 & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC Programming is not a spectator sport! OutputNOT from FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION Example 1.5

sport!, displayed on the Java console, which is the text window that opens automati- cally. Figure 1.4 shows the correct output of the program. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTIONDebugging Techniques NOT FOR SALE OR DISTRIBUTION If the compiler found syntax errors in the code, these are called compiler errors, not because the compiler caused them, but because the compiler found them. When the ©compiler Jones detects & Bartlett errors inLearning, the code, it LLC writes diagnostic information© aboutJones the & errors. Bartlett Learning, LLC NOTFor example, FOR SALE try typing OR printlnDISTRIBUTION with a capital P (as Println), andNOT recompiling. FOR SALE The OR DISTRIBUTION compiler displays the following message:

FirstProgram.java:8: error: cannot find symbol System.out.Println( “Programming is not a spectator sport!” ); © Jones & Bartlett Learning,^ LLC © Jones & Bartlett Learning, LLC NOT FOR SALE symbol: OR DISTRIBUTION method Println(String) NOT FOR SALE OR DISTRIBUTION location: variable out of type PrintStream 1 error

© Jones & Bartlett Learning, LLC© Jones & Bartlett Learning, LLC.© NOTJones FOR SALE& Bartlett OR DISTRIBUTION Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9781284239935_CH01_Print.indd 26 23/11/17 10:13 AM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

1.5 An Introduction to Programming 27

The first line© identifies Jones &the Bartlett file name Learning, that contains LLC the Java source code, as well© Jones as & Bartlett Learning, LLC the line numberNOT in FOR the source SALE code OR where DISTRIBUTION the error occurred. In this case, the NOTerror FOR SALE OR DISTRIBUTION occurred on line 8. The second line displays line 8 from the source code with a caret (^) pointing to Println as being the cause of the error. The symbol and loca- tion information in the third and fourth lines indicate that the Println method is © Jonesunknown. & Bartlett Remember Learning, that Java is LLC case-sensitive, so println and© JonesPrintln are & Bartlettconsid- Learning, LLC NOTered FOR to beSALE different. OR AsDISTRIBUTION you gain experience with Java, theseNOT error FOR messages SALE will OR DISTRIBUTION become more meaningful to you. With most IDEs, double-clicking on the first line in the error message transfers you to the source code window with your cursor posi- tioned on line 8 so you can correct the error. © Jones & BartlettMany Learning, times, the LLCcompiler will find more than© oneJones error & in Bartlett the source Learning, code. When LLC NOT FOR SALE thatOR happens,DISTRIBUTION don’t panic. Often, a single problem,NOT FORsuch as SALE a missing OR semicolon DISTRIBUTION or curly brace, can cause multiple compiler errors. For example, after correcting the preceding error, try deleting the left curly brace in line 7, then recompiling. The compiler reports four errors: © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC FirstProgram.java:6:NOT FOR error: SALE ‘;’ expected OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION public static void main( String [ ] args ) ^ FirstProgram.java:10: error: expected System.exit( 0 ); © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC ^ NOTFirstProgram.java:10: FOR SALE OR DISTRIBUTIONerror: illegal start of type NOT FOR SALE OR DISTRIBUTION System.exit( 0 ); ^ FirstProgram.java:12: error: class, interface or enum expected © Jones & Bartlett} Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE ^OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 4 errors

As you can see, the compiler messages do not always report the problem exactly. SOFTWARE When you receive a compiler message, looking at the surrounding lines will often ENGINEERING help you find© the Jones error. Depending& Bartlett on Learning, your IDE, you LLC might see messages other© than Jones & Bartlett Learning, LLC TIP those shownNOT here becauseFOR SALE some IDEsOR DISTRIBUTIONattempt to interpret the error messages NOTfrom FORBecause SALE one OR DISTRIBUTION the compiler to provide more relevant information on the errors. syntax error can cause multiple It is sometimes easier to fix one error at a time and recompile after each fix, because compiler errors, the first fix might eliminate many of the reported errors. correct only the © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning,obvious errors LLC NOTWhen FOR all SALE the compiler OR DISTRIBUTION errors are corrected, you’re ready to executeNOT theFOR program. SALE OR DISTRIBUTIONand recompile It is possible to get a clean bill of health from the compiler, yet the program still after each correction. won’t run. To demonstrate this, try eliminating the brackets in line 6 after the word

© Jones & Bartlett Learning, LLC© Jones & Bartlett Learning, LLC.© NOTJones FOR SALE& Bartlett OR DISTRIBUTION Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9781284239935_CH01_Print.indd 27 23/11/17 10:13 AM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

28 CHAPTER 1 Introduction to Programming and the Java Language

©String Jones. If you & Bartlettthen compile Learning, the program, LLC no errors are reported. But© Joneswhen you & tryBartlett to Learning, LLC NOTrun the FOR program, SALE you OR get DISTRIBUTION a run-time error. NOT FOR SALE OR DISTRIBUTION Instead of Programming is not a spectator sport!, the following message is displayed on the Java console:

© Jones & BartlettError: Learning,Main method not LLC found in class FirstProgram,© Jonesplease define & Bartlett the main Learning, LLC NOT FOR SALEmethod OR as: DISTRIBUTION NOT FOR SALE OR DISTRIBUTION public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application

This means that the main method header (line 6) was not typed correctly. © Jones & Bartlett Learning,Thus, LLC we’ve seen that two types ©of Joneserrors can & occurBartlett while Learning, you are developing LLC a Java NOT FOR SALE OR DISTRIBUTIONprogram: compiler errors, whichNOT are FORusually SALE caused OR by languageDISTRIBUTION syntax errors or misspellings, and run-time errors, which are often caused by problems using the prewritten classes. Run-time errors can also be caused by exceptions that the JVM detects as it is running, such as an attempt to divide by zero. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOTTesting FOR Techniques SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION Once your program compiles cleanly and executes without run-time errors, you may be tempted to conclude that your job is finished. Far from it—you must also verify the results, or output, of the program. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALEIn theOR sample DISTRIBUTION program, it’s difficult to get incorrectNOT results—other FOR SALE than OR misspelling DISTRIBUTION the message or omitting the spaces between the words. But any nontrivial program should be tested thoroughly before declaring it production ready. To test a program, consider all the possible inputs and the corresponding correct © Jones & Bartlett Learning,outputs. LLC It often isn’t feasible to© test Jones every &possible Bartlett input, Learning, so programmers LLC usually NOT FOR SALE OR DISTRIBUTIONtest boundary conditions, whichNOT are FORthe values SALE that OR sit onDISTRIBUTION the boundaries of pro- ducing different output for a program. For example, to test the code that determines whether an integer is negative or non- negative, you would feed the program −1 and 0. These are the boundaries of nega- ©tive Jones and nonnegative & Bartlett integers. Learning, In other LLC words, the boundary between© Jones negative & Bartlett and Learning, LLC NOTnonnegative FOR SALE integers OR is between DISTRIBUTION −1 and 0. NOT FOR SALE OR DISTRIBUTION When a program does not produce the correct output, we say the program contains logic errors. By testing your program thoroughly, you can discover and correct most logic errors. Table 1.7 shows types of program errors and their usual causes. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALEWe’ll OR talk DISTRIBUTION more about testing techniques throughoutNOT the FOR text. SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC© Jones & Bartlett Learning, LLC.© NOTJones FOR SALE& Bartlett OR DISTRIBUTION Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9781284239935_CH01_Print.indd 28 23/11/17 10:13 AM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

1.5 An Introduction to Programming 29

TABLE 1.7 Types© Jones of Program & Bartlett Errors and Learning,Their Causes LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION Type of Error Usual Causes Compiler errors Incorrect language syntax or misspellings Run-time errors Incorrect use of classes © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC Logic errors Incorrect program design or incorrect implementation of the design NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

DISCUSSION QUESTIONS 1. In the Debugging Techniques section, we saw that making one typo could ? © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC generate several compiler errors. Why do you think that happens? NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 2. Explain why testing boundary conditions is an efficient way to verify a ­program’s correctness. 3. Did any errors occur while you were developing the first application? If so, explain© whether Jones they & were Bartlett compiler Learning, or run-time errors LLC and what you did to © Jones & Bartlett Learning, LLC fix them.NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

1.5.5 Making a JAR File © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOTAfter FOR we SALE finish writing,OR DISTRIBUTION compiling, and testing our application,NOT we FOR may SALE want to OR DISTRIBUTION distribute it. Java programs are typically distributed as JARs, which stands for Java ARchive. The JAR format allows the compression and aggregation of multiple files into one. The .jar extension is, by default, associated with the Java Virtual Machine. © Jones & BartlettThus, Learning, a user can double-clickLLC on a JAR file and© launch Jones its & corresponding Bartlett Learning, application LLC NOT FOR SALE directly.OR DISTRIBUTION Most IDEs include a tool to easily createNOT a JAR FOR file. SALE But you OR can DISTRIBUTIONalso create a JAR file from the command line. The general syntax to create a JAR file is:

jar cf jarfile inputfile(s)

■■ The c and f characters are options. The c option means that we want to cre- © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC ate a JAR file. The f option means that we want to send the output to a file. NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION ■■ jarfile is the name of the JAR file that we want to create; it can be a different name from the name of our Java file(s).

■■ inputfile(s) is a space-separated list of files that we want to include in the © Jones &JAR Bartlett file. It typicallyLearning, includes LLC the .class files and may include© Jones folders & Bartlett or files Learning, LLC NOT FOR containingSALE OR resources DISTRIBUTION used in our application, such asNOT images FOR or sounds. SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC© Jones & Bartlett Learning, LLC.© NOTJones FOR SALE& Bartlett OR DISTRIBUTION Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9781284239935_CH01_Print.indd 29 23/11/17 10:13 AM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

30 CHAPTER 1 Introduction to Programming and the Java Language

©We Jones can create & Bartlett a JAR file Learning, named Greeting.jar LLC from our FirstProgram© Jones application & Bartlett by Learning, LLC NOTtyping FOR the following SALE OR at the DISTRIBUTION command line: NOT FOR SALE OR DISTRIBUTION jar cf Greeting.jar FirstProgram.class Note that the input file is the .class file, not the .java file. Figure 1.5 shows the con- © Jones & Bartletttents of Learning, the folder after LLC executing the command ©above. Jones & Bartlett Learning, LLC NOT FOR SALEThe OR file Greeting.jarDISTRIBUTION is created in the current folder,NOT as shownFOR SALEin Figure OR 1.5. DISTRIBUTION If our application includes several Java files, we can use the wildcard character * to include all the corresponding .class files. The following creates a JAR file for all the .class files located in the current folder: © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTIONjar cf Greetings.jar *.class NOT FOR SALE OR DISTRIBUTION

Figure 1.5 The Folder After the JAR File Is © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC Created NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC© Jones & Bartlett Learning, LLC.© NOTJones FOR SALE& Bartlett OR DISTRIBUTION Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9781284239935_CH01_Print.indd 30 23/11/17 10:13 AM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

CHAPTER© Jones & Bartlett REVIEW Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

1.6 Chapter Summary

© Jones■■ &Basic Bartlett components Learning, of a computer LLC include the CPU, memory,© Jones a &hard Bartlett disk, Learning, LLC NOT FOR keyboard,SALE OR and DISTRIBUTION monitor. NOT FOR SALE OR DISTRIBUTION

■■ Each type of CPU has its own set of instructions for performing arithmetic and logical operations, moving data, and changing the order of execution of instructions. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION■■ An operating system controls peripheralNOT FOR devices, SALE supports OR multitasking,DISTRIBUTION allocates memory to programs, and prevents the user from damaging the system.

■■ Computer networks link two or more computers so that they can share resources,© Jones such &as Bartlettfiles or printers. Learning, LLC © Jones & Bartlett Learning, LLC

■■ TheNOT Internet FOR connects SALE millions OR DISTRIBUTION of computers around the world. Web NOTserv- FOR SALE OR DISTRIBUTION ers deliver webpages to clients running Internet browsers.

■■ Binary numbers are composed of bits. Each bit has the value 0 or 1. A byte holds eight binary digits. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR■■ ToSALE convert OR a binaryDISTRIBUTION number to a decimal number, multiplyNOT eachFOR digit SALE in the OR DISTRIBUTION binary number by 2position−1, counting the rightmost position as position 1 and moving left through the number. Then add the products together.

■■ To convert a decimal number into a binary number, first find the largest © Jones & Bartlett Learning,power of LLC 2 that is smaller than or equal© toJones the decimal & Bartlett number, Learning, subtract that LLC NOT FOR SALE OR DISTRIBUTIONnumber from the decimal number, thenNOT do FOR the sameSALE with OR the DISTRIBUTION remainder, and so on, until the decimal number reaches 0.

■■ Hexadecimal digits can be used to represent groups of four bits.

■■ The Unicode character set, which Java uses, can encode up to 65,536 char- © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC acters using 16 bits per character. NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION ■■ Machine language and assembly language are early forms of programming languages that require the programmer to write to the CPU’s instruction set. Because this low-level programming is time consuming and difficult, © Jones &and Bartlett the programs Learning, are not LLC portable to other CPU architectures,© Jones & machineBartlett Learning, LLC NOT FOR languageSALE OR and DISTRIBUTION assembly language are rarely used. NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC© Jones & Bartlett Learning, LLC.© NOTJones FOR SALE& Bartlett OR DISTRIBUTION Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9781284239935_CH01_Print.indd 31 23/11/17 10:13 AM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

32 CHAPTER 1 Introduction to Programming and the Java Language

© Jones■■ High-level & Bartlett languages Learning, are highly LLC symbolic and somewhat ©portable. Jones They & Bartlett can Learning, LLC NOT FORbe compiled,SALE OR interpreted, DISTRIBUTION or as in the case of Java, convertedNOT to FOR byte codes,SALE OR DISTRIBUTION which are interpreted at run time by the Java Virtual Machine.

■■ A good program is readable, easily maintainable, and reusable.

© Jones & Bartlett■■ Learning,Object-oriented LLC programming uses classes© Jones to encapsulate & Bartlett data Learning, and the LLC functions needed to manipulate that data. Objects are instantiated accord- NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION ing to the class design. An advantage to object-oriented programming is the ability to reuse classes.

■■ Programs use a combination of sequential processing, method calls, © Jones & Bartlett Learning, LLCselection, and iteration© to Jones control & the Bartlett order of Learning, execution of LLC instructions. NOT FOR SALE OR DISTRIBUTIONPerforming operationsNOT in order, FOR one SALE after OR another, DISTRIBUTION is called sequential processing. Temporarily executing other code, then returning, is called a method call. Selecting which code to execute based on the value of data is called selection. Repeating the same code on each item in a group of values © Jonesis &called Bartlett iteration, Learning, or looping. LLC © Jones & Bartlett Learning, LLC

NOT FOR■■ Pseudocode SALE OR allows DISTRIBUTION a programmer to design a programNOT without FOR worrying SALE OR DISTRIBUTION about the syntax of the language.

■■ In programming, we often perform tasks for which there are standard methods of processing, called algorithms. For example, accumulating is a © Jones & Bartlett Learning,common programming LLC operation that ©finds Jones the sum & Bartlett of a group Learning, of num- LLC NOT FOR SALE OR DISTRIBUTIONbers. NOT FOR SALE OR DISTRIBUTION

■■ Programming, in large part, is reducing a complex task to a set of sub- tasks that can be implemented by combining standard algorithms that use sequential processing, selection, and looping. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION■■ Java source code is storedNOT in a FORtext file SALE with an OR extension DISTRIBUTION of .java. Compiling the code produces one or more .class files.

■■ An Integrated Development Environment (IDE) consists of a program edi- tor, a compiler, and a run-time environment, integrated via a Graphical © JonesUser & BartlettInterface. Learning, LLC © Jones & Bartlett Learning, LLC

NOT FOR■■ Compiler SALE errors OR DISTRIBUTION are detected by the compiler and are NOT usually FOR caused SALE by OR DISTRIBUTION incorrect Java syntax or misspellings. Run-time errors are detected by the Java Virtual Machine and are usually caused by exceptions or incorrect use of classes. Logic errors occur during program execution and are caused by © Jones & Bartlett Learning,incorrect program LLC design. © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC© Jones & Bartlett Learning, LLC.© NOTJones FOR SALE& Bartlett OR DISTRIBUTION Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9781284239935_CH01_Print.indd 32 23/11/17 10:14 AM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

1.7 Exercises, Problems, and Projects 33

1.7 Exercises,© Jones Problems, & Bartlett and Projects Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 1.7.1 Multiple Choice Exercises 1. Which one of these is not an operating system? ❑❑ © Jones & BartlettLinux Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR ❑SALE❑ Java OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION ❑❑ Windows ❑❑ Android © Jones & Bartlett 2. Learning,Which one ofLLC these is not an application?© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION❑❑ Word NOT FOR SALE OR DISTRIBUTION ❑❑ Firefox ❑❑ Linux ❑❑ Excel© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 3. How many bits are in three bytes? ❑❑ 3 ❑❑ 8 © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC ❑❑ 24 NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION ❑❑ 0 4. In a network, the computers providing services to the other computers are called © Jones & Bartlett Learning,❑❑ clients. LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION ❑❑ servers. ❑❑ laptops. 5. A binary number ending with a 0 © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC ❑❑ isNOT even. FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION ❑❑ is odd. ❑❑ cannot tell.

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC© Jones & Bartlett Learning, LLC.© NOTJones FOR SALE& Bartlett OR DISTRIBUTION Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9781284239935_CH01_Print.indd 33 23/11/17 10:14 AM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

34 CHAPTER 1 Introduction to Programming and the Java Language

© Jones6. A binary & Bartlett number endingLearning, with a LLC1 © Jones & Bartlett Learning, LLC NOT FOR❑❑ isSALE even. OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION ❑❑ is odd. ❑❑ cannot tell. © Jones & Bartlett 7. A binarLearning,y number LLC ending with two 0s © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION ❑❑ is a multiple of 4. ❑❑ is not a multiple of 4. ❑❑ cannot tell. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION 8. Using four bits, the largest positiveNOT FOR binary SALE number OR we DISTRIBUTION can represent is 1111. ❑❑ true ❑❑ false 9. Which one of these is not a programming language? © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR❑❑ C++SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION ❑❑ Java ❑❑ Windows © Jones & Bartlett Learning,❑❑ Fortran LLC © Jones & Bartlett Learning, LLC NOT FOR SALE 10. ORWhich DISTRIBUTION one of these is not an object-orientedNOT programming FOR SALE language? OR DISTRIBUTION ❑❑ C ❑❑ Java © Jones & Bartlett Learning, LLC❑❑ C++ © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION❑❑ Smalltalk NOT FOR SALE OR DISTRIBUTION 11. What is the file extension for a Java source code file? ❑❑ .java © Jones❑ ❑&.exe Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION ❑❑ .class 12. What is the file extension of a compiled Java program? ❑❑ .java © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC ❑❑ NOT FOR SALE OR DISTRIBUTION.exe NOT FOR SALE OR DISTRIBUTION ❑❑ .class

© Jones & Bartlett Learning, LLC© Jones & Bartlett Learning, LLC.© NOTJones FOR SALE& Bartlett OR DISTRIBUTION Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9781284239935_CH01_Print.indd 34 23/11/17 10:14 AM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

1.7 Exercises, Problems, and Projects 35

13. In order© to Jones compile & a Bartlettprogram named Learning, Hello.java LLC, what do you type at the ©com- Jones & Bartlett Learning, LLC mand line?NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION ❑❑ java Hello ❑❑ java Hello.java © Jones &❑ Bartlett❑ javac He lloLearning, LLC © Jones & Bartlett Learning, LLC NOT FOR ❑SALE❑ javac ORHello.java DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 14. You have successfully compiled Hello.java into Hello.class. What do you type at the command line in order to run the application? © Jones & Bartlett Learning,❑❑ java He LLCllo.class © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION❑❑ java Hello NOT FOR SALE OR DISTRIBUTION ❑❑ javac Hello ❑❑ javac Hello.class © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 1.7.2 Converting Numbers 15. Convert the decimal number 67 into binary. 16. Convert the decimal number 1,564 into binary. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT 17. FOR Conv SALEert the ORbinary DISTRIBUTION number 0001 0101 into decimal. NOT FOR SALE OR DISTRIBUTION 18. Convert the binary number 1101 0101 0101 into decimal. 19. Convert the binary number 0001 0101 into hexadecimal. © Jones & Bartlett 20. Learning,Convert the LLChexadecimal number D8F into© Jones binary. & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 1.7.3 General Questions 21. A RAM chip is organized as × 8 memory, i.e., each unit contains 8 bits, or a byte. There© Jones are 7 address & Bartlett pins on Learning, the chip. How LLC many bytes does that memory© Jones & Bartlett Learning, LLC chip contain?NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 22. If a CPU is rated at 2.5 GHz, how many clock cycles per second are ­performed? 23. If a CPU’s clock cycles 2.6 billion times per second, what is the rating of the © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC CPU in MHz? NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC© Jones & Bartlett Learning, LLC.© NOTJones FOR SALE& Bartlett OR DISTRIBUTION Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9781284239935_CH01_Print.indd 35 23/11/17 10:14 AM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

36 CHAPTER 1 Introduction to Programming and the Java Language

© 24. Jones Suppose & Bartlett we are using Learning, binary encoding LLC to represent colors. For© Jonesexample, & a Bartlett Learning, LLC NOT blacFORk-and-white SALE OR color DISTRIBUTION system has only two colors and thereforeNOT needs FOR only SALE 1 OR DISTRIBUTION bit to encode the color system as follows: Bit Color

© Jones & Bartlett0 Learning,black LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR1 DISTRIBUTIONwhite NOT FOR SALE OR DISTRIBUTION With 2 bits, we can encode four colors as follows: Bit pattern Color © Jones & Bartlett Learning, LLC00 black © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION01 red NOT FOR SALE OR DISTRIBUTION 10 blue 11 white © JonesWith &5 bits, Bartlett how many Learning, colors can LLC we encode? © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION With n bits (n being a positive integer), how many colors can we encode? (Express your answer as a function of n.) 25. In HTML, a color can be coded in the following hexadecimal notation: © Jones & Bartlett#rrggbb Learning,, where LLC © Jones & Bartlett Learning, LLC NOT FOR SALE ORrr represents DISTRIBUTION the amount of red in the colorNOT FOR SALE OR DISTRIBUTION gg represents the amount of green in the color bb represents the amount of blue in the color © Jones & Bartlett Learning, LLCrr, gg, and bb vary between© 00 Jones and FF in& hexadecimalBartlett Learning, notation, i.e., LLC 0 and 255 NOT FOR SALE OR DISTRIBUTIONin decimal equivalent notation.NOT Give FOR the SALE decimal OR values DISTRIBUTION of the red, green, and blue values in the color #33AB12. 26. RGB is a color system representing colors: R stands for red, G for green, and B for blue. A color can be coded as rgb where r is a number between 0 and © Jones255 representing & Bartlett how Learning, much red LLCthere is in the color, g is a number© Jones between & Bartlett 0 Learning, LLC NOTand FOR 255 SALE representing OR DISTRIBUTION how much green there is in the color, andNOT b is FOR a number SALE OR DISTRIBUTION between 0 and 255 representing how much blue there is in the color. The color gray is created by using the same value for r, g, and b. How many shades of gray are there? © Jones & Bartlett 27. List Learning, three benefits LLC of the Java programming© language. Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC© Jones & Bartlett Learning, LLC.© NOTJones FOR SALE& Bartlett OR DISTRIBUTION Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9781284239935_CH01_Print.indd 36 23/11/17 10:14 AM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

1.7 Exercises, Problems, and Projects 37

28. What is© the Jones name of& the Bartlett Java compiler? Learning, LLC © Jones & Bartlett Learning, LLC 29. Write theNOT pseudocode FOR SALE for a program OR DISTRIBUTION that finds the product of two numbers.NOT FOR SALE OR DISTRIBUTION 30. Write the pseudocode for a program that finds the sums of the numbers input that are greater than or equal to 10 and the numbers input that are less © Jonesthan & 10.Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 1.7.4 Technical Writing 31. List the benefits of having a Local Area Network versus standalone computer systems. © Jones & Bartlett 32. Learning,For one day, LLCkeep a diary of the computer© applications Jones & Bartlettthat you use. Learning, Also note LLC NOT FOR SALE OR anyDISTRIBUTION features of the applications that you thinkNOT shouldFOR SALEbe improved OR DISTRIBUTIONor any fea- tures you’d like to see added. 33. You are looking at two computers with the following specifications, everything else being© Jonesequal: & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC PC # 1 NOT FOR SALEPC #OR 2 DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 3.4-GHz CPU 3.3-GHz CPU 16 MB L2 cache 16 MB L2 cache © Jones1 GB& BartlettRAM Learning,4 GB LLC RAM © Jones & Bartlett Learning, LLC NOT FOR1 TB SALE hard drive OR DISTRIBUTION1 TB hard drive NOT FOR SALE OR DISTRIBUTION $399 $399 Which PC would you buy? Explain the reasoning behind your selection. © Jones & Bartlett 34. Learning,Go to Oracle LLC’s Java site (www.oracle.com/technetwork/java© Jones & Bartlett). Explain Learning, what LLC NOT FOR SALE OR resourcesDISTRIBUTION are available there for someoneNOT who wantsFOR toSALE learn Java.OR DISTRIBUTION

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC© Jones & Bartlett Learning, LLC.© NOTJones FOR SALE& Bartlett OR DISTRIBUTION Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9781284239935_CH01_Print.indd 37 23/11/17 10:14 AM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

38 CHAPTER 1 Introduction to Programming and the Java Language

©1.7.5 Jones Group & Bartlett Project (for Learning, a group of 1, LLC 2, or 3 students) © Jones & Bartlett Learning, LLC NOT 35. InFOR the octalSALE system OR (baseDISTRIBUTION 8), numbers are represented using NOTdigits fromFOR 0 SALEto OR DISTRIBUTION 7; a 0 is placed in front of the octal number to indicate that the octal system is being used. For instance, here are some examples of the equivalent of some octal numbers in the decimal system: © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OROctal DISTRIBUTION Decimal NOT FOR SALE OR DISTRIBUTION 000 0 001 1

© Jones & Bartlett Learning, LLC007 7 © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION010 8 NOT FOR SALE OR DISTRIBUTION 011 9 In the hexadecimal system, numbers are represented using digits from 0 to 9 and letters A to F; 0x is placed in front of the hexadecimal number to indicate © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC that the hexadecimal system is being used. For instance, here are some exam- NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION ples of the decimal equivalents of some hexadecimal numbers: Hexadecimal Decimal 0x0 0 © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC 0x1 1 NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 0x9 9 0xA 10 0xB 11 © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION0xF 15 NOT FOR SALE OR DISTRIBUTION 0x10 16 0x11 17

© Jones0x1C & Bartlett 28Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR1. Conv SALEert 0xC3E OR (inDISTRIBUTION hexadecimal notation) into an octal NOTnumber. FOR SALE OR DISTRIBUTION 2. Convert 0377 (in octal notation) into a hexadecimal number. 3. Discuss how, in general, you would convert a hexadecimal number into an octal number and an octal number into a hexadecimal number. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

© Jones & Bartlett Learning, LLC© Jones & Bartlett Learning, LLC.© NOTJones FOR SALE& Bartlett OR DISTRIBUTION Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION

9781284239935_CH01_Print.indd 38 23/11/17 10:14 AM