Foundational Java - Key Elements and Practical Programming.

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Java is now well-established as one of the world’s major programming languages, used in everything from desktop applications to web-hosted applications, enterprise systems and mobile devices. Java applications cover cloud-based services, the Internet of Things, self-driving cars, animation, game development, big data analysis and many more domains. The second edition of Foundational Java: Key Elements and Practical Programming presents a detailed guide to the core features of Java – and some more recent innovations – enabling the reader to build their skills and confidence though tried-and-trusted stages, supported by exercises that reinforce the key learning points. All the most useful and commonly applied Java syntax and libraries are introduced, along with many example programs that can provide the basis for more substantial applications. Use of the Eclipse Integrated Development Environment (IDE) and the JUnit testing framework is integral to the book, ensuring maximum productivity and code quality when learning Java, although to ensure that skills are not confined to one environment the fundamentals of the Java compiler and run time are also explained. Additionally, coverage of the Ant tool will equip the reader with the skills to automatically build, test and deploy applications independent of an IDE. --=== Topics and features: • Presents the most up-to-date information on Java, including Java 14 • Examines the key theme of unit testing, introducing the JUnit 5 testing framework to emphasize the importance of unit testing in modern software development • Describes the Eclipse IDE, the most popular open source Java IDE and explains how Java can be run from the command line • Includes coverage of the Ant build tool • Contains numerous code examples and exercises throughout • Provides downloadable source code, self-test questions, PowerPoint slides and other supplementary material at the website http://www.foundjava.com This hands-on, classroom-tested textbook/reference is ideal for undergraduate students on introductory and intermediate courses on programming with Java. Professional software developers will also find this an excellent self-study guide/refresher on the topic. --=== Dr. David Parsons is National Postgraduate Director at The Mind Lab, Auckland, New Zealand. He has been teaching programming in both academia and industry since the 1980s and writing about it since the 1990s.

Author(s): David Parsons
Edition: 2
Publisher: Springer
Year: 2020

Language: English
Pages: 600
Tags: java 14 jdk openjdk 2020 swing javafx

