Book Details

Java, Java, Java Object-Oriented Problem Solving

Downloads: 587 Views: 1060

  • Author: R. Morelli and R.Walde

Description

Welcome to Java, Java, Java, a book that introduces you to object-oriented programming using the Java language. We introduce the Java programming language and conclude the chapter by exploring object-oriented programming principles and how Java is an object-oriented programming language.

Table of Contents

0 Computers, Objects, and Java 1
0.1 Welcome . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
0.2 What Is a Computer? . . . . . . . . . . . . . . . . . . . . . . . 2
0.3 Networks, the Internet and the World Wide Web . . . . . . . 4
0.4 Why Study Programming? . . . . . . . . . . . . . . . . . . . . 6
0.5 Programming Languages . . . . . . . . . . . . . . . . . . . . 7
0.6 Why Java? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
0.7 What Is Object-Oriented Programming? . . . . . . . . . . . . 11
1 Java Program Design and Development 23
1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
1.2 Designing Good Programs . . . . . . . . . . . . . . . . . . . . 24
1.3 Designing a Riddle Program . . . . . . . . . . . . . . . . . . . 26
1.4 Java Language Elements . . . . . . . . . . . . . . . . . . . . . 34
1.5 Editing, Compiling, and Running a Java Program . . . . . . 49
1.6 From the Java Library: System and
PrintStream . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
2 Objects: Using, Creating, and Defining 63
2.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
2.2 Using String Objects . . . . . . . . . . . . . . . . . . . . . . 64
2.3 Drawing Shapes with a Graphics Object (Optional) . . . . 68
2.4 Class Definition . . . . . . . . . . . . . . . . . . . . . . . . . . 72
2.5 CASE STUDY: Simulating a Two-Person Game . . . . . . . . 77
2.6 From the Java Library: java.util.Scanner. . . . . . . . . 91
3 Methods: Communicating with Objects 101
3.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
3.2 Passing Information to an Object . . . . . . . . . . . . . . . . 102
3.3 Constructor Methods . . . . . . . . . . . . . . . . . . . . . . . 110
3.4 Retrieving Information from an Object . . . . . . . . . . . . . 115
3.5 Passing a Value and Passing a Reference . . . . . . . . . . . . 118
3.6 Flow of Control: Control Structures . . . . . . . . . . . . . . . 121
3.7 Testing an Improved OneRowNim . . . . . . . . . . . . . . . . 130
3.8 From the Java Library java.lang.Object . . . . . . . . . 135
3.9 Object-Oriented Design: Inheritance and Polymorphism . . 136
3.10 Drawing Lines and Defining Graphical Methods (Optional) 138
4 Input/Output: Designing the User Interface 149
4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150

