Building Java Programs

Similar documents
Introduction to Programming Using Java (98-388)

This page intentionally left blank


Contents Chapter 1 Introduction to Programming and the Java Language

MyProgram m i ng Lab. get with the programming. Through the power of practice and immediate personalized

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Data Structures and Abstractions with Java

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

Absolute C++ Walter Savitch


Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Three new chapters. Improved case studies. Reorganization of using objects material. iii

JAVA CONCEPTS Early Objects

Course materials Reges, Stuart, and Stepp, Martin. Building Java Programs: A Back to Basics Approach. 2d ed. (Boston: Addison-Wesley, 2011).

Big Java Late Objects

Language Features. 1. The primitive types int, double, and boolean are part of the AP

Problem Solving with C++

Java Programming with Eclipse

This page intentionally left blank

5/23/2015. Core Java Syllabus. VikRam ShaRma

Java 1.8 Programming

TeenCoder : Java Programming (ISBN )

CompuScholar, Inc. 9th - 12th grades

Java Fundamentals p. 1 The Origins of Java p. 2 How Java Relates to C and C++ p. 3 How Java Relates to C# p. 4 Java's Contribution to the Internet p.

[CHAPTER] 1 INTRODUCTION 1

Syllabus & Curriculum for Certificate Course in Java. CALL: , for Queries

CO Java SE 8: Fundamentals

IMACS: AP Computer Science A

Application Development in JAVA. Data Types, Variable, Comments & Operators. Part I: Core Java (J2SE) Getting Started

College Board. AP CS A Labs Magpie, Elevens, and Picture Lab. New York: College Entrance Examination Board, 2013.

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

Objects and Iterators

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

Java Programming Training for Experienced Programmers (5 Days)

Java SE 8 Programming

Index COPYRIGHTED MATERIAL

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Core Java - SCJP. Q2Technologies, Rajajinagar. Course content

Java for Non Majors. Final Study Guide. April 26, You will have an opportunity to earn 20 extra credit points.

Core Java SYLLABUS COVERAGE SYLLABUS IN DETAILS

Murach s Beginning Java with Eclipse

Chapter 6 Introduction to Defining Classes

Java SE 8 Programming

ощ 'ршорвшэш! цвн-эориэу ощ 'sajbpossv # PIPG DUJ 'ssjmoossv ^ PIPG pipa w н OX ЛЮН VAV

Foundations of object orientation

C ONTENTS PART I FUNDAMENTALS OF PROGRAMMING 1. and Java 3. Chapter 1 Introduction to Computers, Programs,

Java SE 8 Programming

Curriculum Map Grade(s): Subject: AP Computer Science

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Learning C# 3.0. Jesse Liberty and Brian MacDonald O'REILLY. Beijing Cambridge Farnham Köln Sebastopol Taipei Tokyo

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Operators and Expressions

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

Core Java Syllabus. Overview

DATA STRUCTURES AND PROBLEM SOLVING USING JAVA

Object-Oriented Programming and Data Structures

The Sun s Java Certification and its Possible Role in the Joint Teaching Material

WA1278 Introduction to Java Using Eclipse

Mathematics/Science Department Kirkwood Community College. Course Syllabus. Computer Science CSC142 1/10

Index. Animation. factors, 186. Abstract Window Toolkit (AWT), 111. frame rate, 185. Access modifiers. multiple objects. package-private, 27

Core Java Contents. Duration: 25 Hours (1 Month)

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

JAVA. Duration: 2 Months

AP Computer Science A. Course Syllabus. Jackson County High School Instructor: LaDonna Woods School Year

A Quick Tour p. 1 Getting Started p. 1 Variables p. 3 Comments in Code p. 6 Named Constants p. 6 Unicode Characters p. 8 Flow of Control p.

Programming II (CS300)

Programming in Scala Second Edition

MARS AREA SCHOOL DISTRICT Curriculum TECHNOLOGY EDUCATION

COURSE OF STUDY UNIT PLANNING GUIDE COMPUTER SCIENCE 1 FOR: 5 CREDITS GRADE LEVEL: 9-12 FULL-YEAR COURSE PREPARED BY: SUSIE EISEN

Preface. The Purpose of this Book and Its Audience. Coverage and Approach

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

Chapter 4 Defining Classes I

Programming II (CS300)

Java Just in Time: Collected concepts after chapter 22

Glossary. For Introduction to Programming Using Python By Y. Daniel Liang

Course Description. Learn To: : Intro to JAVA SE7 and Programming using JAVA SE7. Course Outline ::

Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION. useful as well. It has been written to provide introductory computer science

