Simply Java: Object Oriented Problem Solving

jrl

0. Preface

  1. Motivation for writing this book
  2. What's cognitive psychology got to do with this?
  3. What this book will cover
  4. What it won't
  5. How to use this book

Section I: Introduction to Computing

I. What is computing anyway?

A. Computing as information processing

  1. The notion of state
  2. Definition of algorithm
  3. Action! The only three statements that do anything.
    1. Input statements - information comes into the program
    2. Assignment statements - change the information in (state of) a variable
    3. Output statements - information goes out of the program
  4. Structure. Everything else.
    1. Control structure - selects which statements are executed, in what order, how many times
    2. Data structures -
    3. Class structure -

B. Computing as a revolution

  1. The web as a new cultural phenomenon
  2. People have never had a tool for processing information before
  3. Allows the automation of process and reasoning about process
  4. Our image of ourselves
  5. Societal impacts
    1. The end of bureaucracy?
    2. Privacy and data collection
    3. 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?

  1. Why prototypes are useful: Problem Solving Principle #1
  2. Von Neumann's prototype
    1. Von Neumann wanted to build a thinking machine; an analog device
    2. But, he decided first to build a prototype with only the values 0 and 1.

C. Evolution of computing

  1. Hardware
    1. Vacuum tubes
    2. The transistor
    3. Integrated circuits
    4. VLSI and stuff
    5. The microcomputer
      1. Education, language and culture
      2. Language in a changing culture
      3. On why being a short lived creature is advantageous
  2. Software: programming the hardware
    1. wires
    2. binary
    3. hex
    4. assembly code
    5. C
    6. C++
    7. Java
  3. virtual machines
  4. 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

D. System administrators

E. Technicians

IV. Programming and cognitive psychology

A. What's a program? Two perspectives:

  1. A series of instructions in memory, that are executed by the CPU
  2. An implementation of an algorithm in the grammar of a particular programming language.
    1. Syntax and illegible
    2. The tyranny of the compiler
    3. brainless automata
    4. why programming is difficult

B. Computer/human complementarity

C. 7+2 limit to working memory

D. Confusion is painful

V. Programming methodologies

A. Software engineering

  1. Why can't software be like hardware?
  2. How do you tell art from science?
  3. Reliability
    1. Perhaps, like OOP, software engineering is best applied to big programs.
    2. The low end of reliability -- does it work at all?
  4. Style
    1. legibility
    2. flexibility
    3. robustness

B. Object oriented programming

  1. Time honored attributes of OOP
    1. Encapsulation
    2. Inheritance
    3. Polymorphism
    4. Code reuse
  2. Java as a machine shop
    1. Takes time to learn to do anything/Tremendously efficient later
    2. Overkill to build a bird feeder
    3. Designed for programming in the large.
    4. How to tell if you are (or might become) a systems programmer.
  3. A different paradigm
    1. Are classes just "syntactic sugar"? Yes! (and no.)
    2. Confessions of an old procedural programmer

Section II: Introductory Java programming

VI. Java programming

A. The design of Java

  1. Designed for the web
  2. Designed for GUIs
  3. powerful, general purpose I/O package
  4. Generic data structures in java.util
  5. C++ syntax; C++ constructs
  6. Objective-C style class hierarchy

B. How Java is different from other programming languages

  1. An object language
  2. Web specific features
    1. Built-in asynchronous Image handlers
    2. predefined classes
      1. Socket, SocketServer
      2. URL
    3. Automatic repainting of every component in a Frame (??)
    4. Byte-code
      1. An intermediate form
      2. Like PostScript
  3. The virtual machine (VM)
  4. Standing on the shoulders of giants: using packages

C. Advantages

  1. Can run on any machine (that has a compatible VM)
  2. Useful language features
    1. Systems tools
      1. Garbage collection
      2. Threads
      3. Monitors
    2. Generic data structures
      1. Stack
      2. Vector
      3. Hashtable
  3. Growing set of APIs
  4. The coherent class structure, once learned, supports all programming.

D. Disadvantages

  1. Not stable
    1. Incompatible VMs
      1. various platforms/OSs/browsers
      2. "Write once, debug everywhere"?
    2. Need to constantly learn new features
    3. Difficulty in training the trainers.
  2. Difficult to get started
  3. Large and complex.
  4. Until the programmer internalizes the class structure and conventions, it is hopelessly confusing.