4.2 The User Interface . . . . . . . . . . . . . . . . . . . . . . . . . 150
4.3 A Command-Line Interface . . . . . . . . . . . . . . . . . . . 151
4.4 A Graphical User Interface (GUI) . . . . . . . . . . . . . . . . 160
4.5 Case Study: The One Row Nim Game . . . . . . . . . . . . . 178
4.6 From the Java Library: java.io.File
and File Input (Optional) . . . . . . . . . . . . . . . . . . . . . 185
5 Java Data and Operators 199
5.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
5.2 Boolean Data and Operators . . . . . . . . . . . . . . . . . . . 201
5.3 Numeric Data and Operators . . . . . . . . . . . . . . . . . . 207
5.4 From the Java Library java.lang.Math . . . . . . . . . . . 219
5.5 Numeric Processing Examples . . . . . . . . . . . . . . . . . 221
5.6 From the Java Library
java.text.NumberFormat . . . . . . . . . . . . . . . . . . 233
5.7 Character Data and Operators . . . . . . . . . . . . . . . . . . 235
5.8 Example: Character Conversions . . . . . . . . . . . . . . . . 239
5.9 Problem Solving = Representation + Action . . . . . . . . . . 241
6 Control Structures 253
6.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
6.2 Flow of Control: Repetition Structures . . . . . . . . . . . . . 254
6.3 Counting Loops . . . . . . . . . . . . . . . . . . . . . . . . . . 255
6.4 Example: Car Loan . . . . . . . . . . . . . . . . . . . . . . . . 264
6.5 Graphics Example: Drawing a Checkerboard . . . . . . . . . 267
6.6 Conditional Loops . . . . . . . . . . . . . . . . . . . . . . . . 271
6.7 Example: Computing Averages . . . . . . . . . . . . . . . . . 278
6.8 Example: Data Validation . . . . . . . . . . . . . . . . . . . . 282
6.9 Principles of Loop Design . . . . . . . . . . . . . . . . . . . . 282
6.10 The switch Multiway Selection Structure . . . . . . . . . . . 285
6.11 OBJECT-ORIENTED DESIGN:
Structured Programming . . . . . . . . . . . . . . . . . . . . . 289
7 Strings and String Processing 309
7.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
7.2 String Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
7.3 Finding ThingsWithin a String . . . . . . . . . . . . . . . . . 316
7.4 Example: Keyword Search . . . . . . . . . . . . . . . . . . . . 318
7.5 From the Java Library: java.lang.StringBuffer . . . . . . . . . 320
7.6 Retrieving Parts of Strings . . . . . . . . . . . . . . . . . . . . 322
7.7 Example: Processing Names and Passwords . . . . . . . . . 325
7.8 Processing Each Character in a String . . . . . . . . . . . . 326
7.9 Comparing Strings . . . . . . . . . . . . . . . . . . . . . . . . 330
7.10 From the Java Library:
java.util.StringTokenizer . . . . . . . . . . . . . . . . . . . . . 336
7.11 Handling Text in a Graphics Context
(Optional) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
8 Inheritance and Polymorphism 349
8.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
8.2 Java’s Inheritance Mechanism . . . . . . . . . . . . . . . . . . 350

8.3 Abstract Classes, Interfaces,
and Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . 359
8.4 Example: A Toggle Button . . . . . . . . . . . . . . . . . . . . 365
8.5 Example: The Cipher Class Hierarchy . . . . . . . . . . . . . 369
8.6 Case Study: A Two Player Game Hierarchy . . . . . . . . . . 375
8.7 Principles Of Object-Oriented Design . . . . . . . . . . . . . 396
9 Arrays and Array Processing 405
9.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406
9.2 One-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . 406
9.3 Simple Array Examples . . . . . . . . . . . . . . . . . . . . . 413
9.4 Example: Counting Frequencies of Letters . . . . . . . . . . . 416
9.5 Array Algorithms: Sorting . . . . . . . . . . . . . . . . . . . . 420
9.6 Array Algorithms: Searching . . . . . . . . . . . . . . . . . . 427
9.7 Two-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . 431
9.8 Multidimensional Arrays (Optional) . . . . . . . . . . . . . . 439
9.9 OBJECT-ORIENTED DESIGN:
Polymorphic Sorting (Optional) . . . . . . . . . . . . . . . . . 441
9.10 From the Java Library: java.util.Vector . . . . . . . . . . . . . 444
9.11 Case Study: An N-Player Computer Game . . . . . . . . . . 446
9.12 A GUI-Based Game (Optional Graphics) . . . . . . . . . . . . 453
10 Exceptions: When Things GoWrong 471
10.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472
10.2 Handling Exceptional Conditions . . . . . . . . . . . . . . . . 472
10.3 Java’s Exception Hierarchy . . . . . . . . . . . . . . . . . . . 474
10.4 Handling Exceptions Within a Program . . . . . . . . . . . . 478
10.5 Error Handling and Robust
Program Design . . . . . . . . . . . . . . . . . . . . . . . . . . 489
10.6 Creating and Throwing Your Own
Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
10.7 From the Java Library: JOptionPane . . . . . . . . . . . . . 501
11 Files and Streams: Input/Output Techniques 513
11.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514
11.2 Streams and Files . . . . . . . . . . . . . . . . . . . . . . . . . 514
11.3 CASE STUDY: Reading and Writing Text Files . . . . . . . . 519
11.4 The File Class . . . . . . . . . . . . . . . . . . . . . . . . . . 532
11.5 Example: Reading and Writing Binary Files . . . . . . . . . . 535
11.6 Object Serialization: Reading and Writing Objects . . . . . . 544
11.7 From the Java Library
javax.swing.JFileChooser . . . . . . . . . . . . . . . . 549
11.8 Using File Data in Programs . . . . . . . . . . . . . . . . . . . 550
12 Recursive Problem Solving 559
12.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 560
12.2 Recursive Definition . . . . . . . . . . . . . . . . . . . . . . . 563
12.3 Recursive String Methods . . . . . . . . . . . . . . . . . . . . 565
12.4 Recursive Array Processing . . . . . . . . . . . . . . . . . . . 577
12.5 Example: Drawing (Recursive) Fractals . . . . . . . . . . . . 583

