About the Authors v About the Technical Writer vii About the Technical Editors vii Acknowledgments ix Introduction xxv Part I: Getting Set Up 1 Lesson 1: Installing Java 2 The Java Universe 3 The Java Development Kit 3 The Java Virtual Machine 4 The Java Runtime Environment 5 Installing OpenJDK 5 Installing OpenJDK on Microsoft Windows 6 Checking for an Existing JDK on Windows 6 Installing OpenJDK 7 Verifying the Installation 10 Installing OpenJDK on macOS 13 Checking for an Existing JDK on macOS 13 Installing OpenJDK 13 Verifying the Installation 17 Creating Your First Java Program 17 Writing the Program 18 Compiling and Running the Program 19 Dealing with Syntax Errors 21 Using the Compiler and Java Virtual Machine 22 Summary 22 Lesson 2: Installing a Development Environment: NetBeans 24 Getting Started 25 Understanding the Value of an IDE 25 Using the Source Code Editor 25 Using the Build Automation Tools 26 Using the Debugger 26 Installing NetBeans 27 Using an Installer 27 Installing on Windows 28 Installing on macOS 32 Installing Binaries 37 Installing Binaries on Windows 40 Adding the JDK 42 Running NetBeans 44 Summary 45 Lesson 3: Using an Integrated Development Environment 46 Using an IDE 47 Creating a New Project 48 Coding the "Hello, World!" Program 53 Compiling the Code in NetBeans 57 Running the Program 59 Using the Debugging Tools 60 Compilation and Syntax Errors 61 Runtime and Logic Errors 63 Understanding Source Control 67 Summary 69 Part II: Basics of Object Oriented Programming 70 Lesson 4: Using Program Statements and Variables 71 Approach 72 Concepts 73 Computers 73 Data vs. Information 73 Programs and Programming 74 Models and Metaphors 74 Objects 74 Specifications 75 Syntax vs. Semantics 75 Language Building Blocks 75 Comments 75 Single-Line Comments 76 Multiline Comments 76 Doc Comments 77 Using Comments 78 Identifiers 79 Data Types 80 Statically Typed Languages 81 Type Conversion 81 Literals 82 Variables 82 Constants 84 Operators and Expressions 84 Three Flavors of Operators 85 Operator Precedence 88 Pulling It All Together: Mathematical Expressions and Operators 89 Summary 93 Exercises 93 Exercise 1: ABeginning.java 94 Exercise 2: ProjectGutenberg.java 95 Exercise 3: CommentingCode.java 97 Exercise 4: AllTheMath.java 98 Exercise 5: BucketsOfFun.java 100 Lesson 5: Collecting and Parsing Input from the User 103 Console Input and Output 104 Adder Version 1 104 Adder Version 2 106 Declaring and Initializing Our Variables 106 Setting Up a Scanner Object 106 Understanding the Command Line 107 Getting the Values from the User 108 Going from Strings to Integers 108 Adding the Numbers Together 109 Using Loops to Gather User Input 111 Parsing Data 112 Dealing with Bad Numbers: NumberFormatException 113 Scanner Problems 115 Summary 121 Exercises 121 Exercise 1: Quest for the User Input 121 Exercise 2: Don't Forget to Store It 123 Exercise 3: Passing the Turing Test 124 Exercise 4: Healthy Hearts 125 Exercise 5: Mini Mad Libs 126 Lesson 6: Pulling It All Together: Building a Useful Program 127 Planning the Program 128 Identifying the Variables 128 Planning the Steps 128 Creating the Code 129 Declaring the Variables 129 Getting Input 130 Converting the Strings 131 Calculating the Area and Perimeter 132 Calculating the Cost 133 Displaying the Results 133 Running the Program 135 Summary 135 Exercises 136 Exercise 1: Mapping Instructions to Code 136 Exercise 2: Adding Prompts for Costs 136 Exercise 3: Adding Multiple Windows 136 Exercise 4: Adding Validation 137 Lesson 7: Controlling Program Flow 138 What Can Our Code Do? 139 Decisions and Boolean Expressions 139 if Statements 139 if-else Statements 142 Chaining if-else Statements 144 Evaluating Multiple Conditions 146 Conditional-And and Conditional-Or Operators 147 Exclusive-Or 149 Ternary or Conditional Operator 150 switch Statement 151 Falling Through with a switch Statement 153 Comparing for Equal Strings 155 Summary 158 Exercises 159 Exercise 1: What Month is It? 159 Exercise 2: Guess Me 159 Exercise 3: Birthstones 160 Exercise 4: Trivia Night 161 Exercise 5: Space Rustlers 162 Exercise 6: Picky Eater 163 Exercise 7: Field Day 165 Lesson 8: Looping Program Flow 167 Loops 167 Using while Loops 168 Initializing the while Loop 171 Incrementing the while Loop 171 Breaking a Loop 172 Using do-while Loops 174 Making do-while Shine 176 Using for Loops 179 Initializing the for Loop 181 Terminating the for Loop 181 Incrementing a for Loop 181 Summary 184 Exercises 184 Exercise 1: Surfacing in BewareTheKraken.java 185 Exercise 2: Do or Do Not 185 Exercise 3: Guess Me Finally 186 Exercise 4: for and Twenty Blackbirds 187 Exercise 5: Spring Forward, Fall Back 188 Exercise 6: for Times 189 Exercise 7: for Times for 189 Exercise 8: Nesting for Loops 190 Exercise 9: Traditional Fizz Buzz 191 Lesson 9: Understanding Flowcharts and Algorithms 193 Flowchart Basics 194 Sequences 194 Branches 195 Loops 196 Flowcharting 197 Flowchart Example 197 Summary 198 Exercises 200 Exercise 1: A Guessing Game 200 A Possible Flowcharting Solution 201 The Guessing Game Flowchart 201 The Guessing Game Code 203 Lesson 10: Adding Randomness to Your Programs 207 Initializing the Random Object 208 Including the Random Class 208 Seeding Random Numbers 208 Generating Random Integers 209 Generating Random Doubles 210 Revisiting the Number Guessing Game 212 Other Random Options 215 Summary 215 Exercises 215 Exercise 1: A Little Chaos 216 Exercise 2: Opinionator--Making Random Choices 217 Exercise 3: High Roller 219 Exercise 4: Coin Flipper 220 Exercise 5: Guess Me More 220 Exercise 6: Fortune Cookie 221 Lesson 11: Debugging 222 Creating a File to Debug 222 Setting Breakpoints 224 Stepping through Code 226 Running the Debugger 226 Examining Variables 229 The Console and the Debugger 232 Summary 234 Exercises 234 Exercise 1: Odd Odd Numbers 234 Exercise 2: A Simple Question of If 235 Lesson 12: Adding Methods 237 Why Methods? 238 Defining Methods 238 Method Signature 239 Method Naming Conventions 239 Return Values 240 Using Return Values 241 Input Parameters 242 Matching Number of Parameters 244 Matching Parameter Types 245 Passing Parameters 246 Method Forms 248 No Return Value, No Parameters 248 Return Value, No Parameters 248 No Return Value, One or More Parameters 249 Return Value, One or More Parameters 249 The staticKeyword 249 Pulling It All Together 250 Identifying the Similarities 252 Identifying the Differences 252 Creating the Method 252 Setting the Name 252 Determining Parameters 253 Setting a Return Type 253 Our Basic Definition 253 Our Method Body 254 Using the Method 255 Scope 257 Debugging and Step Into 259 Setting a Breakpoint in a Method 260 Stepping into Code 262 Debugging and Scope 265 Summary 265 Exercises 265 Exercise 1: Method to the Madness 266 Exercise 2: Return to Sender 267 Exercise 3: MatchWork 268 Exercise 4: Barely Controlled Chaos 269 Lesson 13: Organizing with Arrays 272 What is an Array? 273 Types of Arrays 274 Element vs. Index 275 Declaring Arrays 276 Single-Dimensional Arrays 276 Rectangular Arrays 276 Jagged Arrays 277 Initializing Arrays 277 Initializing a Single-Dimensional Array 278 Initializing a Rectangular Array 279 Initializing a Jagged Array 280 Accessing Array Elements 281 Accessing Elements in a Single-Dimensional Array 281 Accessing Elements in a Multidimensional Array 283 Accessing Elements in a Jagged Array 284 Iterating through Arrays 284 Iterating through Multidimensional and Jagged Arrays 286 Sum the Elements of an Array 288 Looping Back to Front, with a Twist 289 Printing Pairs of Elements 290 Changing the Size of an Array 291 Dealing with Errors 293 Summary 293 Exercises 294 Exercise 1: A Rainbow 294 Exercise 2: Still Positive 295 Exercise 3: Fruit Basket 296 Exercise 4: Simple Combination 297 Exercise 5: Hidden Nuts 297 Exercise 6: Summative Sums 298 Part III: Fundamentals of Classes and Objects 299 Lesson 14: Object-Oriented Concepts 300 Abstraction 301 Object Orientation 301 Types 302 Public Interface/Private Implementation 302 Encapsulation and Data Hiding 303 Single Responsibility Principle and Cohesion 303 Delegation 304 Summary 304 Lesson 15: Creating Classes and Types 305 Creating New Types 305 Classes vs. Objects 307 Properties, Accessors, and Mutators 307 Dot Operator 308 this Keyword 309 Methods/Behaviors 309 Constructors 309 Object Instantiation and Method Invocation 311 Static Keyword 312 Static and the Main Method 313 Static and Constant Values 313 Static and Utility Methods 314 Pulling It All Together 315 Creating a New Class 316 Going Static 320 When to Go Static 323 Existing Static Methods 325 Summary 325 Exercises 326 Exercise 1: Class Modeling 327 Exercise 2: Refactoring 328 Exercise 3: A Multiclass Problem 330 Lesson 16: Managing Storage and Memory 331 Programs and Memory 331 The Stack 332 The Heap 334 Garbage Collection 338 Reference and Null Values 339 Summary 341 Lesson 17: Exploring Interfaces, Composition, and Inheritance 342 How Do These Tools Help? 343 Interfaces 344 Composition and Inheritance 345 Composition 345 Inheritance 346 Using Interfaces, Composition, and Inheritance 346 Summary 347 Lesson 18: Diving into Interfaces 348 What is an Interface? 349 Declaring an Interface 349 Implementing an Interface 350 Interface Restrictions 352 Implementing Multiple Interfaces 352 Extending an Interface 354 Interfaces and Polymorphism 356 Summary 357 Lesson 19: Diving into Composition 358 What is Composition? 359 Book without Composition 359 Book with Composition 360 Summary 362 Exercises 362 Exercise 1: Classroom Composition 362 Exercise 2: Cookbook 363 Lesson 20: Diving into Inheritance 365 Everything Extends Object 366 Terminology 366 Access Control 367 Inheritance 368 Code Reuse through Inheritance 371 Method Overloading 371 Method Overriding 373 Constructors 375 Polymorphism 380 Calling Methods Polymorphically 382 Abstract Base Classes 383 Summary 383 Exercises 383 Exercise 1: Working with Shape 384 Lesson 21: Understanding Collections 385 Collections in Java 385 Javadoc 386 Collections Framework Structure 387 Interface vs. Implementation 389 Iterator 390 Commonly Used Interfaces in the Collections Framework 390 Quick Look at List 390 ArrayList 390 Stack 391 Quick Look at Set 391 HashSet 391 TreeSet 392 Quick Look at Map 392 HashMap 392 TreeMap 392 Summary 393 Lesson 22: Introduction to Lists 394 List 394 Types of Lists 395 ArrayList 395 LinkedList 395 Stack 397 Understanding Generics 397 Using Lists 398 Instantiating a List 398 Adding Items to a List 401 Accessing Items in a List 402 Inserting Items into a List 403 Removing Items from a List 403 Enhanced Approaches to Access List Items 405 The Enhanced for Loop and Lists 406 Visiting Each Element: Iterators 407 Creating an Iterator 407 Using the Iterator 408 Summary 410 Exercises 410 Exercise 1: Three Threes 410 Exercise 2: Mixed-Up Animals 411 Lesson 23: Exploring Maps 412 Map Interface 412 HashMap 413 Creating and Adding Entries to a Map 413 Instantiating a Map 413 Adding Data to a Map 414 Manipulating with Entries in a Map 415 Looking Up Values in a Map 415 Replacing Data in a Map 417 Removing Values from a Map 417 Getting Keys and Listing Mapped Values in a Map 417 Listing All the Keys 417 Listing All the Values Key by Key 420 Listing All the Values: Value Collection 421 Summary 423 Exercises 423 Exercise 1: State Capitals 423 Exercise 2: A Reusable User I/O Class 424 Exercise 3: Student Quiz Scores 427 Lesson 24: Using Simple File Input and Output 429 Data Storage and Representation 430 File Format, Marshaling, and Unmarshaling 431 Student Class 432 File Format Example 433 Marshaling and Unmarshaling Approach 433 Simple File I/O 434 Writing to a File 435 Exceptions 436 The OutFile.txt File 437 Reading from a File 438 Summary 440 Exercises 440 Exercise 1: Creating State Capitals 441 Exercise 2: Hashing the State Capitals 442 Exercise 3: A State Guessing Game 442 Exercise 4: Objectifying States 443 Lesson 25: Applying Application Design 446 CRUD Applications 447 Using a Tiered Design 448 The Basic Tiers 448 The Model-View-Controller Pattern 448 Packages 449 Software Development Lifecycle Overview 450 Waterfall 450 Iterative Development 451 Agile Development 451 Project Lifecycle 452 Phase 1: Inception 452 Phase 2: Elaboration 452 Phase 3: Construction 453 Phase 4: Transition 453 Iterations 453 Summary 454 Lesson 26: Handling Exceptions 455 Exception Handling 456 Catch or Specify Requirement 459 Exception Types 459 Handling (Catching) Exceptions 459 try Block 459 catch Block 461 finally Block 462 Specifying and Throwing Exceptions 464 Exception Translation and Encapsulation 465 Summary 466 Exercises 466 Exercise 1: Keep On Asking 466 Exercise 2: Arrays Gone Bad 466 Exercise 3: Exiting Gracefully 468 Lesson 27: Pulling It All Together: Building the Class Roster App 469 Application Requirements and Use Cases 470 Understanding the Application Structure 470 Interface Relationships 471 Composition Relationships 472 Inheritance Relationships 472 Classes and Interfaces in Our Application 472 MVC Rules of the Game 473 Construction Approach 473 Setting Up the Class Roster Application Shell 474 Creating the Menu System 477 UserIO and UserIOConsoleImpl 478 ClassRosterController 482 App 483 ClassRosterView 484 ClassRosterController 485 Adding a Student Use Case 487 Student (DTO) 487 ClassRosterDao and ClassRoster-DaoFileImpl 488 ClassRosterDao 488 ClassRosterDaoFileImpl 490 ClassRosterView 491 ClassRosterController 493 Viewing All Students Use Case 494 ClassRosterDaoFileImpl 494 ClassRosterView 495 ClassRosterController 496 Getting a Student Use Case 497 ClassRosterDaoFileImpl 498 ClassRosterView 498 ClassRosterController 499 Removing a Student Use Case 501 ClassRosterDaoFileImpl 501 ClassRosterView 501 ClassRosterController 502 Handling Unknown Command and Exiting 504 ClassRosterView 504 ClassRosterController 504 Working with Dependency Injection 506 Implementation 507 ClassRosterController 507 ClassRosterView 508 App 508 Handling Application Exceptions 509 ClassRosterDaoException 509 Adding File Persistence 512 ClassRosterDaoFileImpl 512 Constants 512 unmarshalStudent 513 loadRoster 514 marshalStudent 516 writeRoster 517 addStudent 518 getAllStudents 519 getStudent 520 removeStudent 520 ClassRosterDao 521 ClassRosterView 522 ClassRosterController 522 Create roster.txt 525 Summary 527 Exercises 527 Exercise 1: DVD Library Update 527 Exercise 2: Electronic Address Book 528 Part IV: Intermediate Java 531 Lesson 28: Exploring the Service Layer 532 The Role of the Service Layer 532 Service Layer Exceptions 534 Service Layer API Design 534 New Business Rules 534 Create a Student 535 Define the Method Signature 535 Define the Return Type 535 Define the Errors That Might Occur in This Method 535 Service Layer Methods for Getting Students 536 Get All Students 536 Get a Single Student 537 Remove a Student 537 Summary 537 Lesson 29: Pulling It All Together: Coding the Class Roster Service Layer 538 Create a Service Layer Interface 539 Create New Exceptions 542 ClassRosterDuplicateIdException 542 ClassRosterDataValidation Exception 543 Refactor/Rename ClassRoster-DaoException 543 Create the Service Layer Implementation 546 Constructor and DAO Member 547 Validating Student Data 547 Create Student 548 Get All Students 549 Get One Student 549 Remove Student 550 Modify the Controller 550 Replace Member Field 551 Modify Constructor 551 Replace Calls to DAO Methods with Calls to the Service Layer Method 551 Modify the App 553 Add the Audit Log Feature 554 Audit DAO Interface and Implementation 554 Modify the Service Layer 556 Add Member Field 556 Modify the Constructor 556 Modify createStudent 556 Modify removeStudent 557 Modify App 558 Summary 559 Lesson 30: Doing Unit Testing 560 Unit Testing 561 Types of Unit Testing 561 Black-Box vs. Glass-Box Testing 562 Stateful vs. Stateless Components 562 Test-Driven Development and Red/Green/Refactor 562 Test Stubs 563 JUnit 563 Test Setup and Teardown 563 Annotations 564 Asserts 564 Given/When/Then 564 Stateless Unit Testing 565 What Makes a Good Unit Test? 565 Designing a Test Plan 565 Happy Llama Test Plan 567 Implementing Unit Tests 568 Creating the Test Class 568 Writing Happy Llama Tests 569 Running JUnit Tests 571 Summary 575 Lesson 31: Testing Stateful Code 577 Unit Testing Stateful Code 578 Separating Production and Test Data 579 Adding hashCode and equals to Student 580 Adding toString to Student 582 Creating the Test Class 583 The Set Up and Tear Down Methods 584 Arrange/Act/Assert for Stateful Code 585 Class Roster DAO Test Design 586 Add Student 586 Get Student 588 ClassRosterDaoTest: Adding and Getting a Student 589 Get All Students 590 ClassRosterDaoTest: Adding and Getting All Students 590 Remove Student 592 ClassRosterDaoTest: Adding and Removing Students 592 Unit Testing the Service Layer 594 Planning the Test Design 595 Creating the Test Class 595 Creating the DAO Stubs 596 ClassRosterAuditDaoStubImpl 597 ClassRosterDaoStubImpl 597 Test Setup 599 Test Implementation 600 testCreateValidStudent 600 testCreateStudentDuplicateId 601 testCreateStudentInvalidData 602 testGetAllStudents 603 testGetStudent 603 testRemoveStudent 604 Summary 605 Exercises 605 Exercise 1: Testing the Address Book App 605 Exercise 2: Testing the DVD Library 606 Lesson 32: Including Magic Numbers and Enums 607 Magic Numbers 608 Enums 608 Creating Enums for Fixed Constants 609 Using Enums 609 Getting Values from an Enum 611 Enum Members 612 Summary 616 Exercises 616 Exercise 1: How Many Days until Friday? 617 Exercise 2: Playing Cards 617 Lesson 33: Manipulating Dates and Times 618 ISO Calendar 619 Human Time and Machine Time 619 Local and Zoned Classes 619 Periods and Duration 620 Working with LocalDate Objects 620 Creating LocalDates 620 Converting Dates to and from Strings 622 Formatting Dates 623 Using Localization 625 Getting the Time with LocalDateTime 626 Working with Date Calculations 628 Calculating Dates in the Future and Past 628 Calculating the Time between Two Dates 630 Working with Legacy Dates 632 Converting Date Objects 632 Converting a GregorianCalendar Object 633 Summary 635 Exercises 635 Exercise 1: Birthday Calculator 635 Exercise 2: It's The End of the World as We Know It 636 Exercise 3: Tracking Your Time 636 Lesson 34: Using the BigDecimal Class 638 Exploring BigDecimal 639 Constructing BigDecimals 639 Understanding Scale 641 Understanding Rounding Modes 641 Working with BigDecimal 642 Setting Scale 642 Setting Scale without Rounding Mode 643 Rounding BigDecimals 643 Doing Calculations with BigDecimals 645 Dividing BigDecimals 646 Summary 648 Exercises 648 Exercise 1: Interest Calculator 648 Exercise 2: Car Lot Service Layer 649 Lesson 35: Working with Lambdas and Streams 653 Using Aggregate Operations 654 Understanding Pipelines and Streams 654 Streams vs. Iteration 655 Exploring Lambdas 655 Working with Stream and Lambda Syntax 655 The forEach() Stream Method 656 The filter Stream Method 660 The map Stream Method 661 The collect Stream Method 663 Further Syntax 664 Summary 665 Exercises 665 Exercise 1: Only the Young 665 Exercise 2: DVD Library Update 666 Part V: Advanced Java 667 Lesson 36: Working with the Spring Framework 668 Dependency Injection and Programming to Interfaces 669 Why Spring? 670 Understanding the Spring Approach 670 Plain Old Java Objects 671 Templates 671 Pulling It All Together: Spring DI in Action 673 Spring DI with XML 674 XML Configuration File 675 Update the App Class 676 Spring DI with Annotations 677 Class Annotations 678 Summary 681 Lesson 37: Introducing Maven 682 What is Maven? 683 Project Object Model 683 Project Identification 685 Dependencies 685 Build Settings 686 Dependency Management 686 Maven Lifecycle 687 Summary 687 Lesson 38: Pulling It All Together: Building the Class Roster with Spring 689 Include the Spring Libraries 690 How Do I Know What My Dependency Entry Should Be? 691 Add Spring Configuration Files 692 Convert the App Class to Use Spring 693 Defining Beans 694 Modifying the App Class 696 Convert the Unit Tests to Use Spring 697 Defining Beans 698 Modifying the Test Class Constructor 699 Exception Conditions 700 Summary 701 Exercises 701 Exercise 1: DVD Library 701 Part VI: Appendices 702 Appendix A: Code Checklist 703 Functionality 703 Style 703 Appendix B: Java concepts Study List 705 Variables 705 Methods 705 Arrays 706 Making Decisions 706 Loops 706 Java/JVM 706 Objects 707 Interfaces 708 Inheritance 708 N-Tier Design 708 Collections 709 Exceptions 709 Lambdas/Streams 709 Spring Core 709 Appendix C: Agile Approach Checklist for Console CRUD Applications 710 Assumptions 710 Requirement Steps 711 Design Steps 711 Construction Steps 712 Index 714
HAYTHEM BALTI, PhD, is Director of Curriculum at Wiley's Software Guild and mthree. He has created courses used by thousands of Software Guild Students in Java, Python, Go, and other development and data science competencies. He earned his doctorate in Computer Engineering and Computer Science from the University of Louisville. ALAN GALLOWAY is Director of Instruction at Wiley's Software Guild and mthree. H:e supervises a team of instructors who deliver large-scale training programs in technology.
1997-2024 DolnySlask.com Agencja Internetowa