The Java Workshop - David Cuartielles - E-Book

The Java Workshop E-Book

David Cuartielles

0,0
36,59 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

Get to grips with the fundamentals of Java programming and learn to build useful applications with the help of real-world examples and engaging practical activities

Key Features



  • Build a solid foundation in Java and focus on developing real-world applications
  • Implement the fundamental concepts of object-oriented programming in your code
  • Work with external data storage systems and learn how to efficiently handle data

Book Description



Java is a versatile, popular programming language used across a wide range of industries. Learning how to write effective Java code can take your career to the next level, and The Java Workshop will help you do just that. This book is designed to take the pain out of Java coding and teach you everything you need to know to be productive in building real-world software.



The Workshop starts by showing you how to use classes, methods, and the built-in Collections API to manipulate data structures effortlessly. You'll dive right into learning about object-oriented programming by creating classes and interfaces and making use of inheritance and polymorphism. After learning how to handle exceptions, you'll study the modules, packages, and libraries that help you organize your code. As you progress, you'll discover how to connect to external databases and web servers, work with regular expressions, and write unit tests to validate your code. You'll also be introduced to functional programming and see how to implement it using lambda functions.



By the end of this Workshop, you'll be well-versed with key Java concepts and have the knowledge and confidence to tackle your own ambitious projects with Java.

What you will learn



  • Write clean, well-commented Java code that's easy to maintain
  • Debug logical errors and handle exceptions in your Java programs
  • Implement object-oriented and functional programming paradigms
  • Use regular expressions to search for information in text data
  • Work with information stored in databases using JDBC
  • Make HTTP requests from Java applications and parse the response data
  • Secure your data with cryptography and encryption
  • Write unit tests to validate your code with JUnit

Who this book is for



This Java coding book is designed for anyone who is new to Java. Whether you're an aspiring software developer, or are just curious about learning to code, then this book will get you on the right track. No prior programming experience is required.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB

Seitenzahl: 585

Veröffentlichungsjahr: 2019

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



The Java Workshop

A New, Interactive Approach to Learning Java

David Cuartielles

Andreas Göransson

Eric Foster-Johnson

The Java Workshop

Copyright © 2019 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

Authors: David Cuartielles, Andreas Göransson, and Eric Foster-Johnson

Technical Reviewers: Scott Cosentino, Ashley Frieze, Corey Hendrey, David Parker, Darryl Pierce, and Shubham Shrivastava

Managing Editor: Manasa Kumar

Acquisitions Editor: Sarah Lawton

Production Editor: Shantanu Zagade

Editorial Board: Shubhopriya Banerjee, Bharat Botle, Ewan Buckingham, Megan Carlisle, Mahesh Dhyani, Manasa Kumar, Alex Mazonowicz, Bridget Neale, Dominic Pereira, Shiny Poojary, Abhishek Rane, Erol Staveley, Ankita Thakur, Nitesh Thakur, and Jonathan Wray

First Published: October 2019

Production Reference: 3171219

ISBN: 978-1-83898-669-8

Published by Packt Publishing Ltd.

Livery Place, 35 Livery Street

Birmingham B3 2PB, UK

Table of Contents

Preface   i

1. Getting Started   1

Introduction   2

Writing, Compiling, and Executing Your Hello World Program   2

Exercise 1: Creating Your Hello World Program in Java   3

Basic Syntax and Naming Conventions   9

Printing Out Different Data Types   11

Variables and Variable Types   12

Exercise 2: Printing Different Types of Data   12

Primitive versus Reference Data Types   15

Null   16

Chars and Strings   17

Exercise 3: Declaring Strings   18

Doing Some Math   19

Exercise 4: Solving a Simple Trigonometry Problem   20

Comments Help You to Share Code   21

CLI versus GUI   22

Exercise 5: Running the Code from the CLI   23

Activity 1: Obtaining the Minimum of Two Numbers   24

Summary   24

2. Learning the Basics   27

Introduction   28