Preface
Overview and Goals
Organization and Features
Target Audiences
Suggested Uses
Supplemental Resources
A Note About the Code
Acknowledgements
Contents
1 The Java Story
1.1 A Brief History of Java
1.2 Characteristics of Java
1.2.1 Simple
1.2.2 Object-Oriented
1.2.3 Distributed
1.2.4 Robust
1.2.5 Secure
1.2.6 Architecture-Neutral
1.2.7 Portable
1.2.8 High-Performing
1.2.9 Multithreaded
1.2.10 Dynamic
1.3 The JDK and the JRE
1.3.1 Java Versions
1.4 Java APIs
1.5 Summary
2 Compiling and Running Java Programs
2.1 Java from the Command Prompt
2.1.1 Setting the Path to the JDK
2.2 A First Java Program
2.2.1 The MyJavaProgram Class
2.2.2 The “main” Method
2.2.3 Output with the System Class
2.3 Compiling Java
2.4 Running Java
2.5 The Java Classpath
2.5.1 Packages
2.5.2 Compiling into a Package Folder
2.6 Comment Syntax
2.7 Using the Eclipse Integrated Development Environment (IDE)
2.7.1 Creating a New Project
2.7.2 Creating a New Package
2.7.3 Creating a New Class
2.7.4 Editing Code in Eclipse
2.7.5 Compiling and Running Code in Eclipse
2.8 Summary
3 Data Types, Arithmetic and Arrays
3.1 Java Primitive Data Types
3.1.1 Literals and Variables
3.1.2 Literals and the Compiler
3.1.3 Boolean Variables
3.1.4 Unicode Characters and Escape Sequences
3.1.5 Variable Scope
3.2 Arithmetic and Other Operators
3.2.1 Arithmetic Statements
3.2.2 Increment and Decrement Operators
3.2.3 Prefix and Postfix Operators
3.2.4 Assignment Operators
3.2.5 Promotion and Type Casting
3.2.6 String Concatenation
3.2.7 Bitwise Operators
3.3 Arrays
3.3.1 Declaring Arrays
3.3.2 Initializing Arrays
3.3.3 Accessing Array Data
3.3.4 The Array “length” Field
3.3.5 Multidimensional Arrays
3.4 Precedence, Type Casting and Promotion by Example
3.5 Summary
4 Control Structures
4.1 Making Selections
4.1.1 “if” Statements
4.1.2 Expressing Conditions with Relational and Boolean Operators
4.1.3 Using Selection: The CoinExample Class
4.1.4 The DieExample Class
4.1.5 Writing Test Code
4.1.6 “switch” Statements
4.1.6.1 A “switch” Example: The Dice Man
4.1.6.2 “break” and “continue”
4.1.7 The “switch” Expression
4.1.8 The Ternary Operator
4.2 Iteration
4.2.1 “while” and “do…while” Loops
4.2.1.1 A “do…while” Example
4.2.2 “for” Loops
4.2.2.1 Iteration with a “for” Loop
4.2.2.2 “for” Loops and Arrays
4.2.2.3 Iterating Through the Array Passed to “Main”
4.2.2.4 Multiple Initializations and Actions
4.3 Summary
5 Creating Objects
5.1 Classes and Objects
5.1.1 Object Creation—Constructors
5.2 Classes and Methods for String Data
5.2.1 The String Class
5.2.2 Long Strings and Text Blocks
5.2.3 String Builders
5.2.4 String Buffers
5.2.5 The “toString” Method
5.3 References and Memory
5.3.1 Garbage Collection
5.3.2 Object Equality
5.3.3 String Concatenation and Memory
5.4 Java Library Packages
5.4.1 Importing Classes
5.4.2 Wild Cards and Multiple Imports
5.4.3 Modules, Packages and Sub Packages
5.4.4 Managing Imports with Eclipse
5.5 Using Javadoc
5.5.1 Viewing Javadoc in Eclipse
5.6 Summary
6 Creating Domain Classes
6.1 Object Orientation and Domain Objects
6.1.1 Clock Objects in the Real World
6.1.2 Encapsulation and Information Hiding
6.1.3 Object Identity
6.2 Creating New Classes
6.2.1 Adding Fields to a Class
6.2.2 Adding Methods to a Class
6.3 Constructors
6.3.1 Overloaded Constructors
6.3.2 Chaining Constructors
6.3.3 Internal Encapsulation in Constructors
6.4 Access Modifiers
6.5 Javadoc and Code Comments
6.5.1 Creating Javadoc in Eclipse
6.5.2 Running Javadoc from the Command Line
6.6 Types of Method
6.7 Class Reuse
6.8 Static Fields and Methods
6.8.1 Static Methods
6.8.2 Invoking Static Methods
6.8.3 Static Final Fields
6.8.4 Static Methods in Java Library Classes
6.9 Objects as Java Bean Components
6.9.1 Reflection
6.10 The “var” Keyword
6.11 Summary
7 Objects Working Together: Association, Aggregation and Composition
7.1 Aggregations as Collections
7.2 Message Passing
7.2.1 Object Association
7.2.2 Association Direction
7.2.3 Implementing Associations
7.3 Associating Objects: The Snakes and Ladders Game
7.3.1 The Snakes and Ladders Game Source Code
7.3.1.1 Package Visibility
7.3.1.2 The Snake and Ladder Classes
7.3.1.3 The BoardSquare Class
7.3.1.4 The GameBoard Class
7.3.1.5 The Snakes and Ladders Class
7.3.2 When to Create Objects
7.4 Association, Aggregation or Composition?
7.4.1 Aggregation Example
7.5 Composition
7.5.1 Logic Gates
7.5.2 Half Adder Components
7.6 Summary
8 Inheritance, Polymorphism and Interfaces
8.1 Abstraction
8.1.1 Inheritance
8.1.2 Polymorphism
8.1.3 The Relationship Between Inheritance and Polymorphism
8.2 Inheriting from the Object Class
8.2.1 Overriding the “toString” Method
8.2.2 Overriding the “equals” Method
8.2.3 Overriding the “hashCode” Method
8.2.4 Generating “hashCode” and “equals” Methods in Eclipse
8.2.5 The @Override Annotation
8.3 An Inheritance Hierarchy Using Abstraction
8.3.1 Abstract Classes
8.3.2 Protected Visibility and Inheritance
8.3.3 Creating Subclasses
8.3.4 Calling Superclass Constructors
8.3.5 Adding Further Subclasses
8.4 Dynamic Binding and Abstract Methods
8.4.1 Using Polymorphic Methods
8.5 Interfaces
8.5.1 Naming Interfaces
8.5.2 Implementing an Interface
8.5.3 Further Aspects of Interfaces
8.6 Lambda Expressions
8.6.1 Lambda Expression Syntax
8.7 Summary
9 Exception Handling
9.1 Java Exceptions
9.1.1 The Exception Handling Hierarchy
9.1.2 Checked and Unchecked Exceptions
9.1.3 Exception Handling Keywords
9.2 Handling Checked Exceptions
9.2.1 Catching Exceptions: “try” and “catch”
9.3 Handling Unchecked Exceptions
9.3.1 Exiting
9.4 Catching Multiple Exceptions
9.4.1 Ordering Multiple “catch” Blocks
9.5 Throwing an Exception with “throw”
9.5.1 Delegating Responsibility
9.6 Writing and Using Custom Exceptions
9.6.1 Throwing a Custom Exception
9.6.2 “finally” Blocks
9.6.3 “try-with-resources”
9.6.4 Re-Throwing Custom Exceptions
9.7 Optional: An Alternative to “null”
9.8 Summary
10 Unit Testing with JUnit
10.1 The JUnit Test Framework
10.1.1 Using JUnit with Eclipse
10.2 Test Cases and Units Under Test
10.2.1 Creating a JUnit Test Case
10.2.2 Required Imports
10.2.3 Writing Test Methods
10.2.4 The Eclipse Test Runner
10.3 Types of Assertions
10.3.1 Asserting Floating-Point Equality
10.3.2 Object Equality
10.3.3 Other Assertions
10.4 Exceptions and Timeouts
10.5 Forcing Failures
10.6 Arranging a Test with @BeforeEach and @AfterEach Methods
10.6.1 The 3A Pattern
10.6.2 @BeforeAll and @AfterAll Methods
10.7 Writing a Test Suite
10.8 Summary
11 Exploring the Java Libraries
11.1 Frequently Used Classes in the “java.lang” Package
11.1.1 The java.lang.Object Class
11.1.1.1 The “finalize” Method
11.1.1.2 The “clone” Method
11.1.2 The java.lang.Math Class
11.1.3 The java.lang.System Class
11.1.4 Wrapper Classes
11.2 Classes in the “java.util” Package
11.2.1 Factory Methods
11.2.2 The Locale Class
11.2.3 The Currency Class
11.3 NumberFormat Classes in the “java.text” Package
11.3.1 Formatting Numbers
11.3.2 Formatting Currencies
11.3.3 Parsing Numbers
11.3.4 Parsing Currencies
11.4 Dates and Times in the “java.time” Package
11.4.1 The LocalDate Class
11.4.2 The LocalTime Class
11.4.3 The LocalDateTime Class
11.4.4 Formatting Dates and Times
11.4.5 Parsing Dates
11.5 Summary
12 The Collections Framework and Generics
12.1 Objects that Contain Objects
12.1.1 Associations and Collections
12.2 The Core Collection Interfaces
12.2.1 Partial Implementations of the Core Interfaces
12.2.2 Concrete Implementations of the Core Interfaces
12.2.3 Legacy Classes
12.3 Typesafe Collections with Generics
12.4 A Concrete Collection Class: The ArrayList
12.4.1 Adding and Retrieving Objects
12.4.2 Wrapping and Autoboxing
12.4.3 Iterators
12.4.3.1 ListIterator
12.4.3.2 Enhanced “for” Loop
12.4.3.3 Lambda Expressions
12.4.4 An ArrayList of Module Objects
12.4.5 Unmodifiable Collections
12.5 Maps
12.5.1 Map Views
12.5.2 Using a TreeMap
12.6 Utility Classes
12.6.1 Comparing Objects with the Comparable Interface
12.6.2 The Collections Class
12.6.3 Creating a Comparator
12.7 Generics and Inheritance
12.7.1 Specifying Bounds
12.8 Collection Streams
12.9 Summary
13 Input and Output Streams
13.1 Java Stream Classes
13.1.1 Different Types of Stream
13.2 Byte Streams
13.2.1 Streaming Bytes to and from Files
13.3 Filter Streams
13.4 Readers and Writers
13.4.1 Buffered Readers and Writers
13.5 PrintStreams and PrintWriters
13.6 The File Class
13.7 Streaming Objects
13.7.1 The “instanceof” Operator
13.7.2 Serializing Course Objects
13.8 The New IO Library
13.8.1 Buffer Classes
13.8.1.1 ByteBuffers
13.8.2 File Channels
13.8.2.1 Reading from a Channel
13.8.3 View Buffers
13.8.4 Charsets and String Data
13.9 Summary
14 Automatic Building and Testing with Ant
14.1 Using Ant in Eclipse
14.2 The Ant Build File—“build.xml”
14.2.1 Ant Properties
14.2.2 Tasks and Targets
14.3 Running an Ant Build File in Eclipse
14.4 Packaging Code with the Ant “jar” Task
14.4.1 Target Dependencies
14.5 Running Code, Forking and Classpaths
14.6 Running Tests
14.6.1 Setting the Classpath with a “path” Element
14.6.2 Running Tests with the “junitlaucher” and “test” Elements
14.7 Installing and Configuring Ant Outside Eclipse
14.8 Summary
15 Java and the Database (JDBC)
15.1 An Example Database
15.2 Using MySQL
15.2.1 Creating a New Database
15.2.2 Generating Primary Keys
15.2.3 Using a DDL Script
15.2.4 Viewing Table Schema
15.2.5 Creating an Authorized MySQL User
15.3 Java Database Access with JDBC
15.3.1 JDBC Drivers
15.3.2 Making a Two-Tier Connection
15.4 SQL Exceptions
15.5 Executing SQL Queries and Updates Using JDBC
15.5.1 Processing ResultSets
15.5.2 Updating Records
15.6 Using Prepared Statements
15.7 Summary
16 Multithreading
16.1 Creating and Running a Thread
16.1.1 Thread States
16.2 Running Multiple Threads
16.3 Thread Priority
16.3.1 Setting Thread Priority
16.3.2 Yielding
16.4 Implementing the Runnable Interface
16.5 Synchronizing Threads
16.5.1 Monitors and Synchronization
16.5.2 “wait”, “notify” and “notifyAll”
16.6 Synchronized Code Example
16.6.1 The Sensor Class
16.6.2 The Analyzer Class
16.7 Concurrent Collections
16.8 Summary
17 Building GUIs with the JFC Swing Library
17.1 Components, Containers and Frames
17.2 Creating a Main Window Frame
17.2.1 Setting the JFrame’s Title
17.2.2 Selecting the JFrame’s Closing Behavior
17.2.3 Sizing the JFrame
17.2.4 Showing the JFrame
17.3 Swing Component Classes
17.3.1 The JLabel Class
17.3.2 Adding Components to a Frame
17.3.3 Manually Positioning and Sizing Components
17.3.4 Common Methods of Component and JComponent
17.3.5 The JTextField Class
17.3.6 The JButton Class
17.3.6.1 Placing Components Using “SetBounds”
17.4 Colors, Fonts and Icons
17.4.1 Setting Colors
17.4.2 Setting Fonts
17.4.3 Font Style and Size
17.4.4 Icons on Labels and Buttons
17.5 Some Additional Components
17.5.1 JTextArea
17.5.2 CheckBoxes and Radio Buttons
17.5.3 JComboBox
17.5.4 JSlider
17.5.5 JPanel
17.6 Setting the Look and Feel
17.7 Layout Managers
17.7.1 BorderLayout
17.7.1.1 BorderLayout Constraints
17.7.2 FlowLayout
17.7.2.1 Adding a FlowLayout JPanel to a JFrame
17.7.3 FlowLayout Constraints
17.7.4 GridLayout
17.8 Summary
18 Event-Driven Programming
18.1 Event Listeners
18.2 Event Classes
18.3 EventListener Interfaces
18.3.1 A JButton ActionListener
18.3.2 Adding a Listener
18.4 Multiple Action and Focus Listeners
18.5 Responding to Mouse Events
18.5.1 Mouse Listener Events
18.5.2 Mouse Motion Events
18.6 Event Handlers as Inner Classes
18.6.1 Implementing a DieListener
18.6.2 Inner Classes
18.6.3 Local Inner Classes
18.6.4 Anonymous Inner Classes
18.6.5 Lambda Expressions as Event Handlers
18.7 JPanel Subclasses and Multi-panel Layouts
18.7.1 The GridBagLayout
18.7.1.1 The “gridx” and “gridy” Constraints
18.7.1.2 The “gridwidth” and “gridheight” Constraints
18.7.1.3 The “fill” Constraint
18.7.1.4 The “insets” Constraint
18.7.1.5 The “ipadx” and “ipady” Constraints
18.7.1.6 The “anchor” Constraint
18.7.1.7 The “weightx” and “weighty” Constraints
18.8 Separating the “Model” from the “View”
18.8.1 The Model Class: FlightDistances
18.8.2 The View Class: DistanceViewer
18.9 Summary
19 Dialogs and Menus, Models and Views
19.1 Predefined Dialogs in Swing
19.1.1 Message Box Dialogs Using JOptionPane
19.1.2 File Chooser Dialogs Using JFileChooser
19.1.3 Color Chooser Dialogs Using JColorChooser
19.2 Custom Dialogs with JDialog
19.3 Using Menus
19.3.1 Adding a Menu to a Frame
19.3.2 Menu Enhancement: Separators, Mnemonics and Accelerators
19.3.2.1 Menu Separators
19.3.2.2 Mnemonics
19.3.2.3 Keyboard Accelerators
19.3.3 MenuListeners
19.4 Model View Controller in Swing Components
19.4.1 Model and View in JTextPane Components
19.4.1.1 Adding a Document
19.4.1.2 Text Styling with AttributeSets
19.4.1.3 Presenting Read-Only Text
19.4.2 Model and View in the JTable Component
19.4.2.1 Implementing the TableModel Interface
19.4.2.2 Creating a JTable View
19.5 Summary
20 Java Web Servers and the HttpClient
20.1 Web Browsers, URLs and HTML Pages
20.1.1 URLs
20.1.1.1 The HTTP Protocol
20.1.1.2 The Server Address
20.1.1.3 The Path and Resource Name
20.1.2 Hypertext Markup Language (HTML)
20.2 Setting Up a Tomcat Server
20.2.1 Installing and Starting Tomcat
20.2.2 The “localhost” URL and Port Number
20.3 Deploying a Web Application to Tomcat
20.3.1 XML Deployment Descriptors
20.3.2 Deploying to the Server
20.3.2.1 Deploying the WAR File with Ant
20.4 The HttpClient Class
20.4.1 Reading HTML into a Swing HTMLEditorKit
20.4.2 Reading JSON Web Services
20.4.2.1 Processing a JSON Web Service with the HttpClient and JSON.simple
20.5 Summary
Index