COP 3330 Final Exam Review

C# Programming: From Problem Analysis to Program Design. Fourth Edition

Samples of Evidence to Satisfy the AP Computer Science AB Curricular Requirements

Java 2. Course Outcome Summary. Western Technical College. Course Information. Course History. Course Competencies

Data Structures in C++ Using the Standard Template Library

1 Shyam sir JAVA Notes

15CS45 : OBJECT ORIENTED CONCEPTS

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

Supporting Materials

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

APCS Semester #1 Final Exam Practice Problems

CHAPTER 1 Introduction to Computers and Programming CHAPTER 2 Introduction to C++ ( Hexadecimal 0xF4 and Octal literals 031) cout Object

+ Abstract Data Types

Course Outline. Introduction to java

CS 445: Data Structures Final Examination: Study Guide

Course Text. Course Description. Course Objectives. StraighterLine Introduction to Programming in C++

Chapters and Appendix F are PDF documents posted online at the book s Companion Website (located at

Objects First with Java

Table of Contents. Chapter 1 Getting Started with Java SE 7 1. Chapter 2 Exploring Class Members in Java 15. iii. Introduction of Java SE 7...

1 P a g e A r y a n C o l l e g e \ B S c _ I T \ C \

Write the code to create an enhanced for loop that will go through every member of an ArrayList <String> myarray and print it out to the console.

Peers Techno log ies Pv t. L td. Core Java & Core Java &Adv Adv Java Java

Transcription:

Building Java Programs A Back to Basics Approach Stuart Reges I Marty Stepp University ofwashington

Preface 3 Chapter 1 Introduction to Java Programming 25 1.1 Basic Computing Concepts 26 Why Programming? 26 Hardware and Software 27 The Digital Realm 28 The Process of Programming 30 Why Java? 31 The Java Programming Environment 32 1.2 And Now Java 34 String Literals (Strings) 38 System.out.println 39 Escape Sequences 39 print versus println 41 Identifiers and Keywords 42 A Complex Example: DrawFiguresl 44 Comments and Readability 45 1.3 Program Errors 48 Syntax Errors 48 Logic Errors (Bugs) 52 1.4 Procedural Decomposition 52 Static Methods 55 Flow of Control 58 Methods that Call Other Methods 60 An Example Runtime Error 63 1.5 Case Study: DrawFigures 64 Structured Version 65 Final Version without Redundancy 67 Analysis of Flow of Execution 68

Chapter 2 Primitive Data and Definite Loops 2.1 Basic Data Concepts Primitive Types Expressions Literals Arithmetic Operators Precedence Mixing Types and Casting 2.2 Variables Assignment/Declaration Variations String Concatenation Increment/Decrement Operators Variables and Mixing Types 2.3 The for Loop Tracing for Loops for Loop Patterns Nested for Loops 2.4 Managing Complexity Scope Pseudocode Class Constants 2.5 Case Study: Hourglass Figure Problem Decomposition and Pseudocode Initial Structured Version Adding a Class Constant Further Variations Chapter 3 Introduction to Parameters and Objects 3.1 Parameters The Mechanics of Parameters Limitations of Parameters Multiple Parameters Parameters versus Constants Overloading of Methods 3.2 Methods That Return Values The Math Class Defining Methods That Return Values

3.3 Using Objects 179 String Objects 180 Interactive Programs and Scanner Objects 186 Sample Interactive Program 189 3.4 Case Study: Projectile Trajectory 192 Unstructured Solution 196 Structured Solution 198 Supplement 3G Graphics (Optional) 212 3G.I Introduction to Graphics 213 DrawingPanel 213 Drawing Lines and Shapes 214 Colors 219 Drawing with Loops 222 Text and Fonts 226 3G.2 Procedural Decomposition with Graphics 229 A Larger Example: DrawDiaraonds 230 3G.3 Case Study: Pyramids 233 Unstructured Partial Solution 234 Generalizing the Drawing of Pyramids 236 Complete Structured Solution 237 Chapter 4 Conditional Execution 250 4.1 if /else Statements 251 Relational Operators 253 Nested if /else Statements 255 Object Equality 262 Factoring if /else Statements 263 Multiple Conditions 265 4.2 Cumulative Algorithms 266 Cumulative Sum 266 Min/Max Loops 268 Cumulative Sum with if 272 Roundoff Errors 274 4.3 Text Processing 277 The char Type 277 char versus int 278

Cumulative Text Algorithms 279 System.out.printf 281 4.4 Methods with Conditional Execution 286 Preconditions and Postconditions 286 Throwing Exceptions 286 Revisiting Return Values 290 Reasoning about Paths 295 4.5 Case Study: Body Mass Index 297 One-Person Unstructured Solution 298 Two-Person Unstructured Solution 301 Two-Person Structured Solution 303 Procedural Design Heuristics 307 Chapter 5 Program Logic and Indefinite Loops 325 5.1 The while Loop 326 A Loop to Find the Smallest Divisor 327 Random Numbers 330 Simulations 334 do/while Loop 335 5.2 Fencepost Algorithms 337 Sentinel Loops 339 Fencepost with if 340 5.3 The boolean Type 343 Logical Operators 345 Short-Circuited Evaluation 348 boolean Variables and Flags 352 Boolean Zen 354 Negating Boolean Expressions 357 5.4 User Errors 358 Scanner Lookahead 359 Handling User Errors 361 5.5 Assertions and Program Logic 363 Reasoning about Assertions 365 A Detailed Assertions Example 366 5.6 Case Study: NumberGuess 371 Initial Version without Hinting 371 Randomized Version with Hinting 373 Final Robust Version 377

Chapter 6 File Processing 397 6.1 File-Reading Basics 398 Data, Data Everywhere 398 Files and File Objects 398 Reading a File with a Scanner 401 6.2 Details of Token-Based Processing 406 Structure of Files and Consuming Input 408 Scanner Parameters 413 Paths and Directories 414 A More Complex Input File 417 6.3 Line-Based Processing 419 String Scanners and Line/Token Combinations 420 6.4 Advanced File Processing 425 Output Files with PrintStream 425 Guaranteeing That Files Can Be Read 430 6.5 Case Study: Zip Code Lookup 433 Chapter 7 Arrays 449 7.1 Array Basics 450 Constructing and Traversing an Array 450 Accessing an Array 454 A Complete Array Program 457 Random Access 461 Arrays and Methods 464 The For-Each Loop 467 Initializing Arrays 469 The Arrays Class 470 7.2 Array-Traversal Algorithms 471 Printing an Array 472 Searching and Replacing 474 Testing for Equality 477 Reversing an Array 478 String Traversal Algorithms 483 7.3 Reference Semantics 484 Multiple Objects 486

7.4 Advanced Array Techniques 488 Shifting Values in an Array 488 Arrays of Objects 493 Command-Line Arguments 494 Nested Loop Algorithms 495 7.5 Multidimensional Arrays 497 Rectangular Two-Dimensional Arrays 497 Jagged Arrays 499 7.6 Case Study: Benford's Law 503 Tallying Values 505 Completing the Program 509 Chapter 8 Classes 524 8.1 Object-Oriented Programming 525 Classes and Objects 526 Point Objects 528 8.2 Object State and Behavior 529 Object State: Fields 530 Object Behavior: Methods 532 The Implicit Parameter 535 Mutators and Accessors 537 The tostring Method 539 8.3 Object The Keyword Initialization: Constructors 541 this 546 Multiple Constructors 548 8.4 Encapsulation 549 Private Fields 550 Class Invariants 556 Changing Internal Implementations 560 8.5 Case Study: Designing a Stock Class 562 Object-Oriented Design Heuristics 563 Stock Fields and Method Headers 565 Stock Method and Constructor Implementation 567 Chapter 9 Inheritance and Interfaces 579 9.1 Inheritance Basics 580 Nonprogramming Hierarchies 581

Extending a Class 583 Overriding Methods 587 9.2 Interacting with the Superclass 589 Calling Overridden Methods 589 Accessing Inherited Fields 590 Calling a Superclass's Constructor 592 DividendStock Behavior 594 The Object Class 596 The equals Method 597 The instanceof Keyword 600 9.3 Polymorphism 602 Polymorphism Mechanics 605 Interpreting Inheritance Code 607 Interpreting Complex Calls 609 9.4 Inheritance and Design 612 A Misuse of Inheritance 612 Is-a Versus Has-a Relationships 615 Graphics2D 616 9.5 Interfaces 618 An Interface for Shapes 619 Implementing an Interface 621 Benefits of Interfaces 624 9.6 Case Study: Financial Class Hierarchy 626 Designing the Classes 627 Redundant Implementation 631 Abstract Classes 634 Chapter 10 ArrayLists 652 10.1 ArrayLists 653 Basic ArrayList Operations 654 ArrayList Searching Methods 657 A Complete ArrayList Program 660 Adding to and Removing from an ArrayList 662 Using the For-Each Loop with ArrayLists 666 Wrapper Classes 667 10.2 The Comparable Interface 670 Natural Ordering and compareto 672 Implementing the Comparable Interface 676

10.3 Case Study: Vocabulary Comparison 682 Some Efficiency Considerations 682 Version 1: Compute Vocabulary 685 Version 2: Compute Overlap 688 Version 3: Complete Program 693 Chapter 11 Java Collections Framework 703 M.I Lists 704 Collections 704 LinkedList versus ArrayList 705 Iterators 708 Abstract Data Types (ADTs) 711 LinkedList Case Study: Sieve 714 11.2 Sets 717 Set Concepts 718 TreeSet versus HashSet 720 Set Operations 721 Set Case Study: Lottery 723 11.3 Maps 725 Basic Map Operations 726 Map Views (keyset and values) 728 TreeMap versus HashMap 729 Map Case Study: wordcount 730 Collection Overview 733 Chapter 12 Recursion 742 12.1 Thinking Recursively 743 A Nonprogramming Example 743 An Iterative Solution Converted to Recursion 746 Structure of Recursive Solutions 748 12.2 A Better Example of Recursion 750 Mechanics of Recursion 752 12.3 Recursive Functions and Data 760 Integer Exponentiation 760 Greatest Common Divisor 763 Directory Crawler 769 Helper Methods 773 12.4 Recursive Graphics (Optional) 776

12.5 Case Study: Prefix Evaluator 780 Infix, Prefix, and Postfix Notation 781 Evaluating Prefix Expressions 781 Complete Program 785 Chapter 13 Searching and Sorting 798 13.1 Searching and Sorting in the Java Class Libraries 799 Binary Search 800 Sorting 803 Shuffling 804 Custom Ordering with Comparators 805 13.2 Program Complexity 809 Empirical Analysis 812 Complexity Classes 816 13.3 Implementing Searching and Sorting Algorithms 818 Sequential Search 819 Binary Search 820 Recursive Binary Search 823 Searching Objects 826 Selection Sort 827 13.4 Case Study: Implementing Merge Sort 830 Splitting and Merging Arrays 831 Recursive Merge Sort 834 Complete Program 837 Chapter 14 Graphical User Interfaces 846 14.1 GUI Basics 847 Graphical Input and Output with Option Panes 847 Working with Frames 850 Buttons, Text Fields, and Labels 855 Changing a Frame's Layout 857 Handling an Event 858 14.2 Laying Out Components 860 Layout Managers 861 Composite Layouts 864 14.3 Interaction Between Components 869 Example 1: BMI GUI 869

Object-Oriented GUIs 870 Example 2: Credit Card GUI 874 14.4 Additional Components and Events 879 Text Areas, Scrollbars, and Fonts 880 Icons 882 Mouse Events 884 14.5 Two-Dimensional Graphics 889 Drawing onto Panels 889 Animation with Timers 891 14.6 Case Study: Implementing DrawingPanel 896 Initial Version without Events 896 Second Version with Events 898 Chapter 15 Implementing a Collection Class 910 15.1 Simple ArraylntList 911 Adding and Printing 911 Thinking about Encapsulation 917 Dealing with the Middle of the List 918 Another Constructor and a Constant 923 Preconditions and Postconditions 924 15.2 A More Complete ArraylntList 928 Throwing Exceptions 928 Convenience Methods 931 15.3 Advanced Features 934 Resizing When Necessary 934 Adding an Iterator 936 15.4 ArrayList<E> 942 Chapter 16 Linked Lists 953 16.1 Working with Nodes 954 Constructing a List 955 List Basics 957 Manipulating Nodes 960 Traversing a List 963 16.2 A Linked List Class 967 Simple LinkedlntList 967

23 Appending add 969 The Middle of the List 973 16.3 A Complex List Operation 980 Inchworm Approach 985 16.4 An intlist Interface 986 16.5 LinkedList<E> 989 Linked List Variations 990 Linked List Iterators 993 Other Code Details 995 Chapter 17 Binary Trees 1005 17.1 Binary Tree Basics 1006 Node and Tree Classes 1009 17.2 Tree Traversals 1010 Constructing and Viewing a Tree 1016 17.3 Common Tree Operations 1025 Sum of a Tree 1025 Counting Levels 1026 Counting Leaves 1028 17.4 Binary Search Trees 1029 The Binary Search Tree Property 1030 Building a Binary Search Tree 1032 The Pattern x = change(x) 1036 Searching the Tree 1039 Binary Search Tree Complexity 1043 17.5 SearchTree<E> 1044 Appendix A Answers to Self-Check Problems 1059 Appendix B Java Summary 1131 Appendix C Javadoc Comments and the Java API Specification 1146 Appendix D Additional Java Syntax 1152 Index I 161