Controlling the Flow of Your Programs   28

Exercise 1: Creating a Basic if Statement   29

Comparison Operators   32

Exercise 2: Using Java Comparison Operators   33

Nested if Statements   34

Exercise 3: Implementing a Nested if Statement   35

Branching Two Ways with if and else   36

Exercise 4: Using if and else Statements   36

Using Complex Conditionals   37

Exercise 5: Using Logical Operators to Create Complex Conditionals   39

Using Arithmetic Operators in an if Condition   40

The Ternary Operator   41

Exercise 6: Using the Ternary Operator   41

Equality Can Be Tricky   42

Exercise 7: Comparing Decimal Values   42

Comparing Strings   44

Using switch Statements   45

Exercise 8: Using switch   45

Exercise 9: Allowing Cases to Fall Through   47

Using Java 12 Enhanced switch Statements   48

Exercise 10: Using Java 12 switch Statements   49

Looping and Performing Repetitive Tasks   50

Looping with the for Loop   51

Exercise 11: Using a Classic for Loop   51

Exercise 12: Using an Enhanced for Loop   52

Jumping Out of Loops with Break and Continue   53

Exercise 13: Using break and continue   53

Using the while Loop   54

Exercise 14: Using a while Loop   55

Using the do-while Loop   56

Handling Command-Line Arguments   57

Exercise 15: Testing Command-Line Arguments   57

Converting Command-Line Arguments   58

Exercise 16: Converting String to Integers and Doubles   59

Diving Deeper into Variables — Immutability   60

Comparing Final and Immutable   60

Using Static Values   61

Using Local Variable Type Inference   62

Activity 1: Taking Input and Comparing Ranges   63

Summary   64

3. Object-Oriented Programming   67

Introduction   68

The Anatomy of a Class   68

Working with Objects in Java   70

Checking the Precedence of a Class with instanceof   72

Exercise 1: Creating the WordTool Class   73

Activity 1: Adding the Frequency-of-Symbol Calculation to WordTool   81

Inheritance in Java   82

Overriding and Hiding Methods   83

Avoiding Overriding: Final Classes and Methods   86

Overloading Methods and Constructors   87

Recursion   89

Annotations   90

Interfaces   93

Inner Classes   94

Documenting with JavaDoc   96

Activity 2: Adding Documentation to WordTool   99

Summary   100

4. Collections, Lists and Java's Built-In APIs   103

Introduction   104

Arrays   105

Activity 1: Searching for Multiple Occurrences in an Array   111

Sets   112

Lists   117

Exercise 1: Creating the AnalyzeInput Application   121

Maps   123

Iterating through Collections   125

Exercise 2: Bringing Analytics into the AnalyzeInput Application   129

Sorting Collections   130

Exercise 3: Sort the Results from the AnalyzeInput Application   132

Properties   135

Activity 2: Iterating through Large Lists   138

Summary   139

5. Exceptions   143

Introduction   144

A Simple Exception Example   144

NullPointerException – Have No Fear   148

Catching Exceptions   150

Exercise 1: Logging Exceptions   155

Throws and Throw   156

Exercise 2: Breaking the Law (and Fixing It)   158

The finally Block   161

Activity 1: Designing an Exception Class Logging Data   162

Best Practices for Handling Exceptions   162

Where Do Exceptions Come from?   166

Summary   167

6. Libraries, Packages, and Modules   169

Introduction   170

Organizing Code into Packages   170

Importing Classes   170

Exercise 1: Importing Classes   171

Fully Qualified Class Names   173

Importing All Classes in a Package   174

Dealing with Duplicated Names   175

Static Imports   175

Creating a Package   176

Naming Your Package   176

Directories and Packages   177

Exercise 2: Creating a Package for a Fitness Tracking App   178

Building JAR Files   183

Exercise 3: Building a JAR File   184

Defining the Manifest   185

Exercise 4: Building an Executable JAR File   186

Build Tools   187

Maven   187

