Table Of Content© 2013 by Barry Wittman, Aditya Mathur, Tim Korb.
Distributed by Purdue University Press, http://www.thepress.purdue.edu.
Wittman To the set of all people that I do not dedicate this book to
Mathur To my mother
Korb To my family
Contents
Preface
Acknowledgements
1 Computer Basics
1.1 Problem: Buying a computer
1.2 Concepts: Hardware and software
1.2.1 Hardware
1.2.2 Software
1.2.3 Examples
1.3 Syntax: Data representation
1.3.1 Compilers and interpreters
1.3.2 Numbers
1.3.3 Conversion across number systems
1.3.4 Integer representation in a computer
1.3.5 Binary arithmetic
1.3.6 Negative integers in a computer
1.3.7 Over�ow and under�ow
1.3.8 Bitwise operators
1.3.9 Rational numbers
1.4 Solution: Buying a computer
1.5 Concurrency: Multicore processors
1.5.1 The Good
1.5.2 The Bad
1.5.3 The Ugly
1.6 Summary
Exercises
Conceptual Problems
2 Problem Solving and Programming
2.1 Problem: How to solve problems
2.1.1 What is a program?
2.1.2 What is a programming language?
2.1.3 An example problem
2.2 Concepts: Developing software
2.2.1 Software development lifecycle
2.2.2 Acquiring a Java compiler
2.3 Syntax: Java basics
2.3.1 Java program structure
2.3.2 Command line input and output
2.3.3 GUI input and output
2.3.4 A few operations
2.3.5 Java formatting
2.4 Solution: How to solve problems
2.4.1 Bouncing ball solution (command line version)
2.4.2 Bouncing ball solution (GUI version)
2.4.3 Testing and maintenance
2.5 Concurrency: Solving problems in parallel
2.5.1 Parallelism and concurrency
2.5.2 Sequential versus concurrent programming
2.5.3 Kinds of concurrency
2.6 Summary
Exercises
Conceptual Problems
Programming Practice
3 Primitive Types and Strings
3.1 Problem: College cost calculator
3.2 Concepts: Types
3.2.1 Types as sets and operations
3.2.2 Primitive and reference types in Java
3.2.3 Type safety
3.3 Syntax: Types in Java
3.3.1 Variables and literals
3.3.2 Primitive types
3.3.3 Reference types
3.3.4 Assignment and comparison
3.3.5 Constants
3.4 Syntax: Useful libraries
3.4.1 The Math library
3.4.2 Random numbers
3.4.3 Wrapper classes
3.5 Solution: College cost calculator
3.6 Concurrency: Expressions
3.6.1 Splitting expressions
3.6.2 Care in splitting expressions
3.7 Summary
Exercises
Conceptual Problems
Programming Practice
4 Selection
4.1 Problem: Monty Hall simulation
4.2 Concepts: Choosing between options
4.2.1 Simple choices
4.2.2 Boolean operations
4.2.3 Nested choices
4.3 Syntax: Selection in Java
4.3.1 if statements
4.3.2 The boolean type and its operations
4.3.3 switch statements
4.4 Solution: Monty Hall
4.5 Concurrency: Selection
Exercises
Conceptual Problems
Programming Practice
5 Repetition
5.1 Problem: DNA searching
5.2 Concepts: Repetition
5.3 Syntax: Loops in Java
5.3.1 while loops
5.3.2 for loops
5.3.3 do-while loops
5.3.4 Nested loops
5.3.5 Common pitfalls
5.4 Solution: DNA searching
5.5 Concurrency: Loops
Exercises
Conceptual Problems
Programming Practice
Experiments
6 Arrays
6.1 Introduction
6.2 Problem: Conway’s Game of Life
6.2.1 Terminal limitations
6.3 Concepts: Lists ofdata
6.3.1 Data structure attributes
6.3.2 Characteristics of an array
6.4 Syntax: Arrays in Java
6.4.1 Array declaration and instantiation
6.4.2 Indexing into arrays
6.4.3 Using loops with arrays
6.4.4 Redirecting input
6.5 Examples: Array usage
6.6 Concepts: Multidimensional lists
6.7 Syntax: Advanced arrays in Java
6.7.1 Multidimensional arrays
6.7.2 Reference types
6.7.3 Ragged arrays
6.7.4 Common pitfalls
6.8 Examples: Two-dimensional arrays
6.9 Advanced Syntax: Special array tools in Java
6.9.1 The for-each loop
6.9.2 The Arrays class
6.10 Solution: Conway’s Game of Life
6.11 Concurrency: Arrays
Exercises
Conceptual Problems
Programming Practice
Experiments
7 Simple Graphical User Interfaces
7.1 Problem: Codon extractor
7.2 Concepts: User interaction
7.3 Syntax: Dialogs and the JOptionPane class
7.3.1 Generating an information dialog
7.3.2 Generating a Yes-No con�rm dialog
7.3.3 Generating a dialog with a list ofoptions
7.3.4 Generating a dialog with a custom icon
7.3.5 Generating an input dialog
7.4 Solution: Codon extractor
7.5 Concurrency: Simple GUIs
7.6 Summary
Exercises
Conceptual Problems
Programming Practice
8 Methods
8.1 Problem: Three card poker
8.2 Concepts: Dividing work into segments
8.2.1 Reasons for static methods
8.2.2 Parallel to mathematical functions
8.2.3 Control �ow
8.3 Syntax: Methods
8.3.1 De�ning methods
8.3.2 Calling methods
8.3.3 Class variables
8.4 Examples: De�ning methods
8.5 Solution: Three card poker
8.6 Concurrency: Methods
Exercises
Conceptual Problems
Programming Practice
9 Classes
9.1 Problem: Nested expressions
9.2 Concepts: Object-oriented programming
9.2.1 Objects
9.2.2 Encapsulation
9.2.3 Instance methods
9.3 Syntax: Classes in Java
9.3.1 Members
9.3.2 Methods
9.3.3 Access modi�ers
9.4 Examples: Classes
9.5 Advanced Syntax: Nested classes
9.5.1 Static nested classes
9.5.2 Inner classes
9.6 Solution: Nested expressions
9.7 Concurrency: Objects
Exercises
Conceptual Problems
Programming Practice
Experiments
10 Interfaces
10.1 Problem: Sort it out
10.2 Concepts: Making a promise
10.3 Syntax: Interfaces
10.3.1 Interfaces and static
10.4 Advanced Syntax: Local and anonymous classes
10.4.1 Local classes
10.4.2 Anonymous classes
10.5 Solution: Sort it out
10.6 Concurrency: Interfaces
Exercises
Conceptual Problems
Programming Practice
Experiments
11 Inheritance
11.1 Problem: Boolean circuits
11.2 Concepts: Re�ning classes
11.2.1 Basic inheritance
11.2.2 Adding functionality
11.2.3 Code reuse
11.3 Syntax: Inheritance in Java
11.3.1 The extends keyword
11.3.2 Access restriction and visibility
11.3.3 Constructors
11.3.4 Overriding methods and hiding members
11.3.5 The Object class
11.4 Examples: Problem solving with inheritance
11.5 Solution: Boolean circuits
11.6 Concurrency: Inheritance
Exercises
Conceptual Problems
Programming Practice
Experiments
12 Exceptions
12.1 Problem: Bank burglary
12.2 Concepts: Error handling
12.2.1 Error codes
12.2.2 Exceptions
12.3 Syntax: Exceptions in Java
12.3.1 Throwing exceptions
12.3.2 Handling exceptions
12.3.3 Catch or specify
12.3.4 The �nally keyword
12.3.5 Customized exceptions
12.4 Solution: Bank burglary
12.5 Concurrency: Exceptions
12.5.1 InterruptedException
Exercises
Conceptual Problems
Programming Practice
Experiments
13 Concurrent Programming
13.1 Introduction
13.2 Problem: Deadly virus
13.3 Concepts: Splitting up work
13.3.1 Task decomposition
13.3.2 Domain decomposition
13.3.3 Tasks and threads
13.3.4 Memory architectures and concurrency
13.4 Syntax: Threads in Java
13.4.1 The Thread class
13.4.2 Creating a thread object
13.4.3 Starting a thread
13.4.4 Waiting for a thread
13.4.5 The Runnable interface
13.5 Examples: Concurrency and speedup
13.6 Concepts: Thread scheduling
13.6.1 Nondeterminism
13.6.2 Polling
13.7 Syntax: Thread states
13.8 Solution: Deadly virus
13.9 Summary
Exercises
Conceptual Problems
Programming Practice
Experiments
14 Synchronization
14.1 Introduction
14.2 Problem: Dining philosophers
14.3 Concepts: Thread interaction
14.4 Syntax: Thread synchronization
14.4.1 The synchronized keyword
14.4.2 The wait () andnotify () methods
14.5 Pitfalls: Synchronization challenges
14.5.1 Deadlock
14.5.2 Starvation and livelock
14.5.3 Sequential execution
14.5.4 Priority inversion
14.6 Solution: Dining philosophers
Exercises
Conceptual Problems
Programming Practice
Experiments
15 Constructing Graphical User Interfaces
Description:Multicore microprocessors are now at the heart of nearly all desktop and laptop computers. While these chips offer exciting opportunities for the creation of newer and faster applications, they also challenge students and educators. How can the new generation of computer scientists growing up with m