Ebook Java: Giải Quyết Vấn Đề Hướng Đối Tượng - Phiên Bản Thứ Ba, Tập 1

Khám phá giải quyết vấn đề hướng đối tượng trong Java qua Ebook Java Java Java, ấn bản thứ ba, phần 1. Nâng cao kỹ năng lập trình hiệu quả.

Trường đại học

Trinity College

Chuyên ngành

Computer Science

Người đăng

Ẩn danh

Thể loại

Sách

2017

408
4
0

Phí lưu trữ

75 Point

Mục lục chi tiết

Preface to the Open Source Edition

Preface to the Third Edition

Computers, Objects, and Java

1. Java Program Design and Development

2. Objects: Defining, Creating, and Using

3. Methods: Communicating with Objects

4. Input/Output: Designing the User Interface

5. Java Data and Operators

6. Control Structures

7. Strings and String Processing

8. Inheritance and Polymorphism

9. Arrays and Array Processing

10. Exceptions: When Things Go Wrong

11. Files and Streams

12. Recursive Problem Solving

13. Graphical User Interfaces

14. Threads and Concurrent Programming

15. Sockets and Networking

16. Data Structures: Lists, Stacks, and Queues

Coding Conventions

The Java Development Kit

The ASCII and Unicode Character Sets

Java Keywords

Operator Precedence Hierarchy

Java Inner Classes

Java Autoboxing and Enumeration

Java and UML Resources

Computers, Objects, and Java

0.2. What Is a Computer?

0.3. Networks, the Internet and the World Wide Web

0.4. Why Study Programming?

0.7. What Is Object-Oriented Programming?

1. Java Program Design and Development

1.2. Designing Good Programs

1.3. Designing a Riddle Program

1.4. Java Language Elements

1.5. Editing, Compiling, and Running a Java Program

1.6. From the Java Library: System and PrintStream

2. Objects: Using, Creating, and Defining

2.2. Using String Objects

2.3. Drawing Shapes with a Graphics Object (Optional)

2.5. CASE STUDY: Simulating a Two-Person Game

2.6. From the Java Library: java

3. Methods: Communicating with Objects

3.2. Passing Information to an Object

3.4. Retrieving Information from an Object

3.5. Passing a Value and Passing a Reference

3.6. Flow of Control: Control Structures

3.7. Testing an Improved OneRowNim

3.8. From the Java Library java

3.9. Object-Oriented Design: Inheritance and Polymorphism

3.10. Drawing Lines and Defining Graphical Methods (Optional)

4. Input/Output: Designing the User Interface

4.2. The User Interface

4.3. A Command-Line Interface

4.4. A Graphical User Interface (GUI)

4.5. Case Study: The One Row Nim Game

4.6. From the Java Library: java.File and File Input (Optional)

5. Java Data and Operators

5.2. Boolean Data and Operators

5.3. Numeric Data and Operators

5.4. From the Java Library java

5.5. Numeric Processing Examples

5.6. From the Java Library java

5.7. Character Data and Operators

5.8. Example: Character Conversions

5.9. Problem Solving = Representation + Action

6. Control Structures

6.2. Flow of Control: Repetition Structures

6.4. Example: Car Loan

6.5. Graphics Example: Drawing a Checkerboard

6.7. Example: Computing Averages

6.8. Example: Data Validation

6.9. Principles of Loop Design

6.10. The switch Multiway Selection Structure

6.11. OBJECT-ORIENTED DESIGN: Structured Programming

7. Strings and String Processing

7.3. Finding Things Within a String

7.4. Example: Keyword Search

7.5. From the Java Library: java

7.6. Retrieving Parts of Strings

7.7. Example: Processing Names and Passwords

7.8. Processing Each Character in a String

7.10. From the Java Library: java

7.11. Handling Text in a Graphics Context (Optional)

8. Inheritance and Polymorphism

8.2. Java’s Inheritance Mechanism

8.3. Abstract Classes, Interfaces, and Polymorphism

8.4. Example: A Toggle Button

8.5. Example: The Cipher Class Hierarchy

8.6. Case Study: A Two Player Game Hierarchy

8.7. Principles Of Object-Oriented Design

9. Arrays and Array Processing

