Overview
Course topics
Java
The main thrust of the class is about programming in Java.
We will spend as much time as necessarry on the basic syntax, but we want
to focus on the use of class libraries and Java coding style.
Problem solving and algorithms
The first step in writing a program is developing the algorithm.
In general,
An algorithm is an ordered set of unambiguous ,executable steps
defining a terminating process.
At each step, it is clear what to do next.
Ambiguity is in the eyes of the beholder.
The algorithm maybe clear but the representation is not.
'Make a peanut butter sandwich' is clear enough for me but a small
child needs more details.
It must be something that can be done.
Calculate
all digits in pi is not.
Count all the M&M's in a bag is.
Things like the pi calculation that never end
are of little use.
Some algorithms are designed to be non-terminating like
operating systems or medical monitors.
Algorithm representation is tricky.
Using natural language leads to ambiguities.
For example, time flies like an arrow has several meanings.
It is important that all the
partners agree on the terms used to build the representation.
These are called primitives.
Programming languages are built to be unambiguous.
We all agree what the primitives are and how they are combined.
The compiler is the enforcer.
The primitives in a language have both syntax - what it looks
like and semantics - what it means. The syntax of
assignment in Java is
a = b;
The semantics are that the value stored in b is copied to a.
Programs
A program is different from an algorithm.
A program is a precise implementation
of an algorithm and is written in a formal computer language.
Designing one is the hard part of programming.
A good approach is to first state
clearly what problem is being solved.
Then work out the algorithm that solves it.
Test the algorithm by running it in your head on some test cases.
Then implement the algorithm in a computer language, compile it and test
the actual running application.
We'll see examples of this as we go.
First, a quick overview of object oriented programming, which is related to design.
Overview of Object Oriented Programming (OOP)
This is primarily the grouping of code and data together.
Encapsulation and inheritance are the main features.
This differs from structured programming
mostly in syntax and the inheritance idea.
Why OOP?
What problem were we trying to solve?
Mostly complexity and re-use.
People noticed that code that was grouped together with the data it manipulated
was easier to follow.
Also, complex data problems could be simplified by
making a graph of them.
- Software re-use
Code is hard to re-use since everyone codes the interfaces slightly differently.
At least differently than you want them to be.
Java classes allow a developer to present an interface to the user so they can access
the data without messing with the internals.
- Control of complexity
Systems with a large number of related but different data objects were
hard to understand.
Inheritance and access control makes that easier.
Magic
number 7 + or - 2.
People can only hold about 7 things in their head at the same time.
So if you can lump things together, you can hold more of the program in
your head.
- Code understanding
Similar problem.
Break code into smaller modules to ease understanding.
Definitions and principles
- Encapsulation
- Data and code together
- Object data can only be used by object code
- Attached code called methods
- Polymorphism
Multiple functions with the same name.
Allows more natural function naming.
Most data objects have a print operation.
Now all can be called print().
- Inheritance
There is often a natural hierarchy to data objects.
All rooms have some things in common like walls and doors.
But some rooms have windows.
Some rooms have plumbing.
Some walls go to the ceiling and some don't.
Rather than describe everything for each kind of room, create a generic
room object and have the special rooms add to that, subtract from that
or overload that.