E. Programming as a process

  1. A habit of thinking
  2. Like juggling; you can't learn it unless you practice.
  3. Successive approximations
  4. "growing" programs instead of "building' them

F. Where do we start? Top-down vs. bottom-up

  1. How does a Java program effect processing?
    1. All processing is accomplished by sending messages to objects (this is almost true).
    2. anObject.doSomething() sends anObject the doSomething() message.
  2. The composition of a Java program
    1. Declarations
      1. Classes
      2. Methods
      3. Variables
    2. Statements
      1. Methods are composed of statements
      2. Java statements have a clearly defined syntax and semantics
    3. imports
    4. abracadabra!
      1. There are some things that cannot be explained to a neophyte
      2. It's called code for a reason!

VII. Programming: A quick dip in the pool

A. Problem solving

    1. Problem solving is what you do when you are stuck.
    2. When you first learn to program you get stuck frequently.
    3. An accomplished programmer has much experience solving the problems that arise in the course of programming
    4. Generic problem solving techniques and specific Java/programming techniques.
    5. On having a tool kit for solving problems.
    6. A first problem solving technique: Counting

B. The two types of Java programs: Applications and Applets

  1. What's the difference?
    1. Applets can run from web pages, but can only do certain things
    2. Applications can run independently, and can do anything
  2. Programming examples.
    1. Why experienced programmers always run this program first.
    2. The least you can do - if you can't do this, you can't do anything.
    3. What every Application must have.
      1. A class
      2. Including public static void main (String [] args) - abracadabra!
    4. A minimal Application
  3.  
  4. public class Hi{
  5. public static void main(String [] args) {
  6. System.out.println("Greetings");
  7. }
  8. }
    1. A minimal Applet
  9. import java.applet.*;
  10. public class MinimalApplet extends Applet {}
      1. Does this do anything?
      2. How does it?
    1. A greetings Applet
  11. import java.applet.*;
  12. import java.awt.*;
  13.  
  14. public class GreetingApplet extends Applet {
  15. public void paint(Graphics g) {
  16. g.drawString("Greetings", 0, 100);
  17. } // paint
  18. } // GreetingApplet
  19.  
  20. Mechanics: Typing and running your code
    1. creating source files
    2. invoking the compiler
    3. executing the program
      1. Application
      2. Applet

C. A (very) simple ATM simulator

  1. 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.
  2. Overall program design.
    1. The model-view-controller pattern
    2. Where will the information about customer names and balances be stored?
  3. A generic problem solving technique
    1. What are the things?
    2. what are their relationships?
  4. Class design for Account
    1. Information to be stored for each account
      1. Name
      2. Balance
    2. Actions to be performed on that information
      1. Withdraw
      2. Display
  5. Converting the design to Java code
    1. Variables (state)
      1. containers for information
      2. hold exactly one value at a time
      3. Type, name and value
    2. Methods (action)
      1. Accessing/changing information in an object
        1. How to get the value of a variable that's inside an object?
        2. How to set the value of a variable that's inside an object?
      2. How to handle withdrawals.
        1. What action to take; how to alter the object's state?
        2. Parameters: What information do you need to accomplish the action?
      3. How to display the information in the object.
    3. The Account class
      1. Declaring variables
      2. Declaring methods
    4. Objects and classes
      1. Cookies and cookie cutters metaphor
        1. Classes are patterns (cookie cutters) for making objects (cookies).
        2. All objects of a particular class have the same form (like the cookies from the same cutter).
        3. The cookie cutter doesn't taste very good.
      2. Instantiation! Alakazam!
        1. new Account();
        2. instance variables
    5. Testing the Account class
      1. Why
      2. How
      3. Driver programs
    6. The user interface
  6. More than one Account -- finally!

VIII. Graphics, Color, Inheritance & Composition

A. The Graphics class

B. Displaying an object graphically

C. Example: Drawing two eyes

  1. The task we are to undertake
  2. Design
  3. Growing our program (as opposed to writing it)
    1. Problem solving technique: If the problem is too hard, then do something simpler but similar.
    2. Always have a working program
    3. Test as you go
    4. Stepwise implementation
  4. The Circle class
    1. State - What information do we need to completely describe the state of a circle?
    2. Action - What must a Circle do?
    3. toString()
    4. Displaying a Circle
      1. Using toString() and drawString()
      2. A second Graphics method - drawOval
  5. class FilledCircle extends Circle
    1. Have the data of the super class
    2. Have the methods of the super class
    3. Can add additional methods
    4. Can override methods - to change functionality

