september 5, 2006programming 1 of 19 what is programming? programming computer languages objects and...
TRANSCRIPT
September 5, 2006Programming 1 of 19
WHAT IS PROGRAMMING?
• Programming
• Computer Languages
• Objects and Object Oriented Programming
September 5, 2006Programming 2 of 19
Many Aspects of Programming
• Programming is controlling– computer does exactly what you tell it to
• Programming is teaching– computer can only “learn” to do new things if you
tell it how
• Programming is problem solving– always trying to make computer do something
useful — i.e., finding an optimal travel route
• Programming is creative– must find a good solution out of many possibilities
• Programming is modeling– describe salient (relevant) properties and
behaviors of a system of components (objects)
• Programming is abstraction– identify important features without getting lost in
detail
• Programming is concrete– must provide detailed instructions to complete task
September 5, 2006Programming 3 of 19
What’s a Program?
• Model of complex system– model: simplified representation of salient features
of something, either tangible or abstract– system: collection of components that work
closely together
• Sequences of instructions expressed in specific programming language:– syntax: grammatical rules for forming instructions– semantics: meaning/interpretation of instructions
• Instructions written (programmed/coded) by programmer– coded in a specific programming language– programming languages allow you to express
yourself more precisely than natural language– as a result, programs cannot be ambiguous– all instructions together are called source code
• Executed by computer by carrying out individual instructions
• Real world example:– library catalog, word processor, video game, ATM
September 5, 2006Programming 4 of 19
Java Programs
• CS15 and CS16 use Java– Java was developed by Sun Microsystems– it is meant to run on many “platforms”
• Programs you write in Java can run in two ways:– as normal applications
• just like programs that you are used to: Microsoft Word, Adobe Photoshop, Internet Explorer, etc.
– as Java applets • applets are a particular kind of program (typically
similar to normal applications) which can be embedded in a web page.
• web page is a document on Internet’s World Wide Web (WWW) which can be viewed with a web browser (such as Mozilla Firefox or Microsoft’s Internet Explorer)
September 5, 2006Programming 5 of 19
The Computer Onion
• Layers of software– cover hardware like an
onion covers its core– make it easier to use
computer– organized into libraries
and programs
• In CS15, we only deal with the outermost layers
your Java Program
X/Windows
Linux
hardware(PC)
Ogres have layers. Onions have
layers. You get it? We both have
layers.
September 5, 2006Programming 6 of 19
Two Views of a Program
user interface
software layers hidden by user interface
user’s view
programmer’s view
September 5, 2006Programming 7 of 19
Programming Languages
• Machine language– machine is short for computing machine (i.e.,
computer)– computer’s native language– sequence of zeroes and ones (binary)– different computers understand different sequences– hard for humans to understand:01010001...
• Assembly language– mnemonics for machine language– low level: each instruction is minimal– still hard for humans to understand:ADD.L d0,d2
• High-level languages– FORTRAN, Pascal, BASIC, C, C++, Java, etc.– high level: each instruction composed of many low-
level instructions– closer to English and 9th grade algebra– easier to read and understand:hypotenuse = Math.sqrt(opposite * opposite + adjacent * adjacent);
September 5, 2006Programming 8 of 19
Running Compiled Programs
• In CS15, we want to code in a high-level language, Java
• But each type of computer only “understands” its own machine language (zeroes and ones)
• Thus we must translate from Java to machine language– a team of experts programs a translator, called a
“compiler” which translates entirety of Java program to an executable file in computer’s native machine language
• Two-step process:– compilation: Java program executable– execution: run executable
• machine executes your program by “running” each machine language instruction in the executable file
September 5, 2006Programming 9 of 19
Object-Oriented Programming
• OOP: Now the dominant way to program, yet it is almost 40 years old! (Simula ’67 and Smalltalk ’72 were the first OOPLs)– Dr. Alan Kay received ACM's Turing Award, the
"Nobel Prize of Computing,“ in 2003 for Smalltalk, the first complete dynamic OOPL
• It was slow to catch on, but since the mid-90’s everybody’s been doing it!
• OOP emphasizes objects, which often reflect real-life objects– have both properties and capabilities– i.e., they can perform tasks: “they know how to...”
• Look around you... name that object!
September 5, 2006Programming 10 of 19
OOP as Modeling
• In OOP, model program as collection of cooperating objects– program behavior is determined by group
interactions– group interactions are determined by individual
objects
• In OOP, objects are considered anthropomorphic– each is “smart” in its specialty– i.e., bed can make itself, door can open itself,
menu can let selections be picked– but, each must be told when to perform actions by
another object — so objects must cooperate to accomplish task
• Each object represents an abstraction– a “black box”: hides details you do not care about– allows you as the programmer to control
program’s complexity — only think about salient features
• So, write programs by modeling problem as set of collaborating components– you determine what the building blocks are– then put them together so they cooperate properly– like building with smart Legos that you design!
September 5, 2006Programming 11 of 19
Example: Tetris
• What are the game’s objects?
• What do those objects know how to do?
• What properties do they have?
September 5, 2006Programming 12 of 19
Example: Tetris (cont.)
• What are the game’s objects?– piece, board
• Capabilities: What do those objects know how to do?– piece:
• be created• fall• rotate• stop at collision
– board:• be created• remove rows• check for end of game
• Properties: What attributes and components do they have?– piece:
• orientation• position• shape• color
– board:• size• rows
September 5, 2006Programming 13 of 19
Interpreting vs. Compiling Programs
• An alternative to compiling your program is to interpret your program– each line of your program is translated into
machine language and immediately executed
• Like translating between natural languages– Compiler: human translator translates book
in its entirety and then translated book is printed and read
– Interpreter: human interpreter translates each spoken statement in sequence as speaker is speaking
September 5, 2006Programming 14 of 19
Running Java Programs
• Java uses both compilation and interpretation in a two-step process
• Compiles program into bytecodes– bytecode is close to machine language
instructions, but not quite — it is a generic “machine language”
– does not correspond to any particular machine
• Virtual Machine (VM) interprets bytecodes into native machine language and runs it– different VM exists for different computers, since
bytecode does not correspond to a real machine
• Same Java bytecodes can be used on different computers without re-compiling source code– each VM interprets same bytecodes– allows you to run Java programs by getting just
bytecodes from Web page
• This makes Java code run cross-platform– marketing says, “Write once, run anywhere!”– true for “pure Java,” not for variants
September 5, 2006Programming 15 of 19
Compilation and Execution in Java
• Compilation
• Execution (by interpretation)
Computer A
Java bytecode Compiler
My Javaprogram
My Bytecodeprogram
Computer A
Virtual Machine AMy Bytecodeprogram
output to user
Computer B
Virtual Machine B
user input
(computer-independent)
My Bytecodeprogram
user inputoutput to user
September 5, 2006Programming 16 of 19
Software Development: A 5-Step Process (1 of 4)
1) Analysis– English description of what system models, to
meet a requirement or specification– Work with potential users to develop detailed
specifications• May be written by non-programmer
2) Designing the system– divide & conquer: system is composed of smaller
subsystems which in turn may be composed of even smaller subsystems — in OOP, system is decomposed into a set of cooperating objects
– pseudocode: somewhere between English and Java code
• describe tasks, subtasks, and how they relate– hand-simulation: desk-check pseudocode by
stepping through it without using a computer– often use diagrams to better communicate the
structure of the system
The Waterfall Model
September 5, 2006Programming 17 of 19
Software Development (2 of 4)
3) Implementing the design– In Java (for CS15)– Emphasis on good problem decomposition, well
structured design, and readable, well-documented programming style
– Hand-simulate as you go, may need to backtrack and redesign
– If design is good, most of the hard work should be done
4) Testing and Debugging– testing: submitting input data or sample user
interactions and seeing if program reacts properly– typically done in stages, starting with individual
components and working up to subsystems, and eventually the entire program
– debugging: process of removing program bugs– bugs: errors in code or design
September 5, 2006Programming 18 of 19
Software Development (3 of 4)
5) Maintenance– Finally, keep the program working and current– In a successful piece of software, maintenance is
often said to be 80% of the effort
• Result: Working program– solves original problem
• Good program– satisfies the specification– produced on time and within budget– user-centric design– uses creativity to solve problem– well structured/organized/written/documented– can be easily extended and maintained
September 5, 2006Programming 19 of 19
Software Development (4 of 4)
• Other Models:– Software development not usually as linear as
waterfall model• Often go back during implementation to change the
design• Frequently go back during testing and debugging to
change implementation• This is known as the “spiral model” of software
development• Emphasis on designing, implementing, and testing a
small version of the system, and then repeating these steps for larger and larger versions of the system
– Extreme Programming – Don’t try this at home• Used by experienced programmers working in teams• De-emphasizes detailed upfront design• Start implementing immediately and test the program
right from the beginning
The Spiral Model