To the Instructor Java Teaching Strategies
There are three popular strategies in teaching Java. The first, known as GUI-first, is to mix Java applets and GUI programming with object-oriented programming concepts. The second, known as object-first, is to introduce object-oriented programming from the start. The third strategy, known as fundamentals-first, is a step-by-step approach, first laying a sound foundation on programming concepts, control statements, and methods, then introducing object-oriented programming, and then moving on to graphical user interface (GUI), applets, and finally to exception handling, I/O, data structures, internationalization, multithreading, multimedia, and networking.
The GUI-first strategy, starting with GUI and applets, seems attractive, but requires substantial knowledge of object-oriented programming and a good understanding of the Java event-handling model; thus, students may never fully understand what they are doing.
The object-first strategy is based on the notion that objects should be introduced first because Java is an object-oriented programming language. This notion, however, overlooks the importance of the fundamental techniques required for writing programs in any programming language. Furthermore, this approach inevitably mixes static and instance variables and methods before students can fully understand classes and objects and use them to develop useful programs. Students are overwhelmed by object-oriented programming and basic rules of programming simultaneously in the early stage of learning Java. This is a common source of frustration for freshmen learning object-oriented programming.
From my own experience, confirmed by the experiences of many colleagues, I have found that learning basic logic and fundamental programming techniques like loops is a struggle for most freshmen. Students who cannot write code in procedural programming are not able to learn object-oriented programming. A good introduction on primitive data types, control statements, methods, and arrays prepares students to learn object-oriented programming. Therefore, this text adopts the fundamentals-first strategy, proceeding at a steady pace through all the necessary and important basic concepts, then moving to object-oriented programming, and then to using the object-oriented approach to build interesting GUI applications and applets with exception handling, I/O, data structures, internationalization, multithreading, multimedia, and networking.
Selection of Java Subjects
Many introductory Java texts lack sufficient breadth and do not cover all the core Java knowledge that is needed to develop useful projects. Some authors over ambitiously mix too many topics, such as Java database programming, Remote Method Invocation, JavaBeans and rapid application development, servlets, and JSP, into one introductory Java text. With this approach the coverage of programming principles tends to lose coherence.The Level 1 Certification Exam tests core Java knowledge and fundamental programming skills.
This book gives a comprehensive introduction on the fundamentals of programming in Chapters 1-5, an in-depth treatment of object-oriented programming in Chapters 6-9, extensive examples of GUI programming in Chapters 10-12, and appropriate coverage of advanced Java topics in Chapters 13-19. The book covers all the subjects required for the Level 1 Java Certification Exam.
Audience of This Book
The book is suited for both beginning and advanced students, depending on how it is used. It has been used in two-semester freshman programming courses and one-semester courses in Java as a second language. It has also been used in short training courses for experienced programmers. Computer science departments, engineering departments, and management information systems departments around the world have used this book at various levels.
Use of This Book
Every school is different. Some schools have used the book in one semester, and some have used it in a two-semester or three-semester sequence. The computer science curriculum has two important objectives: (1) to prepare students for immediate employment by teaching them marketable skills; (2) to teach students how to learn on their own. This book is designed to foster self-teaching with many complete examples, notes, tips, and cautions. Students should be able to learn on their own after mastering the first ten chapters. For students with no programming experience, an entire semester of four credit hours could be spent just on the first five chapters of the book, as we do in the first semester of the programming course at Armstrong Atlantic State University. In the second semester, we cover chapters 6-13, 17, and 19. In the first semester, students are new to programming. It takes time and patience to help them get into the mood of programming. In the second semester, we cover Chapters 6-13, 17> and 19. In the first semester, students are new to programming. It takes time and patience to help them get into the mood of programming. In the second semester, we cover Chapters 6, 7, part of 8 (up to Abstract Classes), part of 10 (up to Case Studies), 8 (Polymorphism, Dynamic Binding, and Interfaces), 10 (Event-driven programming), 11, part of 12 (just on applets), 13> part of 17 (just on file IO), 9 and 19, in this sequence (see Chapter Dependency Chart for other possible sequences). Students are capable of learning all the materials after Chapter 10 through self-teaching. The important part of the second semester is to guide the students to learn by themselves.
Over the years, I have tried many ways of teaching Java. The most effective approach is to teach it using the slides and writing, running and testing programs in the class interactively. Using the slides, you don't have to write on the board. It saves valuable lecture time. Writing, running, and testing programs in the class can hold students' attention and give them instantaneous feedback on how to program.
The Instructor's Resource CD-ROM is available for instructors using this book. It contains the following resources:
- Microsoft PowerPoint slides for lectures, with interactive buttons to view syntax-highlighted source code and to run programs without leaving the slides.
- Twelve sample exams. In general, each exam has four parts: (1) multiple-choice questions or short questions; (2) correct programming errors; (3) trace programs; (4) write programs.
- Solutions to all the exercises. Students will have access to the solutions of even-numbered exercises in the book's companion CD-ROM.
- More than forty supplemental exercises and their solutions.
- Suggested syllabi for teaching Java to freshmen, for teaching Java as a second language, and for teaching Java to corporate employees.
- Lecture notes. A number of suggested teaching strategies and activities are presented to help you in the delivery of the course.
To obtain the Instructor's Resource CD-ROM, contact your Prentice Hall sales representative. Some students have requested the solutions to the odd-numbered programming exercises. Please understand that these are for instructors only. Such requests will not be answered.The Web site also contains interactive online self-tests and other supplemental materials.
Pedagogical Features of the Book
The philosophy of the Liang Java Series is teaching by example and learning by doing. Basic features are explained by example so that you can learn by doing. The book uses the following elements to get the most out of the material:
- Objectives list what students should have learned from the chapter. This will help them to determine whether they have met the objectives after completing the chapter.
- Introduction opens the discussion with a brief overview of what to expect from the chapter.
- Programming concepts are taught by representative Examples, carefully chosen and presented in an easy-to-follow style. Each example has the problem statement, solution steps, complete source code, sample run, and review. The source code of the examples is contained in the companion CD-ROM.
- Chapter Summary reviews the important subjects that students should understand and remember. It helps them to reinforce the key concepts they have learned in the chapter.
- Review Questions help students to track their progress and evaluate their learning.
- Programming Exercises at the end of each chapter provide students with opportunities to apply the skills on their own. The trick of learning programming is practice, practice, and practice. To that end, the book provides a large number of exercises.
- Interactive Self-Test helps students to test their knowledge interactively online.It provides more than six hundred multiple-choice and true/false questions organized by chapters.
- Notes, Tips, and Cautions are inserted throughout the text to offer valuable advice and insight on important aspects of program development.
- NOTE: Provides additional information on the subject and reinforces important concepts.
- TIP: Teaches good programming style and practice.
- CAUTION: Helps students steer away from the pitfalls of programming errors.
What's New in This Edition
This book expands and improves upon Introduction to Java Programming, Third Edition. The major changes are as follows:
- The proprietary Mynput class for getting input from the console is replaced by the standard JoptionPane class. Students don't have to learn a proprietary class that is not used in the workplace.
- Part II, "Objected-Oriented Programming," is expanded into four chapters to provide an in-depth introduction to object-oriented programming and design. Strings are now in a separate chapter, and Chapter 9, "Object-Oriented Software Development," covers class design techniques.
- Every chapter is thoroughly revised and improved. Chapter 9, "Object-Oriented Software Development," is partly new; it now introduces object-oriented development using the UML approach and fosters the concept of developing reusable components. Chapter 19, "Java Data Structures" is brand-new; it introduces the Java collections framework.
- The book has been reorganized to provide flexible ordering of chapters so that instructors can easily customize their use of it. Arrays are treated in Chapter 5, but can be covered after Chapter 8. The book provides flexible ordering of chapters. You may cover Chapter 6, "Objects and Classes," after Chapter 4, "Methods." You may cover Chapter 13, "Exception Handling," after class inheritance is introduced in Chapter 8, "Class Inheritance and Interfaces." Chapter 19, "Java Data Structures," can be covered after Chapter 9, "Object-Oriented Software Development."
- Several new appendices provide readers with additional background information and supplemental material.There are more than six hundred questions in the self-test. This site also contains new supplements on computer basics that introduce the terms CPU, memory, hard disk, operating systems, and programming languages.
To the Student
There is nothing more important to the future of computing than the Internet. There is nothing more exciting on the Internet than Java. A revolutionary programming language developed by Sun Microsystems, Java has become the de facto standard for cross-platform applications and programming on the World Wide Web.
Java is a full-featured, general-purpose programming language that is capable of developing robust mission-critical applications. In recent years, Java has gained enormous popularity and has quickly become the most popular and successful programming language. Today, it is used not only for Web programming, but also for developing standalone applications across platforms on servers, desktops, and mobile devices. Many companies that once considered Java to be more hype than substance are now using it to create distributed applications accessed by customers and partners across the Internet. For every new project being developed today, companies are asking how they can use Java to make their work easier.
Java is now taught in every university. This book teaches you how to write Java programs from beginning.
Java's Design and Advantages
- Java is an object-oriented programming language. Object-oriented programming is a favored programming approach that has replaced traditional procedure-based programming techniques. An object-oriented language uses abstraction, encapsulation, inheritance, and polymorphism to provide great flexibility, modularity, and reusability for developing software.
- Java is platform-independent. Its programs can run on any platform with a Java Virtual Machine, a software component that interprets Java instructions and carries out associated actions.
- Java is distributed. Networking is inherently built-in. Simultaneous processing can occur on multiple computers on the Internet. Writing network programs is treated as simple data input and output.
- Java is multithreaded. Multithreading is the capability of a program to perform several tasks simultaneously, such as downloading a video file while playing the video at the same time. Multithreading is particularly useful in graphical user interfaces (GUI) and network programming. Multithread programming is smoothly integrated in Java. In other languages, you can only enable multithreading by calling procedures that are specific to the operating system.
- Java is secure. Computers become vulnerable when they are connected to other computers. Viruses and malicious programs can damage your computer. Java is designed with multiple layers of security that ensure proper access to private data and restrict access to disk files.
Stimulated by the promise of writing programs once and running them anywhere, Java has become the most ubiquitous programming language. Java programs run on full-featured computers, and also on consumer electronics and appliances such as Palm and mobile phones.
Because of its great potential to unite existing legacy applications written on different platforms so that they can run together, Java is perceived as a universal front-end for the enterprise database. The leading database companies, IBM, Oracle, and Sybase, have extended their commitment to Java by integrating it into their products. Oracle, for example, enables Java applications to run on its server and to deliver a complete set of Java-based development tools supporting the integration of current applications with the Web.
To first-time programmers, learning Java is like learning any high-level programming language. The fundamental point in learning programming is to develop the critical skills of formulating programmatic solutions for real problems and translating the solutions into programs using selection statements, loops, and methods.
Once you acquire the basic skills of writing programs using loops, methods, and arrays, you can start to learn object-oriented programming. You will learn to develop object-oriented software using class encapsulation and class inheritance.
Applying the concept of abstraction in the design and implementation of software projects is the key to developing software. The overriding objective of this book, therefore, is to teach students to use many levels of abstraction in solving problems and to see problems in small and in large. The examples and exercises throughout the book foster the concept of developing reusable components and using them to create projects.
Students with no programming experience should take a slow-paced approach in Part I of the book. I recommend that you complete all the exercises in Part I before moving to Chapter 6. Students new to object-oriented programming may need some time to become familiar with the concepts of objects and classes. Once the principles are mastered, programming in Java is easy and productive. Students who know object-oriented programming languages such as C++ and Smalltalk will find it easier to learn Java. In fact, Java is simpler than C++ and Smalltalk in many aspects.
Organization of the Book
This book is divided into four parts that, taken together, form a comprehensive introductory course on Java programming. Because knowledge is cumulative, the early chapters provide the conceptual basis for understanding Java and guide students through simple examples and exercises; subsequent chapters progressively present Java programming in detail, culminating with the development of comprehensive Java applications. The appendixes contain a mixed bag of topics, including an HTML tutorial.
Part I: Fundamentals of Programming
The first part of the book is a stepping stone that will prepare you to embark on the journey of learning Java. You will begin to know Java, and will learn how to write simple Java programs with primitive data types, control statements, methods, and arrays.
Chapter 1, "Introduction to Java," gives an overview of the major features of Java: object-oriented programming, platform-independence, Java bytecode, security, performance, multithreading, and networking. The chapter also shows how to create, compile, and run Java applications and applets. Simple examples of writing applications are provided, along with a brief anatomy of programming structures.
Chapter 2, "Primitive Data Types and Operations," introduces primitive data types, operators, and expressions. Important topics include identifiers, variables, constants, assignment statements, assignment expressions, primitive data types, operators, and shortcut operators. Java programming style and documentation are also addressed.
Chapter 3, "Control Statements," introduces decision and repetition statements. Java decision statements include various forms of if statements and the switch statement. Java repetition statements include the while loop, the do-while loop, and the for loop. The keywords break and continue are discussed.
Chapter 4, "Methods," introduces method creation, calling methods, passing parameters, returning values, method overloading, scope of local variables, and recursion. Applying the concept of abstraction is the key to developing software. The chapter also introduces the use of method abstraction in problem-solving. The math class for performing basic math operations is introduced.
Chapter 5, "Arrays," explores an important structure: arrays for processing data in lists and tables. You will learn how to declare, initialize, and copy arrays. Examples of using two-dimensional arrays for matrix operations are provided. This chapter also introduces popular search and sorting methods.
Part II: Object-Oriented Programming
In the book's second part, object-oriented programming is introduced. Java is a class-centric, object-oriented programming language that uses abstraction, encapsulation, inheritance, and polymorphism to provide great flexibility, modularity, and reusability in developing software. You will learn programming with objects and classes, class inheritance, interfaces, polymorphism, and developing software using the object-oriented approach.
Chapter 6, "Objects and Classes," begins with objects and classes. The important topics include defining classes, creating objects, using constructors, passing objects to methods, instance and class variables, and instance and class methods, scope of variables in the context of a class, the keyword this, and using the UML graphical notations to describe classes. Several examples are provided to demonstrate the power of the object-oriented programming approach. Students will learn the benefits (abstraction, encapsulation, and modularity) of object-oriented programming from these examples.
Chapter 7, "Strings," introduces the classes String, StringBuffer, and StringTokenizer for storing and processing strings. There are more than fifteen hundred predefined Java classes grouped in several packages. Starting with this chapter, students will gradually learn how to use Java classes to develop their own programs. The classes on strings are fine examples to demonstrate the concept of objects and classes.
Chapter 8, "Class Inheritance and Interfaces," teaches how an existing class can be extended and modified as needed. Inheritance is an extremely powerful programming technique, further extending software reusability. Java programs are all built by extending predefined Java classes. The major topics include defining subclasses, using the keyword super, using the modifiers protected, final, and abstract, polymorphism and dynamic binding, and casting objects. This chapter introduces the Object class, which is the root of all Java classes. You will also learn how to use abstract classes and interfaces.
Chapter 9, "Object-Oriented Software Development," focuses on class design. You will learn how to analyze relationships among objects and to design classes with the relationships association, aggregation, composition, strong inheritance, and weak inheritance. This chapter gives the guidelines for class design with several examples. The wrapper classes for primitive data types are introduced to encapsulate primitive data type values as objects. Finally, two examples of designing generic classes for matrix operations and linked lists are introduced.
Part III: GUI Programming
The third part of the book introduces Java GUI programming. Major topics include event-driven programming, creating graphical user interfaces, and writing applets. You will learn the architecture of Java GUI programming API and use the user interface components to develop GUI applications and applets.
Chapter 10, "Getting Started with GUI Programming," introduces the concepts of Java GUI programming using Swing components. Topics include the Swing class hierarchy, frames, panels, and simple layout managers (Flomayout, GridLayout, and BorderLayout). The chapter introduces drawing geometric figures in the graphics context. The concept and techniques for Java event-driven programming are presented.
Chapter 11, "Creating User Interfaces," introduces the user interface components: buttons, labels, text fields, text areas, combo boxes, lists, check boxes, radio buttons, menus, scrollbars, and scroll panes. Today's client/server and Web-based applications use a graphical user interface. Java has a rich set of classes to help you build GUIs.
Chapter 12, "Applets and Advanced GUI," takes an in-depth look at applets, discussing applet behavior and the relationship between applets and other Swing classes. Applets are a special kind of Java class that can be executed from the Web browser. Students will learn how to convert applications to applets, and vice versa, and how to run programs both as applications and as applets. The chapter also introduces two advanced layout mangers (CardLayout and GridBagLayout) and the use of no layout. Advanced examples of handling mouse and keyboard events are provided.
Part IV: Developing Comprehensive Projects
The book's final part is devoted to several advanced features of Java programming. You will learn how to use these features to develop comprehensive programs; for example, using exception handling to make your program robust, using multithreading to make your program more responsive and interactive, incorporating sound and images to make your program user-friendly, using input and output to manage and process a large quantity of data, creating client/server applications with Java networking support, and using the classes in the Java Collections Framework to build data structures in Java.
Chapter 13, "Exception Handling," teaches students how to define exceptions, throw exceptions, and handle exceptions so that their programs can either continue to run or terminate gracefully in the event of runtime errors. The chapter discusses predefined exception classes, and gives examples of creating user-defined exception classes.
Chapter 14, "Internationalization," introduces the development of Java programs for international audiences. You will learn how to format dates, numbers, currencies, and percentages for different regions, countries, and languages. You will also learn how to use resource bundles to define which images and strings are used by a component depending on the user's locale and preferences.
Chapter 15, "Multithreading," introduces threads, which enable the running of multiple tasks simultaneously in one program. Students will learn how to use the Thread class and the Runnable interface to launch separate threads. The chapter also discusses thread states, thread priority, thread groups, and the synchronization of conflicting threads.
Chapter 16, "Multimedia," teaches how to incorporate sound and images to bring live animation to Java applets and applications. Various techniques for smoothing animation are introduced.
Chapter 17, "Input and Output," introduces input and output streams. Students will learn the class structures of I/O streams, byte and character streams, file I/O streams, data I/O streams, print streams, object streams, random file access, delimited I/O, and interactive I/O.
Chapter 18, "Networking," introduces network programming. Students will learn the concept of network communication, stream sockets, client/server programming, and reading data files from the Web server.
Chapter 19, "Java Data Structures," introduces the Java Collections Framework. Students will learn to use classes and interfaces such as Collection, Set, Hashset, Treeset, Iterator, List, ArrayList, LinkedList, Vector, Stack, Map, HashMap, TreeMap, Collections, and Arrays to build projects.
This part of the book covers a mixed bag of topics. Appendix A lists Java keywords. Appendix B gives tables of ASCII characters and their associated codes in decimal and in hex. Appendix C shows the operator precedence. Appendix D summarizes Java modifiers and their usage. Appendix E introduces number systems and conversions among binary, decimal, and hex numbers. Bitwise operations are also introduced in this appendix. Appendix F introduces HTML basics. Appendix G lists UML Graphical Notations for describing classes and their relationships. Appendix H introduces package-naming conventions, creating packages, and using packages. Appendix I discusses special floating-point values. Finally, Appendix J provides a glossary of key terms used in the text.