Exercise 5: Creating a Maven Project   188

Exercise 6: Adding Java Sources to the Maven Project   191

Exercise 7: Building the Maven Project   193

Exercise 8: Creating an Executable JAR with Maven   195

Using Gradle   196

Exercise 9: Creating a Gradle Project   196

Exercise 10: Building an Executable JAR with Gradle   198

Using Third-Party Libraries   200

Finding the Libraries   200

Adding a Project Dependency   201

Exercise 11: Adding a Third-Party Library Dependency   202

Using the Apache Commons Lang Library   205

Exercise 12: Using the Apache Commons Lang Library   205

Using Modules   207

Creating Modules   208

Exercise 13: Creating a Project for a Module   208

Exercise 14: Creating a Second Module Using the First One   211

Activity 1: Tracking Summer High Temperatures   213

Summary   216

7. Databases and JDBC   219

Introduction   220

Relational Databases   220

Relational Database Management Systems   222

Installing a Database   222

Exercise 1: Running the H2 Database   222

Introducing SQL   224

Exercise 2: Creating the customer Table   224

Inserting Data into a Table   226

Exercise 3: Inserting Data   226

Retrieving Data   227

Relating Tables   229

Exercise 4: Creating the email Table   229

Selecting Data from Multiple Tables   230

Modifying Existing Rows   231

Exercise 5: Modifying email Data   231

Deleting Data   232

JDBC—Accessing Databases from Java   233

Connecting to Databases   233

Querying Data with JDBC   234

Exercise 6: Querying Data with JDBC   236

Sanitizing User Input   238

Using Prepared Statements   239

Transactions and Rollback   240

Exercise 7: Using Prepared Statements with Transactions   242

Simplifying JDBC Programming   243

Using Object-Relational Mapping Software   244

Database Connection Pooling   245

Non-Relational, or NoSQL, Databases   245

Activity 1: Track Your Progress   246

Summary   247

8. Sockets, Files, and Streams   249

Introduction   250

Listing Files and Directories   251

Separating Directories from Files   254

Exercise 1: Listing the Contents of Subdirectories   256

Creating and Writing to a File   261

Activity 1: Writing the Directory Structure to a File   264

Reading an Existing File   266

Reading a Properties File   267

Exercise 2: Creating a Properties File from the CLI   270

What are Streams?   273

The Different Streams of the Java Language   276

What are Sockets?   280

Creating a SocketServer   281

Writing Data on and Reading Data from a Socket   282

Activity 2: Improving the EchoServer and EchoClient Programs   284

Blocking and Non-Blocking Calls   286

Summary   286

9. Working with HTTP   289

Introduction   290

Exploring HTTP   290

HTTP Request Methods   292

Representational State Transfer   293

Request Headers   294

Using HttpUrlConnection   294

Exercise 1: Creating a HEAD Request   296

Reading the Response Data with a GET Request   297

Exercise 2: Creating a GET Request   298

Dealing with Slow Connections   298

Requesting Parameters   298

Handling Redirects   299

Creating HTTP POST Requests   299

Exercise 3: Sending JSON Data with POST Requests   300

Parsing HTML Data   301

Exercise 4: Using jsoup to Extract Data from HTML   303

Delving into the java.net.http Module   305

Exercise 5: Getting HTML Contents Using the java.net.http Module   306

Activity 1: Using the jsoup Library to Download Files from the Web   307

Summary   308

10. Encryption   311

Introduction   312

Plaintext   312

Ciphertext   312

Ciphers   313

Keys   314

Symmetric Key Encryption   314

Exercise 1: Encrypting the String Using Advanced Encryption Standard   315

Block Ciphers   319

Initialization Vectors   319

Stream Ciphers   319

Asymmetric Key Encryption   320

Exercise 2: Encrypting the String Using the RSA Asymmetric Key Encryption   320

Encrypting Files   323

Exercise 3: Encrypting a File   323

Summary   327

11. Processes   329