9.2. One-Dimensional Arrays

9.3. Simple Array Examples

9.4. Example: Counting Frequencies of Letters

9.5. Array Algorithms: Sorting

9.6. Array Algorithms: Searching

9.7. Two-Dimensional Arrays

9.9. OBJECT-ORIENTED DESIGN: Polymorphic Sorting (Optional)

9.10. From the Java Library: java

9.11. Case Study: An N-Player Computer Game

9.12. A GUI-Based Game (Optional Graphics)

10. Exceptions: When Things Go Wrong

10.2. Handling Exceptional Conditions

10.3. Java’s Exception Hierarchy

10.4. Handling Exceptions Within a Program

10.5. Error Handling and Robust Program Design

10.6. Creating and Throwing Your Own Exceptions

10.7. From the Java Library: JOptionPane

11. Files and Streams: Input/Output Techniques

11.2. Streams and Files

11.3. CASE STUDY: Reading and Writing Text Files

11.4. The File Class

11.5. Example: Reading and Writing Binary Files

11.6. Object Serialization: Reading and Writing Objects

11.7. From the Java Library javax

11.8. Using File Data in Programs

12. Recursive Problem Solving

12.3. Recursive String Methods

12.4. Recursive Array Processing

12.5. Example: Drawing (Recursive) Fractals

12.6. OBJECT-ORIENTED DESIGN: Tail Recursion

12.7. OBJECT-ORIENTED DESIGN: Recursion or Iteration?

12.8. From the Java Library: javax

13. Graphical User Interfaces

13.2. Java GUIs: From AWT to Swing

13.3. The Swing Component Set

13.4. OBJECT-ORIENTED DESIGN: Model-View-Controller Architecture

13.5. The Java Event Model

13.6. CASE STUDY: Designing a Basic GUI

13.7. Containers and Layout Managers

13.8. Checkboxes, Radio Buttons, and Borders

13.9. Menus and Scroll Panes

14. Threads and Concurrent Programming

14.2. What Is a Thread?

14.3. From the Java Library: java

14.4. Thread States and Life Cycle

14.5. Using Threads to Improve Interface Responsiveness

14.6. CASE STUDY: Cooperating Threads

14.7. CASE STUDY: The Game of Pong

15. Sockets and Networking

15.2. An Overview of Networks

15.3. Using Multimedia Network Resources for a Graphical Program

15.4. From the Java Library: java

15.5. The Slide Show Program

15.6. Adding Text Network Resources for an Application

15.7. Client/Server Communication via Sockets

15.8. CASE STUDY: Generic Client/Server Classes

15.9. Playing One Row Nim Over the Network

15.10. Java Network Security Restrictions

15.11. Java Servlets and Java Server Pages

16. Data Structures: Lists, Stacks, and Queues

16.2. The Linked List Data Structure

16.3. OBJECT-ORIENTED DESIGN: The List Abstract Data Type (ADT)

16.4. The Stack ADT

16.5. The Queue ADT

16.6. From the Java Library: The Java Collections Framework and Generic Types

16.7. Using the Set and Map Interfaces

16.8. The Binary Search Tree Data Structure

Tóm tắt

I. Tổng Quan Về Giải Quyết Vấn Đề Hướng Đối Tượng Trong Java

Giải quyết vấn đề hướng đối tượng trong Java là một chủ đề quan trọng trong lập trình hiện đại. Java, với tính năng hướng đối tượng mạnh mẽ, cho phép lập trình viên xây dựng các ứng dụng phức tạp một cách hiệu quả. Việc hiểu rõ các nguyên tắc cơ bản của lập trình hướng đối tượng sẽ giúp lập trình viên phát triển kỹ năng và tạo ra các sản phẩm chất lượng cao.

1.1. Khái Niệm Cơ Bản Về Lập Trình Hướng Đối Tượng

Lập trình hướng đối tượng (OOP) là một phương pháp lập trình dựa trên các đối tượng. Các đối tượng này có thể chứa dữ liệu và phương thức. OOP giúp tổ chức mã nguồn một cách rõ ràng và dễ bảo trì.

1.2. Tại Sao Nên Sử Dụng Java Trong Lập Trình Hướng Đối Tượng

