# Java Programming Notes: Table of Contents

Purpose. These notes were written to fill in missing or weak topics in textbooks that I've taught from. Many pages are useful for reference, but not as an ordered tutorial. Some pages are still rough drafts, and now that I'm back to teaching Java I'm working on them a little every week and considering putting some of them into an orderly tutorial form.

Copyleft is my reaction to the restrictive copyright that most materials are published in. There is also great enthusiasm for open source software, but surprisingly little open source writing. The text of these notes is copyrighted according to the very permissive MIT License, except as otherwise noted (eg, some GPL'ed images in one case). The irony of many textbooks is that they show you code examples, but they're copyrighted! All my Java code examples are placed in the public domain.

A zipped version is available: notes-java-2005-03-26.zip [2 MB].

Enjoy, Fred Swartz

### Background

- Preface
- Programs
- Applications, Applets, etc.
  - Intro
    - GUI - Swing vs. AWT
    - Strengths
    - Weaknesses
    - History of Java
    - Java Virtual Machine
    - Java vs. C
  - Applications - example
  - Compiling a Java program
  - Applets
- Files and Directories
  - CLASSPATH
  - Directories and Files
  - Packages - Importing
  - Packages - Defining
  - Packages - Installing
- Java Tools
  - Tools
  - Programming
  - IDEs
    - NetBeans IDE 4.0
  - Text Editors
    - TextPad Editor
  - Software Development Kit
  - Ant
  - Checkers
- Language Complexity
- Garbage Collection
- Links
  - Other Java Resources

### Principles and practices

- Source Code Style
- Cohesion
- Design By Contract
- Process
  - Development Process
  - Iterative/Incremental Dev
  - Fail Early, Fail Often
  - Start with working pgm
- Complexity is your enemy
- Complexity Measurement
- Debugging Output

### Summaries

- Data summaries
  - Summary - Data
  - Summary - Expressions
  - Summary - String
  - Summary - Character
  - Summary - StringBuffer etc
- GUI summaries
  - Summary - GUI Components
  - Summary - GUI Containers
  - Summary - GUI Layouts
  - Summary - Graphics
- Misc summaries
  - Summary - Basic Elements
  - Summary - Control Flow
  - Methods
  - Classes, Interfaces
  - I/O
  - Math and java.util.Random
  - DOS Commands

### Introductory Examples

- Most elementary
  - First Program - Do Nothing
- Dialog box input-output
  - Dialog Box Output
  - Dialog Box Input-Output
  - Kilometers to Miles
    - Km to Miles - Method
    - Km to Miles - Formatted
    - Km to Miles - Methods
    - Km to Miles - IO Class
- Console input-output
  - Console Output
  - Console Input Java 2
  - Console Input Java 5
  - Console Input 3 Versions
- Application GUI
  - Tiny Window
  - Tiny Window with subclass
  - Km to Mi - User Interface
  - Km to Mi - Complete

### Example Programs

- Components
  - Example - ToUppercase
  - Example - CombineName
  - Example - Body Mass Index
  - Example - Sleepy Face
  - Example - Calculator
  - Example - Simple editor
- Graphics
  - Example - Drawing a Face
  - Example - BullseyeDemo
  - Example - Rolling Dice
  - Example - ColorDisplay
- Animation
  - Example - Bouncing Ball
  - TextClock
    - Example - Text Clock
    - Example - Text Clock 2
    - Example - Extend Clock
  - Example - Buffered Clock
- Mouse
  - Example - MouseTest.java
  - Example - DragDemo.java
  - Example - Card Demo
  - Example - PaintDemo.java
  - Example - Raise.java
- Games
  - Example - Slide Puzzle
  - Example - Five-in-a-row
- Collections
  - Example - WordFrequency

### Language

- Basics
  - Comments
  - Identifier Names
  - import
  - Final

### Control Flow

- If
  - if Statement - Overview
  - 'else' Not Required
  - 'if' Statement - Braces
  - 'if' Statement - Indentation
  - 'if' Statement - if inside if
  - 'if' Statement - 'else if' style
  - Exercises
    - 'if' Quiz 1
    - Prob: Maximum of Three
- switch
  - switch Statement - Overview
- Loops
  - Loops - 'while' and 'for'
  - 'while' Statement
  - 'for' Statement
  - Exercises
    - Ulam Sequence
    - Loop Quiz (Graphics)
    - Loop Exercises 1
    - Loop Exercises 4 (Graphics)
    - Approximating pi
    - Rainfall stats
- Assertions
  - Assertions
- Exceptions
  - Exceptions
  - Exception Usage
  - Exceptions - More
- Methods
  - Methods - Introduction
  - Methods - Example
  - Methods - Declaring
  - Methods - Calling
  - Methods - OOP
  - Methods - Vocabulary
  - Static methods
  - Example - CirclePanel.java
  - Example - CircleMain.java
  - Example -- drawHouse
  - Ex: Variations on max()

### Classes

- Overview
  - Class and Interface Concepts
- Constructors
  - Constructors
  - Constructors - super
  - Constructors - super example
  - Constructor Chaining
- Interfaces
  - Interfaces
  - Interface Exercises 1
- Reflection / Introspection
  - Class class

### IO

- File IO
  - java.io.File
  - File I/O - Text Files
  - BufferedReader/Writer
  - File Filters
  - javax.swing.filechooser.FileFilter
  - java.io.FileFilter
  - Examples
    - Example - FileTest
    - Example - ReadWords
    - Example - Read into textarea
    - Example - Recursive List
- Console
  - Console I/O
- Internet
  - Read page from Web server
- Formatted Output
  - Formatted Output
- Properties and Preferences
  - Preferences
  - Properties
    - System Properties
- JDBC
  - Database Engines

### Data

- Basic Types
  - Numbers
  - Integers
  - Floating-point
  - Boolean
  - Autoboxing
- Variables
  - Variables
  - Local/Instance/Class
  - Shadow variables are evil
  - Shadowing this
- Expressions
  - Expressions
  - Comparison Operators
  - ==, .equals(), and compareTo()
  - Numeric Expression Exercises
  - Boolean Expression Exercises
- Numbers
  - java.math.BigInteger
  - Example - Factorial
- Strings
  - String Overview
  - String Comparison
  - StringBuffer and StringBuilder
  - Summary pages
    - Summary - String
    - Summary - Character
    - Summary - StringBuffer etc
  - Conversion
    - Converting Numbers to String
    - Converting Anything to String
    - Converting Strings to Numbers
  - Regular_expressions
    - Regular Expressions
    - String Regex Methods
    - Basic regex summary
    - Pattern and Matcher
    - Regex Language
    - Regex Examples
    - Full regex summary
  - StringTokenizer
    - java.util.StringTokenizer
    - Tokenizing Delimiters
    - TokenizeExpr.java
  - Exercises
    - String Exercise 1
    - String Exercise 2
  - Programming Exercises
    - Exercise - Capitalize
    - Exercise - Delete Blanks
    - Exercise - Capitalize Words
    - Exercise - Count Words
    - Exercise - Pad Left
    - Exercise - Palindrome
    - Exercise - Trim Right
  - String Examples
    - Example - Array to String
    - Example - Change Extension
    - Example - Count occurences
    - Example - Count vowels
    - Example - Palindrome test
    - Example - Replace word
    - Example - SortWords3.java
    - Example - Words to array
- Arrays
  - Arrays -- Introduction
  - Arrays -- Intermediate
  - Array Quiz
  - Array Quiz 2
  - Arrays -- 2-dimensional
  - Arrays -- Examples
  - Selection Sort
  - Bubble Sorts
  - Reverse an array
  - Array - Maximum
  - Mean - average
  - Arrays -- Multi-dimensional
  - Sorting Arrays
- Collections - Non Generic
  - Java Data Structures
  - Collections Overview
  - Collection Interface
  - Lists
    - ArrayList
    - Vector
  - Sets
    - Sets
  - Maps
    - Maps
    - Map iteration
    - Example - WordFrequency
  - Iterators
    - Iterators
  - Other
    - Class or Interface
    - Generics
    - Alternative Data Structures
  - Exercises
    - Exercise - Unique Components
    - Exercise - State Capitals

### Algorithms

- Big-Oh Notation
  - Big-Oh Notation
- Sorting and Searching
  - Collections Class
  - Comparators
  - Searching
- Random numbers
  - Random numbers - intro
  - Random numbers - API
  - Shuffling
- Math
  - Math and java.util.Random

### Other

- Time
  - Various time classes
  - javax.swing.Timer
  - java.util.Calendar
- Sound
  - Beep
  - Audio
- Memory
  - OutOfMemoryError

### GUI

- Basics
  - GUI Overview
  - AWT and swing
  - GUI Tips
  - GUI Structural Patterns
- Summaries
  - Summary - Basic Components
  - Summary - Containers
  - Summary - Layouts
- Events and Listeners
  - Events - Introduction
  - Listeners
  - Inner-class Listeners
  - Anonymous Listeners
  - Top-level Listeners
  - Action, AbstractAction
- Components
  - Components
  - Introduction to Components
  - Where to declare components
  - Intro to components
    - Buttons
    - Text
  - Labels
    - JLabel
    - JLabel font and color
    - JLabel setting text
  - Buttons
    - JButton
    - JButton Appearance
    - Buttons with Icons
  - Textfield
    - JTextField
  - Textarea
    - JTextArea
    - Example - JTextArea
    - Newline Characters
  - Radio buttons
    - Radio Buttons
  - Checkboxes
    - JCheckBox
  - Sliders
    - Sliders
    - Change Listener
  - Combobox
    - JComboBox (uneditable)
  - Menus
    - JMenuBar, JMenu, ...
- Containers
  - Containers
  - Windows
    - JFrame - Window
    - JFrame Close Box
    - Window Size and Position
    - Content Panes
  - Dialogs
    - Dialogs
    - JOptionPane - Simple
    - JOptionPane - More
    - JFileChooser
    - FileFilter
  - Panels
    - JPanel - Container
    - JTabbedPane
- Layouts
  - Layouts
  - FlowLayout
  - BorderLayout
  - GridLayout
  - BoxLayout and Boxes
  - BoxLayout spacing
  - BoxLayout alignment
  - GridBagLayout
  - Null Layout
  - Alternate Layouts
    - Alternate Layout Managers
- GUI Program Structure
  - Big Blob Structure
  - Presentation Model
  - MVC - Model View Controller
  - MVC with less coupling
- Fonts
  - Font
  - FontMetrics
  - Fonts - Centering Text
  - Unicode
  - FontDemo example
- Borders
  - Borders
  - Border Example
- Misc
  - Cursor
  - Look and Feel
  - GUI Alternatives

### Graphics

- Summary: Graphics
- Drawing
- Screen Coordinates
- Graphics class
- Who calls paintComponent
- Color
- Polygons
- Drawingpanel
  - JPanel - Drawing Area
  - JPanel - Drawing Area 2
  - JPanel - Example
- Images - BufferedImage
- Images - ImageIcon
- Graphics2D
  - Graphics2D
  - Antialiasing

### Mouse and Keyboard

- Mouse
  - The Mouse
  - MouseListener
  - MouseMotionListener
  - Mouse Listeners
  - Mouse Buttons, Modifier Keys
- Keyboard
  - Keyboard
  - Example - KeyDemo

Acknowledgements.