# Table of Contents

 Foreword 1 Terminology 2 The HelloWorld Program 4 Section 1 - Creating and Running the HelloWorld Program 4 Section 2 - Analyzing the HelloWorld Program 5 Short Problems 6 Comments and Indentation 8 Section 1 - Defining Java-style Comments 8 Section 2 - Defining C-style Comments 9 Section 3 - Indentation 10 Short Problems 12 Types 13 Section 1 - Variables 13 Section 2 - Types 14 Section 3 - Primitive Types 14 Section 3a - The Integer Types 14 Section 3b - The Floating Point Types 16 Section 3c - The char Type 16 Section 3d - The boolean Type 17 Section 4 - Strings 17 Short Problems 17 Literals 17 Section 1 - Integer Literals 18 Section 1a - Integer Literals in Different Numeric Bases 18 Section 2 - Floating Point Literals 18 Section 3 - Boolean Literals 19 Section 4 - Character Literals 19 Section 5 - String Literals 19 Short Problems 20 println and print 21 Section 1 - Creating Longer String Literals 21 Section 2 - The Newline Character ‘\n’ 22 Section 3 - The print Method 22 Section 4 - The Tab Character ‘\t’ 23 Short Problems 24 Long Problems 24 Variables 26 Section 1 - Keywords 26 Section 2 - Identifiers 26 Section 3 - Variable Declarations 27 Section 3a - Declaring Multiple Variables in a Single Statement 28 Section 4- Using Variable Declarations 29 Section 5- The Lifetime of a Variable 30 Section 6- String Variables 30 Short Problems 31 Long Problems 32 Constants 34 Section 1 - Using the final Keyword to Define Constants 35 Section 2 - Constants Make your Programs Easier to Maintain 35 Section 3 - String Constants 36 Short Problems 36 Long Problems 37 Expressions 38 Section 1 - Assignment Statements 38 Section 2 - Expressions Defined 39 Section 3 - Arithmetic Expressions 39 Section 3a - Division 39 Section 3b - Remainder 40 Section 3c - Parentheses 40 Section 3d - Order of Operations 40 Section 3e - The Unary -, ++, and -- Operators 41 Section 3f - Prefix and Postfix Form 41 Section 3g - Shorthand 42 Section 4 - Boolean Expressions 42 Section 4a - Equality 43 Section 4b - Relational Operators 43 Section 4c - Logical Operators 44 Section 5 - Bitwise Expressions 45 Section 5a - Shifting 45 Section 6 - Concatenation 46 Section 7 - String Equality 47 Short Problems 48 Long Problems 49 Conversion 51 Section 1 - Widening Conversions 51 Section 2 - Narrowing Conversions 52 Section 3 - Conversions involving char 52 Section 4 - Conversions involving boolean 53 Section 5 - Conversions involving String 53 Section 6 - Promotion 54 Section 7 - Typecasting 54 Short Problems 55 if Statements 56 Section 1 - The if Statement 56 Section 2 - The if-else Statement 57 Section 3 - The if-else if Statement 58 Section 4 - Statement Blocks 60 Section 5 - Nested if Statements 61 Short Problems 63 Long Problems 63 switch Statements 66 Section 1 - switch Statements 66 Section 2 - Execution Flow in a switch Statement 67 Section 3 - The Default Case 67 Section 4 - Fallthrough in switch Statements 68 Section 5 - Sharing Statement Blocks 69 Section 6 - Switching on Strings 69 Short Problems 70 Long Problems 71 Loops 73 Section 1 - while Loops 73 Section 2 - do-while Loops 75 Section 3 - for Loops 76 Section 3a - Decrementing for Loops 77 Section 3b - Computing a Sum with a for Loop 77 Section 3c - More Complex Exit Conditions 77 Section 3d - Performing Larger Increments per Iteration 78 Section 3e - Nested for Loops 78 Short Problems 79 Long Problems 80 Methods 81 Section 1 - Writing Your Own Methods 81 Section 2 - Method Definition 82 Section 3 - Calling a Method 83 Section 4 - Method Parameters 84 Section 5 - Passing by Value 85 Section 6 - Returning a Value from a Method 86 Section 7 - Premature Returns 88 Section 8 - Return Statements in void Methods 88 Short Problems 89 Long Problems 89 Local Variables 92 Section 1 - Variables Local to Methods 92 Section 2 - Variables Local to if Clauses 93 Section 3 - Variables Local to Loops 94 Section 4 - Variables Local to Arbitrary Scopes 95 Short Problems 96 Long Problems 97 Classes 99 Section 1 - Classes Defined 99 Section 2 - Defining Your Own Classes 101 Section 3 - The Reason We Need Classes 101 Section 4 - The State of a Class 102 Section 5 - Class Fields 103 Section 5a - Determining the Fields of a Class 106 Section 5b - Default Initialization of Class Fields 107 Section 6 - Class Methods 107 Section 7 - Mutators 109 Section 7a - Validation in Mutators 111 Short Problems 112 Long Problems 113 Objects and References 116 Section 1 - Objects Defined 116 Section 2 - Object Declarations 116 Section 3 - The new Operator 117 Section 4 - Reference Variables 117 Section 5 - Multiple References to the Same Object 118 Section 6 - String Objects are Immutable 119 Section 7 - The Java Application Programming Interface (API) 119 Section 8 - The Object Class 120 Section 8a - The toString Method 120 Section 8b - The equals Method 121 Short Problems 122 Long Problems 122 Encapsulation 124 Section 1 - Encapsulation Defined 125 Section 2 - Accessors 126 Short Problems 128 Long Problems 128 Constructors 130 Section 1 - Constructors Defined 130 Section 2 - Calling a Constructor 130 Section 3 - Overloading Constructors 131 Section 4 - The Default Constructor 131 Section 5 - Default Constructor Generation 132 Section 6 - Constructors with no Arguments 132 Section 7 - The this Reference 133 Section 8 - Using this to Call Another Constructor 134 Section 9 - Constructors and final Fields 134 Short Problems 136 Long Problems 137 Static Fields and Methods 139 Section 1 - Static Fields 140 Section 2 - Static Field Declarations 140 Section 3 - Static Field Initialization 141 Section 4 - Implementing an Object Counter using a Static Field 142 Section 5 - Static versus Instance Fields 143 Section 6 - Defining Class Constants using Static Fields 143 Section 7 - Static Methods 145 Section 8 - Referencing Static Fields and Methods from Outside Defining Classes 146 Section 9 - Helper Classes 147 Short Problems 148 Long Problems 148 Passing and Returning Object References 151 Section 1- Passing an Object Reference to a Method 151 Section 2- Modifying the Fields of a Object Passed into a Method 152 Section 3- Returning an Object Reference from a Method 153 Short Problems 155 Long Problems 155 Arrays 159 Section 1 - Arrays Defined 159 Section 2 - Array Declarations 160 Section 3 - Array Subscripts 160 Section 4 - The Bounds of an Array 161 Section 5 - Arrays are Objects 161 Section 6 - Array Initialization 162 Section 7 - Arrays and Loops 162 Section 8 - The length Field 162 Section 9 - Array References as Class Fields 164 Section 10 - The Size of a Java Array is Constant 166 Section 11 - Enhanced For Loops 167 Section 12 - Two Dimensional Arrays 168 Short Problems 170 Long Problems 170 Arrays of Object References 174 Section 1 - Declaring an Array of Object References 174 Section 2 - Populating an Array of Object References using Assign Statements 176 Section 3 - Populating an Array of Object References using an Initialization List 177 Section 4 - Declaring Arrays of String References 178 Section 5 - Passing an Array of Object References to a Method 179 Section 6 - Returning an Array of Object References from a Method 181 Short Problems 182 Long Problems 182 Wrapper Classes 186 Section 1 - The Integer Class 186 Section 1a - The parseInt Method 186 Section 1b - The parseUnsignedInt Method 187 Section 1c - The toString Methods 187 Section 1d - Displaying in Different Numeric Bases 187 Section 1e - Converting to a Different Primitive Type 188 Section 1f - The MIN_VALUE and MAX_VALUE Constants 189 Section 1g - The min and max Methods 190 Section 1h - The toUnsignedLong Method 190 Section 2 - The Double Class 191 Section 2a - The POSITIVE_INFINITY, NEGATIVE_INFINITY, and NaN Constants 191 Section 2b - The isInfinite, isFinite, and isNaN Methods 191 Section 3 - The Float, Long, Short, and Byte Classes 192 Section 4 - The Boolean Class 193 Section 5 - Autoboxing 193 Section 6 - Unboxing 194 Section 7 - Wrapper Objects are Immutable 194 Section 8 - The Character Class 195 Short Problems 200 Long Problems 200 ArrayLists 203 Section 1 - ArrayList Definition 203 Section 2 - ArrayList Declarations 203 Section 3 - ArrayLists Contain Object References 204 Section 4 - Adding Elements to the End of an ArrayList 204 Section 5 - Retrieving Elements from an ArrayList 204 Section 6 - Looping Through an ArrayList 204 Section 7 - import Statements 205 Section 8 - Autoboxing and Unboxing with ArrayLists 206 Section 9 - Adding Elements to an ArrayList at a Specified Index 206 Section 10 - Removing Elements from an ArrayList 207 Section 11 - Replacing an Element at a Specified Index 207 Section 12 - Finding a Value in an ArrayList 208 Section 13 - Determining if an ArrayList is Empty 208 Section 14 - Copying ArrayLists 208 Section 15 - Clearing an ArrayList 209 Section 16 - Converting an ArrayList to a Java Array 209 Section 17 - Passing an ArrayList to a Method 210 Section 18 - Returning an ArrayList from a Method 210 Short Problems 211 Long Problems 211 User Interaction 215 Section 1 - System.in 216 Section 2 - The Scanner Class 216 Section 3 - Prompting 217 Section 4 - The nextDouble, nextFloat, nextLong, nextShort, nextByte, and nextBoolean Methods 218 Section 5 - Delimiters and the next Method 219 Section 6 - The nextLine Method 220 Section 7 - Scanner Use inside While Loops 222 Section 8 - Using a Scanner to Play a Game 223 Section 9 - Using a Scanner to Read a File 224 Section 9a - The hasNext Method 225 Section 10 - Using a Scanner to Read a String 226 Section 11 - Changing a Scanner’s Delimiter 227 Short Problems 228 Long Problems 228 String Operations 232 Section 1 - Obtaining the Length of a String 232 Section 2 - Retrieving the Character at an Index in a String Object 232 Section 3 - Retrieving the Last Character of a String Object 233 Section 4 - Finding the Position of a Character or String in a String Object 233 Section 5 - Retrieving a Portion of a String Object 235 Section 6 - The startsWith and endsWith Methods 236 Section 7 - The isEmpty Method 236 Section 8 - The toUpperCase and toLowerCase Methods 236 Section 9 - The valueOf Methods 237 Section 10 - The trim Method 238 Section 11 - Matching a Region of two Strings 238 Section 12 - Comparing two String Objects Lexicographically 238 Section 13 - Strings and char Arrays 240 Section 14 - Replacing a Portion of a String Object 240 Section 15 - The StringBuilder Class 241 Section 16 - The StringTokenizer Class 245 Short Problems 247 Long Problems 248 Random Numbers 251 Section 1 - The Random Class 251 Section 2 - Limiting Random Numbers to a Range 252 Section 3 - Specifying the Seed Value 252 Section 4 - The nextFloat Method 253 Section 5 - Computer Guessing Games 254 Short Problems 256 Long Problems 256 Formatting Output 258 Section 1 - The NumberFormat Class 258 Section 1a - The getNumberInstance Method 258 Section 1a1 - Grouping 259 Section 1a2 - Rounding 260 Section 1b - The getCurrencyInstance Method 260 Section 1c - The getPercentInstance Formatter 261 Section 2 - The DecimalFormat Class 262 Section 3 - The printf Method 263 Short Problems 263 Long Problems 264 Math Operations 266 Section 1 - The Math Class 266 Section 2 - Math Class Constants 266 Section 3 - The abs Method 267 Section 4 - The floor Method 267 Section 5 - The ceil Method 267 Section 6 - The round Method 267 Section 7 - The min Method 268 Section 8 - The max Method 268 Section 9 - The pow Method 268 Section 10 - The log, log10, and exp Methods 269 Section 11 - The hypot Method 269 Section 12 - Trigonometric Functions 269 Section 12a - The toRadians and toDegrees Methods 269 Section 12b - The sin, cos, and tan Methods 270 Section 12c - The asin, acos, and atan Methods 270 Short Problems 271 Long Problems 272 Enum Types 274 Section 1 - Defining an Enum Type 275 Section 2 - Enum Types are Classes 276 Section 3 - The ordinal Method 276 Section 4 - The toString and name Methods 277 Section 5 - The equals Method 277 Section 6 - The compareTo Method 277 Section 7 - The values Method 278 Section 8 - Placing an Enum Type in a Separate File 278 Section 9 - Switching on an Enum Reference Variable 279 Section 10 - Enum Reference Variables as Fields in other Classes 280 Short Problems 281 Long Problems 282

Copyright ©2017 by Ralph Lecessi Incorporated. All rights reserved.

Back to ralphlecessi.com