Programming Building Blocks—Java Basics
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 2 © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE ProgrammingOR DISTRIBUTION BuildingNOT FOR SALE OR DISTRIBUTION Blocks—Java Basics © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION CHAPTER CONTENTS Introduction 2.3.5 Integer Division and Modulus © Jones2.1 Java & Bartlett Application Learning, Structure LLC 2.3.6© JonesDivisio n& by Bartlett Zero Learning, LLC NOT2.2 FOR Data SALE Types, OR Variables, DISTRIBUTION and Constants 2.3.7NOT Mixed-T FOR ypeSALE Arithmetic OR DISTRIBUTION and Type 2.2.1 Declaring Variables Casting 2.2.2 Integer Data Types 2.3.8 Shortcut Operators 2.2.3 Floating-Point Data Types 2.4 Programming Activity 2: Temperature © Jones & Bartlett Learning,2.2.4 Charac LLCter Data Type © Jones & BartlettConversion Learning, LLC NOT FOR SALE OR DISTRIBUTION2.2.5 Boolean Data Type NOT FOR2.5 ChapSALEter ORSummary DISTRIBUTION 2.2.6 The Assignment Operator, Initial 2.6 Exercises, Problems, and Projects Values, and Literals 2.6.1 Multiple Choice Exercises 2.2.7 String Literals and Escape Sequences 2.6.2 Reading and Understanding Code 2.2.8 ©Cons Jonestants & Bartlett Learning, LLC 2.6.3 Fill In the Code© Jones & Bartlett Learning, LLC 2.3 ExpresNOTsions andFOR Arithmetic SALE OR Operators DISTRIBUTION 2.6.4 Identifying ErrorsNOT in FOR Code SALE OR DISTRIBUTION 2.3.1 The Assignment Operator and 2.6.5 Debugging Area—Using Messages Expressions from the Java Compiler and Java 2.3.2 Arithmetic Operators JVM © Jones 2.3.3 & Bartlett Operat Learning,or Precedence LLC 2.6.6© JonesWrite a &Short Bartlett Program Learning, LLC NOT FOR2.3.4 SALE Prog ORramming DISTRIBUTION Activity 1: 2.6.7NOT Progra FORmming SALE Projects OR DISTRIBUTION Converting Inches to 2.6.8 Technical Writing Centimeters © 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_CH02_Print.indd 39 23/11/17 10:36 AM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 40 CHAPTER 2 Programming Building Blocks—Java Basics ©Introduction Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION If we boil it down to the basics, a program has two elements: instructions and data. The instructions tell the CPU what to do with the data. Typically, a program’s struc- ture will consist of the following operations: © Jones & Bartlett 1. Inpu Learning,t the data. LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 2. Perform some processing on the data. 3. Output the results. The data used by a program can come from a variety of sources. The user can enter data © Jones & Bartlett Learning,from LLC the keyboard, for example, ©when Jones we type & aBartlett new document Learning, into a word LLC processor. NOT FOR SALE OR DISTRIBUTIONThe program can read the data fromNOT a file,FOR for SALEexample, OR when DISTRIBUTION we load an existing doc- ument into the word processor. Or the program can generate the data randomly, for example, when a computer card game deals hands. Finally, some data is already known; for example, the number of hours in a day is 24, the number of days in December is 31, ©and Jones the value & Bartlettof pi is 3.14159. Learning, This type LLC of data is constant. The Java ©language Jones provides & Bartlett a Learning, LLC NOTsyntax FOR for describing SALE OR a program’s DISTRIBUTION data using keywords, symbolic names,NOT and FOR data SALEtypes. OR DISTRIBUTION Although the data may be different in each execution of the program, the instruc- tions stay the same. In a word processor, the words (data) are different from docu- ment to document, but the operation (instructions) of the word processor remains © Jones & Bartlettthe same. Learning, When a line LLC becomes full, for example,© theJones word &processor Bartlett automatically Learning, LLC NOT FOR SALEwraps OR to DISTRIBUTION the next line. It doesn’t matter which wordsNOT FORare on SALEthe line, OR only DISTRIBUTION that the line is full. When we select a word and change the font to bold, it doesn’t matter which word we select; it will become bold. Thus, a program’s instructions (its algo- rithm) must be written to correctly handle any data it may receive. © Jones & Bartlett Learning,We LLCwill write our programs by translating© Jones our& Bartlett algorithms Learning, into the basic LLC operations NOT FOR SALE OR DISTRIBUTIONthat the computer can perform:NOT input FOR and outputSALE of OR data DISTRIBUTION and various operations related to processing data, such as arithmetic calculations, comparisons of data and subsequent changes to the flow of control, and movement of data from one location in memory to another. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC In this chapter, we’ll look at basic Java syntax for defining the data to be used in the NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION program, performing calculations on that data, and outputting program results to the screen. © Jones & Bartlett2.1 Learning,Java Application LLC Structure © Jones & Bartlett Learning, LLC NOT FOR SALEEvery OR Java DISTRIBUTION program consists of at least one class. ItNOT is impossible FOR SALEto write ORa Java DISTRIBUTION program that doesn’t use classes. Classes describe a logical entity that has data as well as meth- ods (the instructions) to manipulate that data. An object is a physical instantiation of © 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_CH02_Print.indd 40 23/11/17 10:36 AM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 2.1 Java Application Structure 41 the class that© contains Jones specific & Bartlett data. InLearning, Example 2.1 LLC we provide a shell that contains© Jones & Bartlett Learning, LLC the basic formatNOT of FORa Java applicationSALE OR with DISTRIBUTION a class name of ShellApplication. Our sourceNOT FOR SALE OR DISTRIBUTION code will use this format, changing the class name as appropriate. 1 /* An application shell 2 Anderson, Franceschi © Jones 3 */ & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT 4 FORpublic SALE class ShellApplication OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 5 { 6 public static void main( String [ ] args ) // required 7 { 8 // write your code here © Jones & Bartlett 9 Learning, } LLC © Jones & Bartlett Learning, LLC NOT FOR SALE 10OR } DISTRIBUTION NOT FOR SALE OR DISTRIBUTION EXAMPLE 2.1 A Shell for a Java Application In Example 2.1, the numbers to the left of each line are not part of the program © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC code; they are included here for our convenience. IDEs typically allow us to display NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION line numbers. From application to application, the name of the class, ShellApplication, will change, because we will want to name our class something meaningful that reflects its func- © Jonestion. Each & Bartlett Java source Learning, code file must LLC have the same name as ©the Jones class name & Bartlett with a Learning, LLC NOT.java FOR extension. SALE InOR this DISTRIBUTION case, the source file must be ShellApplication.javaNOT FOR. Whatever SALE OR DISTRIBUTION name we select for a class must comply with the Java syntax for identifiers. Java identifiers are symbolic names that we assign to classes, methods, and data. Identifiers must start with a Java letter and may contain any combination of letters © Jones & Bartlettand Learning,digits, but no LLC spaces. A Java letter is any ©character Jones in & the Bartlett range a–z Learning, or A–Z, the LLC NOT FOR SALE underscoreOR DISTRIBUTION (_), or the dollar sign ($), as wellNOT as many FOR Unicode SALE characters OR DISTRIBUTION that are used as letters in other languages. Digits are any character between 0 and 9. The length of an identifier is essentially unlimited. Identifier names are case sensitive, so Number1 and number1 are considered to be different identifiers. In addition, ©none Jones of Java’s & reservedBartlett words Learning, can be usedLLC as identifiers. These reserved© Jones & Bartlett Learning, LLC words, whichNOT are listed FOR in SALEAppendix OR A, DISTRIBUTIONconsist of keywords used in Java instructions,NOT FOR SALE OR DISTRIBUTION as well as three special data values: true, false, and null. As of Java 9, the single under- score (_) is a reserved word and cannot be used as an identifier. Given that Java iden- tifiers are case sensitive, note that it is legal to use True or TRUE as identifiers, but © Jonestrue is not& Bartlett a legal variable Learning, name. TableLLC 2.1 lists the rules for creating© Jones Java identifiers.& Bartlett Learning, LLC NOTThe FOR shell SALE code in OR Example DISTRIBUTION 2.1 uses four identifiers: ShellApplicationNOT FOR, main SALE, String OR, DISTRIBUTION and args. The remainder of Example 2.1 consists of comments, Java keywords, and required punctuation. © 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_CH02_Print.indd 41 23/11/17 10:36 AM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 42 CHAPTER 2 Programming Building Blocks—Java Basics SOFTWARE ©The Jones basic &building Bartlett block Learning, of a Java program LLC is the statement.