12.6 OBJECT-ORIENTED DESIGN:
Tail Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . 587
12.7 OBJECT-ORIENTED DESIGN:
Recursion or Iteration? . . . . . . . . . . . . . . . . . . . . . . 588
12.8 From the Java Library:
javax.swing.JComboBox . . . . . . . . . . . . . . . . . . . . . 591
13 Graphical User Interfaces 603
13.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 604
13.2 Java GUIs: From AWT to Swing . . . . . . . . . . . . . . . . . 604
13.3 The Swing Component Set . . . . . . . . . . . . . . . . . . . . 607
13.4 OBJECT-ORIENTED DESIGN:
Model-View-Controller Architecture . . . . . . . . . . . . . . 608
13.5 The Java Event Model . . . . . . . . . . . . . . . . . . . . . . 610
13.6 CASE STUDY: Designing a Basic GUI . . . . . . . . . . . . . 614
13.7 Containers and Layout Managers . . . . . . . . . . . . . . . . 626
13.8 Checkboxes, Radio Buttons, and Borders . . . . . . . . . . . 632
13.9 Menus and Scroll Panes . . . . . . . . . . . . . . . . . . . . . 641
14 Threads and Concurrent Programming 655
14.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 656
14.2 What Is a Thread? . . . . . . . . . . . . . . . . . . . . . . . . . 656
14.3 From the Java Library: java.lang.Thread . . . . . . . . . 660
14.4 Thread States and Life Cycle . . . . . . . . . . . . . . . . . . . 666
14.5 Using Threads to Improve
Interface Responsiveness . . . . . . . . . . . . . . . . 668
14.6 CASE STUDY: Cooperating Threads . . . . . . . . . . . . . . 676
14.7 CASE STUDY: The Game of Pong . . . . . . . . . . . . . . . . 693
15 Sockets and Networking 707
15.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 708
15.2 An Overview of Networks . . . . . . . . . . . . . . . . . . . . 708
15.3 Using Network Resources from an Applet . . . . . . . . . . . 714
15.4 From the Java Library: java.net.URL . . . . . . . . . . . . 715
15.5 The Slide Show Applet . . . . . . . . . . . . . . . . . . . . . . 717
15.6 Using Network Resources from an
Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . 722
15.7 Client/Server Communication via Sockets . . . . . . . . . . 732
15.8 CASE STUDY: Generic Client/Server Classes . . . . . . . . . 737
15.9 Playing One Row Nim Over the Network . . . . . . . . . . . 747
15.10Java Network Security Restrictions . . . . . . . . . . . . . . . 754
15.11Java Servlets and Java Server Pages . . . . . . . . . . . . . . . 755
16 Data Structures: Lists, Stacks, and Queues 769
16.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 770
16.2 The Linked List Data Structure . . . . . . . . . . . . . . . . . 770
16.3 OBJECT-ORIENTED DESIGN:
The List Abstract Data Type (ADT) . . . . . . . . . . . . . . . 782
16.4 The Stack ADT . . . . . . . . . . . . . . . . . . . . . . . . . . . 788
16.5 The Queue ADT . . . . . . . . . . . . . . . . . . . . . . . . . . 790

16.6 From the Java Library: The Java Collections Framework
and Generic Types . . . . . . . . . . . . . . . . . . . . . . . . 794
16.7 Using the Set and Map Interfaces . . . . . . . . . . . . . . . 797
16.8 The Binary Search Tree Data Structure . . . . . . . . . . . . . 801
A Coding Conventions 813
B The Java Development Kit 821
C The ASCII and Unicode Character Sets 831
D Java Keywords 833
E Operator Precedence Hierarchy 835
F Java Inner Classes 837
G Java Autoboxing and Enumeration 843
H Java and UML Resources 849

Download

Share this book


Comments
comments powered by Disqus

Navigation

Social Media