Simply Java: Object Oriented Problem Solving
jrl
0. Preface
-
Motivation for writing this book
-
What's cognitive psychology got to do with this?
-
What this book will cover
-
What it won't
-
How to use this book
Section I: Introduction to Computing
I. What is computing anyway?
A. Computing as information processing
-
The notion of state
-
Definition of algorithm
-
Action! The only three statements that do anything.
-
Input statements - information comes into the program
-
Assignment statements - change the information in (state of) a variable
-
Output statements - information goes out of the program
-
Structure. Everything else.
-
Control structure - selects which statements are executed, in what order, how many times
-
Data structures -
-
Class structure -
B. Computing as a revolution
-
The web as a new cultural phenomenon
-
People have never had a tool for processing information before
-
Allows the automation of process and reasoning about process
-
Our image of ourselves
-
Societal impacts
-
The end of bureaucracy?
-
Privacy and data collection
-
Commerce
II. The past: A (very) brief history of computing
A. Living in scaffolding - a cautionary tale
B. Why are we still using this prototype?
-
Why prototypes are useful: Problem Solving Principle #1
-
Von Neumann's prototype
-
Von Neumann wanted to build a thinking machine; an analog device
-
But, he decided first to build a prototype with only the values 0 and 1.
C. Evolution of computing
-
Hardware
-
Vacuum tubes
-
The transistor
-
Integrated circuits
-
VLSI and stuff
-
The microcomputer
-
Education, language and culture
-
Language in a changing culture
-
On why being a short lived creature is advantageous
-
Software: programming the hardware
-
wires
-
binary
-
hex
-
assembly code
-
C
-
C++
-
Java
-
virtual machines
-
the web
III. People in computing
A. A caveat: This is just one perspective.
B. Electrical Engineers: Designing/building the hardware
C. Programmers: Designing/building the software
IV. Programming and cognitive psychology
A. What's a program? Two perspectives:
-
A series of instructions in memory, that are executed by the CPU
-
An implementation of an algorithm in the grammar of a particular programming language.
-
Syntax and illegible
-
The tyranny of the compiler
-
brainless automata
-
why programming is difficult
B. Computer/human complementarity
C. 7+2 limit to working memory
V. Programming methodologies
A. Software engineering
-
Why can't software be like hardware?
-
How do you tell art from science?
-
Reliability
-
Perhaps, like OOP, software engineering is best applied to big programs.
-
The low end of reliability -- does it work at all?
-
Style
-
legibility
-
flexibility
-
robustness
B. Object oriented programming
-
Time honored attributes of OOP
-
Encapsulation
-
Inheritance
-
Polymorphism
-
Code reuse
-
Java as a machine shop
-
Takes time to learn to do anything/Tremendously efficient later
-
Overkill to build a bird feeder
-
Designed for programming in the large.
-
How to tell if you are (or might become) a systems programmer.
-
A different paradigm
-
Are classes just "syntactic sugar"? Yes! (and no.)
-
Confessions of an old procedural programmer
Section II: Introductory Java programming
VI. Java programming
A. The design of Java
-
Designed for the web
-
Designed for GUIs
-
powerful, general purpose I/O package
-
Generic data structures in java.util
-
C++ syntax; C++ constructs
-
Objective-C style class hierarchy
B. How Java is different from other programming languages
-
An object language
-
Web specific features
-
Built-in asynchronous Image handlers
-
predefined classes
-
Socket, SocketServer
-
URL
-
Automatic repainting of every component in a Frame (??)
-
Byte-code
-
An intermediate form
-
Like PostScript
-
The virtual machine (VM)
-
Standing on the shoulders of giants: using packages
C. Advantages
-
Can run on any machine (that has a compatible VM)
-
Useful language features
-
Systems tools
-
Garbage collection
-
Threads
-
Monitors
-
Generic data structures
-
Stack
-
Vector
-
Hashtable
-
Growing set of APIs
-
The coherent class structure, once learned, supports all programming.
D. Disadvantages
-
Not stable
-
Incompatible VMs
-
various platforms/OSs/browsers
-
"Write once, debug everywhere"?
-
Need to constantly learn new features
-
Difficulty in training the trainers.
-
Difficult to get started
-
Large and complex.
-
Until the programmer internalizes the class structure and conventions, it is hopelessly confusing.
E. Programming as a process
-
A habit of thinking
-
Like juggling; you can't learn it unless you practice.
-
Successive approximations
-
"growing" programs instead of "building' them
F. Where do we start? Top-down vs. bottom-up
-
How does a Java program effect processing?
-
All processing is accomplished by sending messages to objects (this is almost true).
-
anObject.doSomething() sends anObject the doSomething() message.
-
The composition of a Java program
-
Declarations
-
Classes
-
Methods
-
Variables
-
Statements
-
Methods are composed of statements
-
Java statements have a clearly defined syntax and semantics
-
imports
-
abracadabra!
-
There are some things that cannot be explained to a neophyte
-
It's called code for a reason!
VII. Programming: A quick dip in the pool
A. Problem solving
-
Problem solving is what you do when you are stuck.
-
When you first learn to program you get stuck frequently.
-
An accomplished programmer has much experience solving the problems that arise in the course of programming
-
Generic problem solving techniques and specific Java/programming techniques.
-
On having a tool kit for solving problems.
-
A first problem solving technique: Counting
B. The two types of Java programs: Applications and Applets
-
What's the difference?
-
Applets can run from web pages, but can only do certain things
-
Applications can run independently, and can do anything
-
Programming examples.
-
Why experienced programmers always run this program first.
-
The least you can do - if you can't do this, you can't do anything.
-
What every Application must have.
-
A class
-
Including public static void main (String [] args) - abracadabra!
-
A minimal Application
-
-
public class Hi{
-
public static void main(String [] args) {
-
System.out.println("Greetings");
-
}
-
}
-
A minimal Applet
-
import java.applet.*;
-
public class MinimalApplet extends Applet {}
-
Does this do anything?
-
How does it?
-
A greetings Applet
-
import java.applet.*;
-
import java.awt.*;
-
-
public class GreetingApplet extends Applet {
-
public void paint(Graphics g) {
-
g.drawString("Greetings", 0, 100);
-
} // paint
-
} // GreetingApplet
-
-
Mechanics: Typing and running your code
-
creating source files
-
invoking the compiler
-
executing the program
-
Application
-
Applet
C. A (very) simple ATM simulator
-
The problem: write a minimal ATM program that will manage 3 bank accounts, allowing users to withdraw (simulated) funds, and keep track of the balances, in addition to their name.
-
Overall program design.
-
The model-view-controller pattern
-
Where will the information about customer names and balances be stored?
-
A generic problem solving technique
-
What are the things?
-
what are their relationships?
-
Class design for Account
-
Information to be stored for each account
-
Name
-
Balance
-
Actions to be performed on that information
-
Withdraw
-
Display
-
Converting the design to Java code
-
Variables (state)
-
containers for information
-
hold exactly one value at a time
-
Type, name and value
-
Methods (action)
-
Accessing/changing information in an object
-
How to get the value of a variable that's inside an object?
-
How to set the value of a variable that's inside an object?
-
How to handle withdrawals.
-
What action to take; how to alter the object's state?
-
Parameters: What information do you need to accomplish the action?
-
How to display the information in the object.
-
The Account class
-
Declaring variables
-
Declaring methods
-
Objects and classes
-
Cookies and cookie cutters metaphor
-
Classes are patterns (cookie cutters) for making objects (cookies).
-
All objects of a particular class have the same form (like the cookies from the same cutter).
-
The cookie cutter doesn't taste very good.
-
Instantiation! Alakazam!
-
new Account();
-
instance variables
-
Testing the Account class
-
Why
-
How
-
Driver programs
-
The user interface
-
More than one Account -- finally!
VIII. Graphics, Color, Inheritance & Composition
B. Displaying an object graphically
C. Example: Drawing two eyes
-
The task we are to undertake
-
Design
-
Growing our program (as opposed to writing it)
-
Problem solving technique: If the problem is too hard, then do something simpler but similar.
-
Always have a working program
-
Test as you go
-
Stepwise implementation
-
The Circle class
-
State - What information do we need to completely describe the state of a circle?
-
Action - What must a Circle do?
-
toString()
-
Displaying a Circle
-
Using toString() and drawString()
-
A second Graphics method - drawOval
-
class FilledCircle extends Circle
-
Have the data of the super class
-
Have the methods of the super class
-
Can add additional methods
-
Can override methods - to change functionality
Section III: A second cut at Java programming
IX. Class design I: Towards consistent classes
A. Motivation
-
If every class is similar, the programmer's cognitive overhead is reduced.
-
JavaBeans are a more sophisticated example of this.
B. What every class should have
-
Data (state)
-
attributes
-
name
-
type
-
value
-
Auto initialization
-
Each instance of a class has a copy of each instance variable
-
Methods (control)
-
Constructors - are invoked when on object is created
-
Accessors - give other objects access to information inside objects of this type
-
getters - get values
-
setters - set values
-
parameters
-
return types
-
public String toString() - converts an object to a String
-
Everything else - do whatever you wrote the class to do
-
Those methods form a framework for the class
-
most classes have other methods in addition
C. The class maker
-
Repetitive tasks should be automated
-
Takes the drudgery out of creating a new class
D. The Account class as produced by the class maker
E. Details
-
Types
-
Expressions
-
Casting
-
Special to Java
-
super, this,
-
super(), and this()
-
Conventions
-
Naming conventions
-
Case conventions
-
The importance of good names
X. Software reuse
A. Two main techniquies
-
Inheritance
-
Composition
B. Composition Programming Example: A snowman
-
A snowman is three white FilledCircles
-
Displaying the snowperson
-
Where the three circles go
-
How big are they?
-
Keeping things simple
-
Simple methods
-
Letting the computer do the arithmetic
-
Making the snowman melt.
-
Adding a melt for a day button
-
The puddle under the snowman
XI. A slightly more complicated programming example: Animating simulated molecules in a box
A. Introduction to Object design.
-
Understanding about the problem
-
Actors, scripts, and forgetting to go on stage.
-
Problem solving technique: What are the things, what are their relationships?
B. Extending the Circle class to make a Ball class
-
Inheritance revisited.
-
Overriding methods.
C. Making the ball bounce
-
A very simple simulation
-
Time in steps
-
why we need looping constructs
-
the for loop
-
initialization
-
continuation condition
-
update
-
Updating the state of the Ball
-
updating the position
-
updating the display
-
first undraw
-
then draw
-
Gravity - can it be this easy?
-
Animation!
-
Threading the simulation
-
Two reasons
-
User controls will still work
-
Animation works
-
The magic (this will make sense later; for now, just copy it!)
-
The mystery of update, paint and repaint
D. Giving the user control. Mouse clicks.
E. Molecules in box; many instances of a Ball (see A Second Cut at Programming).
XII. Control structure, finally.
A. Introduction
-
Procedural programming and control structure
-
Object programming allows you to substitute class structure
B. Different actions depending on conditions - Conditional execution
-
if - do something or don't
-
syntax
-
semantics
-
Example - preventing overdrafts from the ATM
-
if-else do one thing or another
-
syntax
-
semantics
-
Example - preventing overdrafts while alerting the customer to the problem
-
cascaded if-elses -- do one of a number of things
-
syntax
-
semantics
-
Example - reporting the score of a tennis game
-
switch -- do one of a number of things
-
syntax
-
semantics
-
Example - a more concise score reporter
-
Programming example: using the score reporter class in a tennis score keeping program.
-
Model/View/Controller pattern with all three components
-
The rules of tennis: The first player to win 2 sets wins
-
Making is smaller; let's just play a single game.
-
How event driven programming allows us to avoid loops.
C. Repeated action
-
Recursion
-
Iteration
-
while
-
for
XIII. Files
B. Reading text from a file
C. Writing text to a file
D. Persistence
-
ObjectStream
-
Serializable - an interface
XIV. Data structures
B. Arrays
-
Arrays are Objects
-
Must be instantiated!
-
Examples
C. Lists
-
A simple List class
-
Design
-
Representation
-
Actions
-
state
-
an array of values
-
an int which tells how many things are currently in the list
-
actions
-
Sorting using a List
-
java.util.Vector
D. Generic java.util data structures
-
Vector
-
Hashtable
-
The Enumeration interface
XV. Putting it all together: some more complex examples.
A. A box full of molecules
B. A bank database
-
Using a List
-
Using an Vector
-
Using a Hashtable
Section III: Advanced topics
XVI. A very simple web browser and server
XVII. Client/Server computing
A. Creating a simple chat room
B. Building a multiplayer internet game
XVIII. Appendices
A. Partial, preliminary glossary
B. HTML code to run an applet