Java là một ngôn ngữ lập trình phổ biến với tính năng hướng đối tượng mạnh mẽ. Nó cung cấp nhiều thư viện và công cụ hỗ trợ, giúp lập trình viên dễ dàng phát triển ứng dụng.

II. Những Thách Thức Trong Giải Quyết Vấn Đề Hướng Đối Tượng

Mặc dù Java cung cấp nhiều công cụ hữu ích, nhưng việc giải quyết vấn đề hướng đối tượng vẫn gặp phải nhiều thách thức. Các lập trình viên thường phải đối mặt với việc thiết kế cấu trúc lớp, quản lý mối quan hệ giữa các đối tượng và xử lý ngoại lệ.

2.1. Thiết Kế Cấu Trúc Lớp Trong Java

Thiết kế cấu trúc lớp là một trong những thách thức lớn nhất trong lập trình hướng đối tượng. Lập trình viên cần xác định các thuộc tính và phương thức của lớp một cách hợp lý để đảm bảo tính khả thi và hiệu quả.

2.2. Quản Lý Mối Quan Hệ Giữa Các Đối Tượng

Mối quan hệ giữa các đối tượng như kế thừa và đa hình có thể gây khó khăn cho lập trình viên. Việc hiểu rõ các nguyên tắc SOLID sẽ giúp cải thiện thiết kế và giảm thiểu lỗi.

III. Phương Pháp Giải Quyết Vấn Đề Hướng Đối Tượng Trong Java

Để giải quyết vấn đề hướng đối tượng trong Java, lập trình viên có thể áp dụng một số phương pháp và kỹ thuật. Việc sử dụng các nguyên tắc thiết kế như SOLID và mô hình hóa UML sẽ giúp cải thiện chất lượng mã nguồn.

3.1. Nguyên Tắc SOLID Trong Java

Nguyên tắc SOLID bao gồm năm nguyên tắc thiết kế giúp lập trình viên xây dựng mã nguồn dễ bảo trì và mở rộng. Việc áp dụng các nguyên tắc này sẽ giúp giảm thiểu lỗi và cải thiện hiệu suất.

3.2. Mô Hình Hóa Đối Tượng Bằng UML

Mô hình hóa UML giúp lập trình viên hình dung cấu trúc và mối quan hệ giữa các đối tượng. Việc sử dụng UML trong thiết kế sẽ giúp cải thiện khả năng giao tiếp và hiểu biết giữa các thành viên trong nhóm.

IV. Ứng Dụng Thực Tiễn Của Giải Quyết Vấn Đề Hướng Đối Tượng

Giải quyết vấn đề hướng đối tượng trong Java không chỉ là lý thuyết mà còn có nhiều ứng dụng thực tiễn. Các ứng dụng này có thể được tìm thấy trong nhiều lĩnh vực như phát triển phần mềm, trò chơi, và hệ thống quản lý.

4.1. Phát Triển Phần Mềm Với Java

Java được sử dụng rộng rãi trong phát triển phần mềm nhờ vào tính năng hướng đối tượng. Các ứng dụng doanh nghiệp thường được xây dựng trên nền tảng Java để đảm bảo tính ổn định và bảo mật.

4.2. Ứng Dụng Trong Lập Trình Trò Chơi

Nhiều trò chơi hiện đại được phát triển bằng Java nhờ vào khả năng xử lý đồ họa và âm thanh. Việc áp dụng OOP giúp tổ chức mã nguồn một cách hiệu quả và dễ bảo trì.

V. Kết Luận Về Giải Quyết Vấn Đề Hướng Đối Tượng Trong Java

Giải quyết vấn đề hướng đối tượng trong Java là một kỹ năng quan trọng cho lập trình viên. Việc nắm vững các nguyên tắc và phương pháp sẽ giúp lập trình viên phát triển ứng dụng chất lượng cao và dễ bảo trì.

5.1. Tương Lai Của Lập Trình Hướng Đối Tượng

Lập trình hướng đối tượng sẽ tiếp tục phát triển và đóng vai trò quan trọng trong tương lai của công nghệ. Việc áp dụng các công nghệ mới sẽ giúp cải thiện hiệu suất và khả năng mở rộng của ứng dụng.

5.2. Lời Khuyên Cho Lập Trình Viên Mới