Introduction   330

Launching a Process   330

Sending Input to a Child Process   332

Capturing the Output of a Child Process   334

Storing the Output of a Child Process in a File   335

Activity 1: Making a Parent Process to Launch a Child Process   335

Summary   337

12. Regular Expressions   339

Introduction   340

Decrypting Regular Expressions   340

Character Classes   341

Character Sets   341

Quantifiers   342

Anchors   342

Capturing Groups   343

Escaped Characters   343

Flags   343

Exercise 1: Implementing Regular Expressions    344

Activity 1: Regular Expressions to Check If the Entrance is Entered in the Desired Format   347

Regular Expressions in Java   347

Exercise 2: Extracting the Domain Using Pattern Matching   348

Exercise 3: Extracting Links Using Pattern Matching   353

Summary   358

13. Functional Programming with Lambda Expressions   361

Introduction   362

Background   362

Functional Programming   362

Side Effects   362

Deterministic Functions   364

Pure Functions   364

Exercise 1: Writing Pure Functions    365

Immutability of State   368

Exercise 2: Creating an Immutable Class   369

Activity 1: Modifying Immutable Lists   373

Immutable Collections   374

Exercise 3: Overriding the String Method   375

Functional Interfaces   376

Lambda Expressions   377

Exercise 4: Listing Spare Tires   379

Summary   382

14. Recursion   385

Introduction   386

Delving into Recursion   386

Exercise 1: Using Recursion to Overflow the Stack   387

Trying Tail Recursion   388

Exercise 2: Using Recursion to Calculate Factorials   389

Processing an XML Document   390

Exercise 3: Creating an XML File   391

Introducing the DOM XML API   392

Exercise 4: Traversing an XML Document   394

Activity 1: Calculating the Fibonacci Sequence   397

Summary   398

15. Processing Data with Streams   401

Introduction   402

Creating Streams   403

Parallel Streams   405

Encounter Order   406

Closing Streams   406

Terminal Operations   408

Intermediate Operations   413

Exercise 1: Using the Stream API   419

Activity 1: Applying Discount on the Items   423

Using Collectors   423

I/O Streams   431

Exercise 2: Converting CSV to a List   433

Activity 2: Searching for Specifics   439

Summary   439

16. Predicates and Other Functional Interfaces   441

Introduction   442

Predicate Interface   442

Exercise 1: Defining a predicate   443

Activity 1: Toggling the Sensor States   451

Consumer Interface   451

Exercise 2: Producing Side Effects   451

Function   455

Exercise 3: Extracting Data   456

Activity 2: Using a Recursive Function   459

Activity 3: Using a Lambda Function   459

Summary   460

17. Reactive Programming with Java Flow   463

Introduction   464

Publisher   466

SubmissionPublisher   466

Subscriber   467

Subscription   468

Exercise 1: A Simple Application with a Single Publisher and a Single Subscriber   468

Processor   475

Exercise 2: Using a Processor to Convert a Stream of Strings to Numbers   475

Activity 1: Let NumberProcessor Format Values as Integers   498

Summary   498

18. Unit Testing   501

Introduction   502

Getting Started with Unit Tests   502

Introducing JUnit   503

Writing Unit Tests with JUnit   503

Exercise 1: Writing a First Unit Test   506

Exercise 2: Writing a Successful Test   510

Deciding What to Test   513

Writing Parameterized Tests   513

Exercise 3: Writing a Parameterized Test   515

When Tests Won't Work—Disabling Tests   517

Test Setup   517

Exercise 4: Using Test Setup and Cleanup Methods   518

Mocking   520

Testing with Mocks Using Mockito   521

Exercise 5: Using Mocks when Testing   524

Activity 1: Counting the Words in the String   527

Summary   528

Appendix   531

Preface

About

This section briefly introduces the coverage of this book, the technical skills you'll need to get started, and the software requirements required to complete all of the included activities and exercises.

1. Getting Started

KAY34

2. Learning the Basics