Introduction to Programming and the Java Language
Total Page:16
File Type:pdf, Size:1020Kb
© 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 programmers, 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