Lập trình viên mới nên bắt đầu từ những khái niệm cơ bản và dần dần áp dụng các nguyên tắc thiết kế. Việc thực hành thường xuyên sẽ giúp cải thiện kỹ năng và hiểu biết về lập trình hướng đối tượng.

17/07/2025
Ebook java java java object oriented problem solving third edition part 1

Trích đoạn nội dung tài liệu

Java, Java, Java Object-Oriented Problem Solving Third Edition R. Walde Trinity College Hartford, CT June 25, 2017 This work is licensed under a Creative Commons Attribution 4.0 International License (CC BY 4. This book was previously published by Pearson Education, Inc. Preface to the Open Source Edition Java, Java, Java, 3e was previously published by Pearson Education, Inc.

The first edition (2000) and the second edition (2003) were published by Prentice-Hall. In 2010 Pearson Education, Inc. reassigned the copyright to the authors, and we are happy now to be able to make the book available under an open source license. This PDF edition of the book is available under a Creative Commons Attribution 4.0 International License, which allows the book to be used, modified, and shared with attribution: (https://creativecommons.org/licenses/by/4.

– Ralph Morelli and Ralph Walde – Hartford, CT – December 30, 2016 i ii Preface to the Third Edition We have designed this third edition of Java, Java, Java to be suitable for a typical Introduction to Computer Science (CS1) course or for a slightly more advanced Java as a Second Language course. This edition retains the “objects first” approach to programming and problem solving that was characteristic of the first two editions. Throughout the text we emphasize careful coverage of Java language features, introductory programming concepts, and object-oriented design principles. The third edition retains many of the features of the first two editions, including: • Early Introduction of Objects • Emphasis on Object Oriented Design (OOD) • Unified Modeling Language (UML) Diagrams • Self-study Exercises with Answers • Programming, Debugging, and Design Tips.

• From the Java Library Sections • Object-Oriented Design Sections • End-of-Chapter Exercises • Companion Web Site, with Power Points and other Resources The In the Laboratory sections from the first two editions have been moved onto the book’s Companion Web Site. Table 1 shows the Table of Contents for the third edition. What’s New in the Third Edition The third edition has the following substantive changes: • Although the book retains its emphasis on a “running example” that is revisited in several chapters, the CyberPet examples have been replaced with a collection of games and puzzle examples. The CyberPet examples from earlier editions will be available on the Companion Web Site.

iii iv Table 1: Table of Contents for the Third Edition. Chapter Topic Chapter 0 Computers, Objects, and Java (revised) Chapter 1 Java Program Design and Development Chapter 2 Objects: Defining, Creating, and Using Chapter 3 Methods: Communicating with Objects (revised) Chapter 4 Input/Output: Designing the User Interface (new) Chapter 5 Java Data and Operators Chapter 6 Control Structures Chapter 7 Strings and String Processing Chapter 8 Inheritance and Polymorphism (new) Chapter 9 Arrays and Array Processing Chapter 10 Exceptions: When Things Go Wrong Chapter 11 Files and Streams Chapter 12 Recursive Problem Solving Chapter 13 Graphical User Interfaces Chapter 14 Threads and Concurrent Programming Chapter 15 Sockets and Networking (expanded) Chapter 16 Data Structures: Lists, Stacks, and Queues (revised and expanded) • Chapters 0 (Computers, Objects, and Java) and 1 (Java Program De- sign and Development) have been substantially reorganized and rewritten. The new presentation is designed to reduce the pace with which new concepts are introduced. The treatment of object- oriented (OO) and UML concepts has also been simplified, and some of the more challenging OO topics, such as polymorphism, have been moved to a new Chapter 8.

• The new Java 1.5 Scanner class is introduced in Chapter 2 and is used to perform simple input operations. • Chapter 4 (Input/Output: Designing the User Interface) has been completely written. Rather than relying primarily on applet inter- faces, as in the second edition, this new chapter provides indepen- dent introductions to both a command-line interface and a graphi- cal user interface (GUI). Instructors can choose the type of interface that best suits their teaching style.

The command-line interface is based on the BufferedReader class and is used throughout the rest of the text. The GUI is designed to work with either graphi- cal applications or applets. Both approaches are carefully presented to highlight the fundamentals of user-interface design. The chapter concludes with an optional section that introduces file I/O using the new Scanner class.

• Much of the discussion of inheritance and polymorphism, which was previously woven through the first five chapters in the second edition, has been integrated into a new Chapter 8. • An optional graphics track is woven throughout the text. Beginning with simple examples in Chapters 1 and 2, this track also includes v some of the examples that were previously presented in Chapter 10 of the second edition. • Chapter 15, on Sockets and Networking, is expanded to cover some of the more advanced Java technologies that have emerged, includ- ing servlets and Java Server Pages.

• Chapter 16, on Data Structures, has been refocused on how to use data structures. It makes greater use of Java’s Collection Framework, including the LinkedList and Stack classes and the List inter- face. It has been expanded to cover some advanced data structures, such as sets, maps, and binary search trees. The Essentials Edition An Essentials Edition of the third edition, which will include Chapters 0- 12, will be published as a separate title.

The Essentials Edition will cover those topics (Chapters 0-9) that are covered in almost all introductory (CS1) courses, but it will also include topics (Exceptions, File I/O, and Recursion) that many CS1 instructors have requested. Why Start with Objects? The Third Edition still takes an objects-early approach to teaching Java, with the assumption that teaching beginners the “big picture” early gives them more time to master the principles of object-oriented programming. This approach seems now to have gained in popularity as more and more instructors have begun to appreciate the advantages of the object-oriented perspective. If OO concepts are introduced late, it is much too easy to skip over them when push comes to shove in the course.

The first time I taught Java in our CS1 course I followed the same ap- proach I had been taking in teaching C and C++ — namely, start with the basic language features and structured programming concepts and then, somewhere around midterm, introduce object orientation. This approach was familiar, for it was one taken in most of the textbooks then available in both Java and C++. One problem with this approach was that many students failed to get the big picture. They could understand loops, if-else constructs, and arith- metic expressions, but they had difficulty decomposing a programming problem into a well-organized Java program.

Also, it seemed that this procedural approach failed to take advantage of the strengths of Java’s object orientation. Why teach an object-oriented language if you’re going to treat it like C or Pascal? I was reminded of a similar situation that existed when Pascal was the predominant CS1 language. Back then the main hurdle for beginners was procedural abstraction — learning the basic mechanisms of procedure call vi and parameter passing and learning how to design programs as a collec- tion of procedures. Oh! Pascal!, my favorite introductory text, was typical of a “procedures early” approach.

It covered procedures and parameters in Chapter 2, right after covering the assignment and I/O constructs in Chapter 1. It then covered program design and organization in Chap- ter 3. It didn’t get into loops, if-else, and other structured programming concepts until Chapter 4 and beyond. Today, the main hurdle for beginners is the concept of object abstraction.

Beginning programmers must be able to see a program as a collection of interacting objects and must learn how to decompose programming prob- lems into well-designed objects. Object orientation subsumes both proce- dural abstraction and structured programming concepts from the Pascal days. Teaching objects-early takes a top-down approach to these three im- portant concepts. The sooner you begin to introduce objects and classes, the better the chances that students will master the important principles of object orientation.

Java is a good language for introducing object orientation. Its object model is better organized than C++. In C++ it is easy to “work around” or completely ignore OO features and treat the language like C. In Java there are good opportunities for motivating the discussion of object orien- tation.

For example, it’s almost impossible to discuss GUI-based Java ap- plications without discussing inheritance and polymorphism. Thus rather than using contrived examples of OO concepts, instructors can use some of Java’s basic features — the class library, Swing and GUI components — to motivate these discussions in a natural way. Organization of the Text The book is still organized into three main parts. Part I (Chapters 0-4) in- troduces the basic concepts of object orientation and the basic features of the Java language.

Part II (Chapters 5-9) focuses on remaining language el- ements, including data types, control structures, string and array process- ing, and inheritance and polymorphism. Part III (Chapters 10-16) covers advanced topics, including exceptions, file I/O, recursion, GUIs, threads and concurrent programming, sockets and networking, data structures, servlets, and Java Server Pages. The first two parts make up the topics that are typically covered in an introductory CS1 course. The chapters in Part III are self-contained and can be selectively added to the end of a CS1 course if time permits.

The first part (Chapters 0 through 4) introduces the basic concepts of object orientation, including objects, classes, methods, parameter passing, information hiding, and a little taste of inheritance, and polymorphism. The primary focus in these chapters is on introducing the basic idea that an object-oriented program is a collection of objects that communicate and cooperate with each other to solve problems. Java language elements are introduced as needed to reinforce this idea. Students are given the basic building blocks for constructing Java programs from scratch.

Although the programs in the first few chapters have limited function- ality in terms of control structures and data types, the priority is placed vii Table 2: A one-semester course. Weeks Topics Chapters 1 Object Orientation, UML Chapter 0 Program Design and Development Chapter 1 2-3 Objects and Class Definitions Chapter 2 Methods and Parameters Chapter 3 Selection structure (if-else) 4 User Interfaces and I/O Chapter 4 5 Data Types and Operators Chapter 5 6–7 Control Structures (Loops) Chapter 6 Structured Programming 8 String Processing (loops) Chapter 7 9 Inheritance and Polymorphism Chapter 8 10 Array Processing Chapter 9 11 Recursion Chapter 12 12 Advanced Topic (Exceptions) Chapter 10 13 Advanced Topic (GUIs) Chapter 11 Advanced Topic (Threads) Chapter 15 on how objects are constructed and how they interact with each other through method calls and parameter passing. The second part (Chapters 5 through 9) focuses on the remaining lan- guage elements, including data types and operators (Chapter 5), control structures (Chapter 6), strings (Chapter 7), and arrays (Chapter 9). It also provides thorough coverage of inheritance and polymorphism, the primary mechanisms of object orientation: (Chapter 8).

Topics from these chapters can be used selectively depending on instructor and student interest. Throughout the book, key concepts are introduced through simple, easy-to-grasp examples. Many of the concepts are used to create a set of games, which are used as a running example throughout the text. Our pedagogical approach focuses on design.

Rather than starting of with lan- guage details, programming examples are carefully developed with an emphasis on the principles of object-oriented design. Table2 provides an example syllabus from our one-semester CS1 course. Our semester is 13 weeks (plus one reading week during which classes do not meet). We pick and choose from among the advanced topics during the last two weeks of the course, depending on the interests and skill levels of the students.

Ralph Morelli June 25, 2017 viii Contents 0 Computers, Objects, and Java 1 0.2 What Is a Computer? .3 Networks, the Internet and the World Wide Web .4 Why Study Programming? .7 What Is Object-Oriented Programming?. 11 1 Java Program Design and Development 23 1.2 Designing Good Programs .

Nội dung được bảo vệ bản quyền — Tải xuống đầy đủ

Tài liệu "Giải Quyết Vấn Đề Hướng Đối Tượng Trong Java - Tập 1" cung cấp cái nhìn sâu sắc về lập trình hướng đối tượng (OOP) trong ngôn ngữ Java, giúp người đọc hiểu rõ hơn về các khái niệm cơ bản và ứng dụng thực tiễn của OOP. Tài liệu này không chỉ giải thích các nguyên lý cốt lõi như lớp, đối tượng, kế thừa và đa hình, mà còn đưa ra các ví dụ minh họa cụ thể, giúp người học dễ dàng nắm bắt và áp dụng vào thực tế.

Để mở rộng kiến thức của bạn về lập trình hướng đối tượng, bạn có thể tham khảo thêm tài liệu Chương 2 hướng đối tượng trong Java, nơi cung cấp những thông tin chi tiết hơn về các khái niệm OOP trong Java. Ngoài ra, tài liệu Giáo trình lập trình hướng đối tượng nghề tin học ứng dụng cao đẳng sẽ giúp bạn có cái nhìn tổng quát hơn về ứng dụng OOP trong lĩnh vực công nghệ thông tin. Cuối cùng, tài liệu Sổ tay kiến thức OOP là một nguồn tài liệu hữu ích để bạn có thể tra cứu và củng cố kiến thức của mình về lập trình hướng đối tượng.

Những tài liệu này sẽ giúp bạn mở rộng hiểu biết và nâng cao kỹ năng lập trình của mình trong lĩnh vực OOP.