Section III: A second cut at Java programming

IX. Class design I: Towards consistent classes

A. Motivation

  1. If every class is similar, the programmer's cognitive overhead is reduced.
  2. JavaBeans are a more sophisticated example of this.

B. What every class should have

  1. Data (state)
    1. attributes
      1. name
      2. type
      3. value
    2. Auto initialization
    3. Each instance of a class has a copy of each instance variable
  2. Methods (control)
    1. Constructors - are invoked when on object is created
    2. Accessors - give other objects access to information inside objects of this type
      1. getters - get values
      2. setters - set values
      3. parameters
      4. return types
    3. public String toString() - converts an object to a String
    4. Everything else - do whatever you wrote the class to do
      1. Those methods form a framework for the class
      2. most classes have other methods in addition

C. The class maker

  1. Repetitive tasks should be automated
  2. Takes the drudgery out of creating a new class

D. The Account class as produced by the class maker

E. Details

  1. Types
  2. Expressions
  3. Casting
  4. Special to Java
    1. super, this,
    2. super(), and this()
  5. Conventions
    1. Naming conventions
    2. Case conventions
    3. The importance of good names

X. Software reuse

A. Two main techniquies

  1. Inheritance
  2. Composition

B. Composition Programming Example: A snowman

  1. A snowman is three white FilledCircles
  2. Displaying the snowperson
  3. Where the three circles go
  4. How big are they?
  5. Keeping things simple
    1. Simple methods
    2. Letting the computer do the arithmetic
  6. Making the snowman melt.
    1. Adding a melt for a day button
    2. The puddle under the snowman

XI. A slightly more complicated programming example: Animating simulated molecules in a box

A. Introduction to Object design.

  1. Understanding about the problem
  2. Actors, scripts, and forgetting to go on stage.
  3. Problem solving technique: What are the things, what are their relationships?

B. Extending the Circle class to make a Ball class

  1. Inheritance revisited.
  2. Overriding methods.

C. Making the ball bounce

  1. A very simple simulation
    1. Time in steps
      1. why we need looping constructs
      2. the for loop
        1. initialization
        2. continuation condition
        3. update
    2. Updating the state of the Ball
      1. updating the position
      2. updating the display
        1. first undraw
        2. then draw
  2. Gravity - can it be this easy?
  3. Animation!
    1. Threading the simulation
      1. Two reasons
        1. User controls will still work
        2. Animation works
      2. The magic (this will make sense later; for now, just copy it!)
    2. 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

  1. Procedural programming and control structure
  2. Object programming allows you to substitute class structure

B. Different actions depending on conditions - Conditional execution

  1. if - do something or don't
    1. syntax
    2. semantics
    3. Example - preventing overdrafts from the ATM
  2. if-else do one thing or another
    1. syntax
    2. semantics
    3. Example - preventing overdrafts while alerting the customer to the problem
  3. cascaded if-elses -- do one of a number of things
    1. syntax
    2. semantics
    3. Example - reporting the score of a tennis game
  4. switch -- do one of a number of things
    1. syntax
    2. semantics
    3. Example - a more concise score reporter
  5. Programming example: using the score reporter class in a tennis score keeping program.
      1. Model/View/Controller pattern with all three components
      2. The rules of tennis: The first player to win 2 sets wins
      3. Making is smaller; let's just play a single game.
      4. How event driven programming allows us to avoid loops.

C. Repeated action

  1. Recursion
  2. Iteration
    1. while
    2. for

XIII. Files

A. What's a file?

B. Reading text from a file

C. Writing text to a file

D. Persistence

  1. ObjectStream
  2. Serializable - an interface

XIV. Data structures

A. What? Why?

B. Arrays

  1. Arrays are Objects
  2. Must be instantiated!
  3. Examples

C. Lists

  1. A simple List class
    1. Design
      1. Representation
      2. Actions
    2. state
      1. an array of values
      2. an int which tells how many things are currently in the list
    3. actions
  2. Sorting using a List
  3. java.util.Vector

D. Generic java.util data structures

  1. Vector
  2. Hashtable
  3. The Enumeration interface

XV. Putting it all together: some more complex examples.

A. A box full of molecules

B. A bank database

  1. Using a List
  2. Using an Vector
  3. Using a Hashtable

C. An army of snowmen

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

C. Glossary

D. BNF

E. Reserved words

F. Packages included