欢迎来到传世资源网!
加载中...
正在加载,请耐心等待...
本站为收藏、学习站,如有侵权,请联系管理员删除!

Fundamentals of Java Programming.pdf

介绍 评论 失效链接反馈

Fundamentals of Java Programming,2018年
Fundamentals of Java Programming.pdf Java-第1张
ContentsPart I Programming Basics1 “Hello, World!” .............................................................. 31.1 The Programming Environment for Java .................................... 31.1.1 The Java Virtual Machine (JVM) ................................... 31.1.2 Changing Folders in a Command Line Interface ...................... 41.1.3 Source Codes, Bytecodes, and Compilation .......................... 61.2 The First Program, “Hello, World!” ........................................ 71.2.1 Methods and Their Declarations .................................... 91.2.2 System.out.println and System.out.print . . . . . . . . . . . . . . . . 101.2.3 Spacing in the Source Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111.2.4 Commenting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111.2.5 Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131.3 Using Multiple Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151.3.1 System.out.println and System.out.print (Reprise) . . . . . . . 151.3.2 Printing Multiple-Line Texts on the Screen . . . . . . . . . . . . . . . . . . . . . . . . . . . 161.3.3 Escaping Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181.3.4 Printing Shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202 Using Data for Computation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292.1 Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292.1.1 Data and Their Taxonomy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292.1.2 Number Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302.1.3 Variable Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322.1.4 Assigning Values to Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332.2 The Primitive Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352.3 Using Variables for Computation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372.3.1 Quarterbacks Program (Reprise). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372.3.2 Number Arithmetics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392.3.3 Computing the Body-Mass Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462.3.4 Sum of Integers from 1 to 100 à la Gauss . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482.3.5 Simplified Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512.4 An Introduction to the String Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 562.4.1 The String Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 562.4.2 String Concatenation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59ixx Contents3 Reading Keyboard Input. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 693.1 Class Scanner . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 693.1.1 Importing Source Codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 693.1.2 The Instantiation of a Scanner Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 703.2 Reading Data with a Scanner Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 713.3 Reading Input from the Keyboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 774 Decomposing Code into Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 874.1 Procedural Decomposition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 874.1.1 Printing Rectangles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 874.1.2 Printing Quadrangles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 924.1.3 “Old MacDonald Had a Farm” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 954.1.4 A General Strategy for Procedural Decomposition . . . . . . . . . . . . . . . . . . . . . 1014.2 Using Multiple Program Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1045 Passing Values to and from Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1095.1 Passing Values to Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1095.1.1 Methods That Work with Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1095.1.2 Method Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1155.2 Receiving a Value from a Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1195.3 Class Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1245.3.1 Mathematical Functions in Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1245.3.2 Mortgage Calculation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1316 Conditions and Their Use for Controlling the Flow of Programs . . . . . . . . . . . . . . . . . . . 1436.1 Condition and Its Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1436.2 The If Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1496.2.1 If . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1496.2.2 Else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1566.2.3 If-Else Inside If/Else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1586.2.4 Truncation of Conditional Evaluations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162Part II Loops7 For-Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1737.1 Using For-Loops for Repetition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1737.2 Iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1807.2.1 Simple Iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1807.2.2 Iteration with an Auxiliary Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1867.3 Double For-Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1877.4 Computing the Maximum and Minimum in a Series of Numbers . . . . . . . . . . . . . . . . 1937.5 A Betting Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1957.5.1 For-Loops with Skipped Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1957.5.2 The Statements continue and break . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1997.6 Computing the Fibonacci Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2018 Formatted Printing Using printf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2118.1 General Rules for printf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2118.2 Formatted Printing of String Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2128.3 Formatted Printing of Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214Contents xi8.4 Formatted Printing of Floating Point Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2168.5 Printing the Fibonacci Sequence (Reprise). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2189 Classes String and StringBuilder. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2259.1 Methods for Obtaining Information from String Data . . . . . . . . . . . . . . . . . . . . . . . 2259.2 Methods for Comparing String Data with Another. . . . . . . . . . . . . . . . . . . . . . . . . . 2289.2.1 The Equality Test and the Comparison in Dictionary Order . . . . . . . . . . . . . 2289.2.2 The Prefix and Suffix Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2309.3 Methods for Searching for a Pattern in a String Data . . . . . . . . . . . . . . . . . . . . . . . . 2319.4 Methods for Creating New String Data from Another . . . . . . . . . . . . . . . . . . . . . . . 2349.4.1 String.format. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2379.5 Class StringBuilder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23710 The Switch Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24510.1 The Syntax of Switch Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24510.2 Using a char Data in a Switch-Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25210.3 Using a String Data in a Switch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25711 While-Loops and Do-While Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26311.1 Using While-Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26311.1.1 The Syntax of While-Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26311.1.2 Summing Input Numbers Until the Total Reaches a Goal . . . . . . . . . . . . . . . 26511.1.3 Integer Overflow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26611.1.4 Vending Machines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26811.1.5 The Collatz Conjecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27011.1.6 Covnerting Decimal Numbers to Binary Numbers . . . . . . . . . . . . . . . . . . . . . 27311.1.7 Infinite Loops and Their Termination . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27611.2 Using Do-While Loops. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27611.2.1 The Syntax of Do-While Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27611.2.2 “Waiting for Godot” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27711.2.3 Converting Decimal Numbers to Binary Numbers (Reprise) . . . . . . . . . . . . 27811.3 CTRL-D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27911.4 Approximating the Square Root of a Real Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283Part III Arrays and Objects12 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29512.1 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29512.1.1 The Structure of an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29512.1.2 Computing the Number of Occurrences. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29612.1.3 ArrayIndexOutOfBoundsException . . . . . . . . . . . . . . . . . . . . . . . . . 30312.2 Relative Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30512.2.1 The Concept of Relative Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30512.2.2 Calculating the BMI for a Range of Weight Values . . . . . . . . . . . . . . . . . . . . 30612.2.3 Counting the Occurrences of Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30712.3 Arrays of boolean Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31012.4 Using Multiple Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31412.5 String Methods That Return an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317xii Contents13 The Class Arrays and Resizing Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32513.1 The Class Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32513.2 Reordering Elements in an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32913.2.1 Reversing the Order of Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33213.2.2 Cyclic Shifting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33213.3 Modifications of an Array That Require Resizing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33613.3.1 Insertion and Deletion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33613.3.2 Adjoining Two Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34113.4 args . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34213.5 Searching for an Element in an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34313.5.1 Sequential Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34313.5.2 Binary Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34413.6 Arrays with Capacity and Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34514 Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35714.1 Rectangular Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35714.1.1 Defining Multi-Dimensional Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35714.1.2 Summing the Elements in Subsequences. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35814.2 Jagged Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36115 Class File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36715.1 Class File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36715.1.1 The File Path and the Instantiation of a File Object . . . . . . . . . . . . . . . . . . 36715.1.2 File Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36815.1.3 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37015.1.4 File Listing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37615.2 Using Scanner Objects to Read from Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37815.3 Using PrintStream to Write to Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38416 Designing Object Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39116.1 Packaging a Group of Data as an Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39116.1.1 The Position of a Game Piece . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39116.1.2 Private Instance Variables and the toString Method . . . . . . . . . . . . . . . . . 39516.1.3 Using Constants in an Object Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39816.1.4 Information Hiding. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40216.2 An Object Class Representing a Bank Account . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40816.3 Array with Capacity and Size (Reprise) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415Part IV Advanced Concepts17 Interfaces, Inheritance, and Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42717.1 Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42717.1.1 The Structure of an Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42717.1.2 A Simple Pizza Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42717.2 Subclasses and Superclasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43517.2.1 Extending Existing Classes and Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . 43517.2.2 Writing Extensions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43717.3 Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445Contents xiii17.4 Boxed Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44617.5 Interface Comparable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44718 Generic Class Parameters and the Java Collection Framework . . . . . . . . . . . . . . . . . . . . 45718.1 ArrayList . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45718.1.1 Maintaining a Collection of Merchandise Items . . . . . . . . . . . . . . . . . . . . . . . 45718.1.2 The Class for Merchandise Item . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45718.1.3 The Comparator Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45818.1.4 The Collection of Merchandise Items That Uses ArrayList . . . . . . . . . . 45918.1.5 The Main Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46318.2 The Dynamic Maintenance of the Largest K Values . . . . . . . . . . . . . . . . . . . . . . . . . . . 47118.3 The Java Collection Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47318.3.1 The Framework. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47318.3.2 Some Classes from the Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47518.3.3 A Demonstration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47719 Online and Recursive Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48519.1 Online Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48519.1.1 The Definition of Online Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48519.1.2 Computing Recurrences. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48619.1.3 Computing the Factorial Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48919.2 Recursive Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49219.2.1 Computing the Factorial Function Recursively . . . . . . . . . . . . . . . . . . . . . . . . 49219.2.2 The Greatest Common Divisor of Two Integers . . . . . . . . . . . . . . . . . . . . . . . 49719.2.3 The Tower of Hanoi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507List of FiguresFig. 1.1 The program layers, JVM, JRE, and JDK ................................ 4Fig. 1.2 A screen short of Terminal on a Mac OS X machine ....................... 4Fig. 1.3 An IDE screen of Eclipse ............................................. 5Fig. 1.4 The compilation and execution of HelloWorld.java ................... 6Fig. 3.1 The results of five consecutive calls of next . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73Fig. 3.2 The results of executing a program that uses next and nextLine . . . . . . . . . . 77Fig. 4.1 The method calls in Rectangle01 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91Fig. 4.2 The method calls in Rectangle02 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93Fig. 4.3 The decomposition of actions in the generation of the quadrant . . . . . . . . . . . . . . 94Fig. 4.4 The dependency among methods in OldMacDonaldDecomposed.java . . 101Fig. 4.5 The dependency among methods in the two source code . . . . . . . . . . . . . . . . . . . . 104Fig. 5.1 The call-by-reference concept . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116Fig. 6.1 The execution diagram of an if-statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150Fig. 6.2 The execution diagram of Temperature01.java . . . . . . . . . . . . . . . . . . . . . . 152Fig. 6.3 The combinations of temperature and humidity considered in Temperature03. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154Fig. 6.4 The execution diagram of an if-else statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156Fig. 6.5 A hypothetical situation with interwoven conditions . . . . . . . . . . . . . . . . . . . . . . . 160Fig. 7.1 A generic flow chart of for-loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176Fig. 7.2 The code execution diagram of ForExample . . . . . . . . . . . . . . . . . . . . . . . . . . . 177Fig. 11.1 A diagram that represents the while-loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264Fig. 12.1 A view of an array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296Fig. 13.1 Swapping values between two array elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331Fig. 13.2 Reversing the order of appearance of elements in an array . . . . . . . . . . . . . . . . . . 332Fig. 13.3 The results obtained by executing cyclic shifts . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333Fig. 13.4 An algorithm for left cyclic shift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334Fig. 13.7 An array with capacity and size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346xvxvi List of FiguresFig. 13.8 The concept of a array with capacity and size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346Fig. 14.1 The structure of a multi-dimensional array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358Fig. 15.1 The mechanism for handling run-time errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371Fig. 16.1 An 8 × 8 game board . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392Fig. 16.3 A black box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402Fig. 17.1 Two interfaces and their implementations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436Fig. 17.2 A PizzaComplex object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438Fig. 18.1 The Java Collection Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474Fig. 18.2 A LinkedList. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476Fig. 18.3 A hash table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477Fig. 19.1 The value passing that occurs during the computation of the factorial (part 1) . . 495Fig. 19.2 The value passing that occurs during the computation of the factorial (part 2) . . 495Fig. 19.3 The value passing that occurs during the computation of the factorial (part 3) . . 495Fig. 19.4 The value passing that occurs during the computation of the factorial (part 4) . . 495Fig. 19.5 The value passing that occurs during the computation of the factorial (part 5) . . 495Fig. 19.6 The value passing that occurs during the computation of the factorial (part 6) . . 496Fig. 19.7 The value passing that occurs during the computation of the factorial (part 7) . . 496Fig. 19.8 The value passing that occurs during the computation of the factorial (part 8) . . 496Fig. 19.9 The value passing that occurs during the computation of the factorial (part 9) . . 496Fig. 19.10 The value passing that occurs during the computation of the factorial (part 10) . 496Fig. 19.11 The value passing that occurs during the computation of the factorial (part 11) . 497Fig. 19.12 An example of the tower of Hanoi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500Fig. 19.13 The solution to a small Tower of Hanoi problem . . . . . . . . . . . . . . . . . . . . . . . . . . 502List of TablesTable 1.1 A short list of commands.............................................. 5Table 1.2 The list of meaningful symbols in Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21Table 2.1 The primitive data type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35Table 2.2 The list of reserved words in Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39Table 2.3 The selection of number types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45Table 3.1 Selected methods of Scanner . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75Table 5.1 One-parameter functions in Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125Table 5.2 Two-parameter functions in Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125Table 9.1 A list of String methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226Table 10.1 The output generated by the three examples of switch . . . . . . . . . . . . . . . . . . . 246Table 15.1 A list of File methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369

下载声明:

本站资源均有第三方用户自行上传分享推荐,非本站自制,仅供玩家做交流学习之用!切勿用于商业用途!游戏作品版权归原作者享有,如有版权问题,请附带版权证明至邮件,本平台将应您的要求删除。
相关推荐:

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复