PROBLEM SOLVING WITH DATA STRUCTURES USING JAVA A Multimedia Approach Mark Guzdial and Barbara Ericson College of Computing Georgia Institute of Technology PEARSON Boston Columbus Indianapolis New York San Francisco Upper Saddle River Amsterdam Cape Town Dubai London Madrid Milan Munich Paris Montreal Toronto Delhi Mexico City Sao Paulo Sydney Hong Kong Seoul Singapore Taipei Tokyo
Contents Preface 13 About the Authors 19 1 INTRODUCTION TO JAVA: OBJECT-ORIENTED PROGRAMMING FOR MODELING A WORLD 23 1 Objects for Modeling a World 25 1.1 Making Representations of a World 27 1.2 Why Java? 33 Getting Java Set Up 33 2 Introduction to Java 37 2.1 What's Java About? 37 2.2 Basic (Syntax) Rules of Java 39 Declarations and "types 39 Strings 41 ObjectVariables and Primitive Variables 42 Assignment 44 Semicolons 44 Conditionals 44 Arrays 46 Iteration 47 Strings versus Arrays and Substrings 48 2.3 Using Java to Model the World 48 Discourse Rules for Java 53 Defining tostring 53 Defining Student as Subclass of Person 54 Creating a main() Method 57 Exploring Inheritance 58 2.4 Manipulating Pictures in Java 61 2.5 Exploring 2.6 Exploring Sound in Java 67 Music in Java 68
Methods in Java: Manipulating Pictures 73 3.1 Reviewing Java Basics 73 Assignment 73 Arrays 75 Conditionals 75 Iteration: For each, While, and For 76 3.2 Changing the Picture Class 77 Pictures Are About Arrays and Pixels 79 A Method for Decreasing Red 79 Method with an Input 82 3.3 Methods That Return Something: Compositing Images 84 Composing by Chromakey 90 3.4 Creating Classes That Do Something 93 Objects as Agents: Manipulating Turtles 97 4.1 Turtles: An Early Computational Object 97 4.2 Drawing with Turtles 98 When Cascades Don't Work 100 Making Lots of Turtles 101 Composing Pictures with Turtles 103 4.3 Creating Animations with Turtles and Frames 105 The Data Structure within FrameSequencer 108 4.4 Making a Slow Moving Turtle with sleep and Exceptions 110 Arrays: A Static Data Structure for Sounds 115 5.1 Manipulating Sampled Sounds 115 5.2 Inserting and Deleting in an Array 120 5.3 How Slow Does It Get? 124 INTRODUCING LINKED LISTS 127 Structuring Music Using Linked Lists 129 6.1 JMusic and Imports 129 6.2 Making a Simple Song Object 134 6.3 Making a Song Something to Explore as a Linked List 135 Walking through SongNode 143 Improving insertafter 147
The Contents 9 Helping Others Use Our Classes 148 Computing Phrases 149 Repeating and Weaving to Make Music 151 Linked Lists versus Arrays 154 Creating a Music Tree 156 7 Structuring Images Using Linked Lists 163 7.1 Simple Arrays of Pictures 164 7.2 Listing the Pictures, Left-to-Right 165 Generalizing Moving the Element 168 7.3 Listing the Pictures, Layering 171 Reordering Elements in a List 176 7.4 Reversing a List 178 7.5 Animation 179 7.6 Lists with Two Kinds of Elements 181 Structuring Our Multi-Element Lists 191 3 TREES: HIERARCHICAL STRUCTURES FORMEDIA 199 8 Trees of Images 201 8.1 Representing Scenes with Trees 201 8.2 OurFirstSceneGraph:AttackoftheKillerWolvies 204 8.3 Classes in the Scene Graph 206 8.4 Building a Scene Graph 208 8.5 Implementing the Scene Graph 215 Implementing the Abstract Superclass for the Scene Graph: DrawableNode 217 Implementing the Leaf Nodes: PictNode and BlueScreenNode 219 Implementing the Branches: Branch, MoveBranch, VBranch, andhbranch 221 The Line between Structure and Behavior 227 9 Lists and Trees for Structuring Sounds 231 9.1 Composing with Sampled Sounds and Linked Lists: Recursive Traversals 231 Tracing a Recursive Traversal 237 Testing and Replacing 239
10 Contents 9.2 Using Trees to Structure Sampled Sounds 245 Implementing a Sound Tree 249 Tracing a Recursive Tree Traversal 255 Where We're Going Next 261 10 Generalizing Lists and Trees 264 10.1 Refactoring a General Linked List Node Class 264 Making Wol fattackmovi e Work Again 267 Making Sound Trees Work Again 270 10.2 Making a New Kind of List 273 10.3 The Uses and Characteristics of Arrays, Lists, and Trees 275 Examples of Tree Uses 277 10.4 Binary Search Trees: Trees That Are Fast to Search 280 Traversals of Trees 287 Trees Can Do Anything 289 11 Abstract Data Types: Separating the Meaning from the Implementation 293 11.1 Introducing Stacks 293 Defining an Abstract Data Type 294 A Stack Interface 296 Multiple Implementations of a Stack 297 Uses of a Stack 303 11.2 Introducing Queues 305 Implementing a Queue 307 Creating an Abstract Queue Class 312 11.3 Using an ArrayList 315 11.4 Using a Map ADT 317 12 Circular Linked Lists and Graphs: Lists and Trees That Loop 322 12.1 Making Sprite Animation with Circular Linked Lists 322 12.2 Generalizing a Circular Linked List 327 12.3 Graphs: Trees with Loops 329 13 User Interface Structures 337 13.1 A Toolkit for Building User Interfaces 337 Building the Simplest Possible Swing User Interface 338
Contents 11 13.2 Building and Rendering User Interfaces 340 Building a Simple User Interface 340 Java Swing Layout Managers: GUI Tree Renderers 344 13.3 Creating an Interactive User Interface 347 Making Our GUItree Interactive 349 Creating a Picture Tool 351 13.4 Running from the Command Line 356 4 SIMULATIONS: PROBLEM SOLVING WITH DATA STRUCTURES 361 14 Using an Existing Simulation Package 363 14.1 Introducing Simulations 363 14.2 Overview of Greenfoot 365 14.3 Greenfoot Basics 370 14.4 Creating New Classes 372 14.5 Breakout 377 15 Introducing UML and Continuous Simulations 387 15.1 Our First Model and Simulation: Wolves and Deer 387 Modeling the Wolves and Deer 389 15.2 Modeling in Objects 390 15.3 Implementing the Simulation Class 394 15.4 Implementing 15.5 Implementing a Wolf 397 Deer 402 15.6 Implementing AgentNode 403 15.7 Extending the Simulation 404 Making Hungry Wolves 404 Writing Results to a File 407 Getting Results from a Simulation 411 16 Abstracting Simulations: Creating a Simulation Package 413 16.1 Creating a Generalized Simulation Package 413 Real Programmers Rarely Build Data Structures 414
Real Programmers Make Models and Choices 415 The Structure of the Simulation Package 416 Using a Li nkedli st from the Java Collection Classes 419 16.2 Re-Making the Wolves and Deer with Our Simulation Package 420 16.3 Making a Disease Propagation Simulation 428 Exploring Scenarios in Disease Propagation 432 16.4 Walking through the Simulation Package 434 16.5 Finally! Making Wildebeests and Villagers 439 Going Beyond the Wildebeests 447 Discrete Event Simulation 456 17.1 Describing a Marketplace 456 17.2 Differences between Continuous and Discrete Event Simulations Introducing Resources 458 17.3 Different Kinds of Random 458 Random Distributions in the World 459 Generating Different Random Distributions 460 Generating Useful Random Distributions 466 17.4 Ordering Events by Time 469 Sorting Objects 473 Inserting into a Sorted List 474 Using a Min-Heap 475 17.5 Implementing a Discrete Event Simulation 477 Building a Discrete Event Simulation 480 17.6 The Final Word: The Thin Line between Structure and Behavior 493 Bibliography 497 Index 499