the PROGRAM - Homepages at WMU

advertisement
CS1110 Focus
1. Problem solving





Understand the problem & requirements (I P O)
Design modular program
Design algorithm(s)
Code
Test & Debug
2. Programming
 PP (Procedural Programming)
 OOP (Object-Oriented Programming)
3. Java language (the basics)
4. IDE: NetBeans (the basics)
1
What’s “a computer”?
Laptop, desktop
[CS1110 focus]
Mainframe, supercomputer
Tablet, smart phone, Tivo, Xbox, …
Server: print server, file server,
DB server, web server, …
2
2 parts of a computer
Hardware (HW)
1.
2.
3.
4.
5.
CPU
Memory (RAM, …)
Storage: Disk, CD, DVD, thumb drive, SD card,…
I/O devices
Connectivity: network, wifi, bluetooth, ethernet,…
Software (SW)




[CS1110 focus]
programs
makes computer “smart”
controls HW
hides most HW from user
3
HW – CPU
1) CU (Control Unit)
- Boss
 fetch & decode instruction
(the one specified in PC (program counter) )
 call & pass data to/from other HW units
2) ALU (Arithmetic & Logic Unit)
 arithmetic
 comparisons
- Worker
+ - * /
== != < > <= >=
4
HW - IPO (Input/Processing/Output)
CPU
Arithmetic
Logic
Unit
Instruction (input)
Result (output)
Control
Unit
1-5
HW – Storage
1 - Primary
[internal]
 memory (RAM)
 temporary, volatile, small, fast-access
 e.g., 4GB, . . .
2 - Secondary
[external]
 disk, CD, DVD, SD card, thumb drive
 permanent, large capacity, slow-access
 e.g., 500GB, . . . 1TB, . . . HD
(or 8GB flash drive in netbook) . . .
6
HW - RAM
 “Random Access Memory” contains:
1. Currently running PROGRAMS
2. DATA used by those programs
 RAM divided into bytes,
bytes grouped into words
 Each word has a unique address
7
HW - bits, bytes, words
 1 byte = 8 bits
 a bit is either ON (1) or OFF (0)
 1 word = 4 bytes (32-bit system)
= 8 bytes (64-bit system)
 Bytes/words contain:
 Machine language instruction
 OR Data:
 1 char (‘A’) stored in 1 byte (or 2 if Unicode)
 1 integer (300129876) stored in 4 bytes
 etc.
8
HW – I/O
Input:
 keyboard, mouse, touchscreen, . . .
camera, scanner, microphone, . . .
 file, DB, internet, . . .
Output:
 screen, printer, AV device, . . .
 controller for machine / robot, . . .
 file, DB, internet, . . .
9
SW (= programs)
system SW
 OS, utilities, device drivers, compilers,
text editors, network SW, . . .
application SW
 general-purpose
 DBMS, MS Office, browser
 application-specific
 payroll, WMU registration
10
Types of Programmers








Application programmer
Systems
“
Database
“
Network
“
Web
“
...
AI
“
Games
programmer
11
Software Engineer
Programmer
Systems Analyst
- “Developer”
- “Designer”
SW Engineering activities:
 Plan, design, code, test, document
 GUI (Graphical User Interface)
 Program modules (classes, methods)
 Code (= write program)
 Customize purchased package
 Build SW from components
 ...
12
Program
 recipe
 detailed step-by-step
set of INSTRUCTIONS
 tells computer EXACTLY what to do
 controls HW
 processes data
 an algorithm to solve a problem,
implemented in a programming language
13
Algorithm
 set of well-defined steps
to complete a specific task
 steps performed sequentially
(unless…)
 algorithm translated to machine language
 algorithm written in
pseudocode or flowchart or . . .
 developer implements algorithm in a
high-level language (like Java)
 compiler produces machine language
(all 0’s and 1’s)
1-14
IPO model
Input

IP(S)O model
Processing
& Storing

Output
15
IPO model
HUMAN
 see/hear
[think & remember]  speak/write
HW
 mouse/KB …  [CPU & RAM & disk]  screen, …
SW
(traditional program)
 data
 [process & store]  data
(user/file/DB)
(user/file/DB)
^^^^^^^^^^
[= the PROGRAM]
16
IP(S)O - SW
windows application PROGRAM
user input  [process & store]  screen display
mouse clicks 
 DB
data in a form 
a program METHOD
input parameters [procedure]
 return value
[& local variables]
[& class’s instance variables]
17
Data
 text, numbers
 graphics, sound, images, movies, . . .
 mouse clicks (single/double, left/right),
mouse hovers, . . .
 web page, text message, . . .
18
Types of applications
 Batch processing
 Typically: file in, file/printer out
 Interactive
 simple text I/O with user (Console App)
 GUI (Windows App)
(Web App)
Java can do all of these
CS1110 – mainly Console Applications
19
Windows App vs. Console App
Event-driven
Windows app
Console app
Input:
PUSHed into program PULLed into program
program by user
by program
Controller:
user
program (main)
Interface:
windows/GUI/
visual/web/. . .
console (text)
Mode:
interactive
batch or
simple text I/O
20
Types of Programs






Event-driven
Modular
Visual
Structured
Procedural
Object-oriented

program
program
program
program
program
program
OOP
- these overlap
- Java can do all of these
- CS1110: Modular/Structured/ Procedural/OOP
21
Event-driven programming
IPO:
event 
[handler module
in program]
 effects
Events:
left-mouse-click on button/slider bar/menu item,
mouse hovers over X,
user hits Enter key, hit F5 key,
sensor detects change,
change made to the DB,
...
Program: a module to handle
ANY event that could happen
22
Modular programming
Program = a collection of small modules
A module is:
 (in Procedural Programming)
 an IPSO procedure or function
 (in Object Oriented Programming)
 a Class (object)
 a IPSO method within a class

~ procedure
 Programming = write modules
 Top-down or bottom-up
23
Visual Programming
Visual C#, Visual Basic, Java with library of classes
1) Construct GUI from pre-existing components
 Text box, radio button, slider bar, dropdown list,...
2) Adjust properties of these objects
3) Add procedural code (a module) specifying:
WHAT to do for
each EVENT that might happen to this object
(Much code is automatically generated for an object)
24
Structured Programming
All procedural code (Java methods) is made from
STACKING or NESTING of:
1) Sequence Structure
do action1, do action2, do action3, ...
2) Selection (condition) Structure
if conditionX is true then
do action1, ...
else
do action2, ...
3) Repetition (loop) Structure
while conditionX is true then
{
do action1,
do action2,
...
}
25
Procedural Programming (PP)
 older languages mainly PP
 a procedure =
 set of statements which do a specific task
 program is mainly a set of procedures
 procedures
 operate on program’s data
 data typical separate from procedure itself
 data commonly passed
from one procedure to another
PP
 Data may be
 global, so available to all procedures
 OR passed to & from procedures
 DISADVANTAGE:
If data formats change
then procedures that operate on that data
must be changed
1-27
Object-Oriented Programming
(OOP)
 OOP focus: create objects
(vs. procedures)
 Objects = combination of BOTH
1. Data – the attributes of the object
2. Procedures that manipulate that data
- methods
(behaviors, local procedures,
public services)
1-28
OOP
 Encapsulation - combine data & behavior
 Data hiding = object X’s data not visible
to other objects in program
 Only object X’s methods can
directly manipulate object X’s data
 Other objects can only access/manipulate
object X’s attributes VIA object X’s methods
1-29
Programming
= problem-solving
  solution
1) Solve the right problem AND
2) Solve the problem right
Determine:
WHAT needs to be done
HOW to do it
(the algorithm)
30
Example Problems
iPhone/Android app
List Song titles in alpha order on iPod
Calculate final grade in CS1110
Pay off a car loan of amount: Amt
at interest rate %: Rate
over ? Years:
Years
 Google Maps
– find shortest route KZoo  NY




31
Steps in programming
1. Requirements specification

input, processing, output
2. Program design

(how)
algorithm, modules, GUI
3. Coding (development)
4. Testing & debugging


(what)
[in Java]
compile errors & logic errors & runtime errors
validate output results
5. Documentation (external)
6. Maintenance
32
Algorithm (the “P” of IPSO)
EXAMPLE:
find sum of 1st 100 integers
User’s view: BLACK-box
Programmer’s view: WHITE(“clear”)-box
(write & test actual code)
Program’s processing (algorithm) could:
 Look it up in a table / file / DB
 Crowdsource the micro-task on the web
 Calculate it using Algorthm1: 1 + 2 + 3 + …
OR Algorithm2: formula: (N * (N+1)) / 2
33
Construct program from
 pre-existing classes/methods in library
 just need to know method interface
 classes/methods
written by programmer
34
Basic Operations (processing)
used in a program
1) Actual Work
 arithmetic
 comparison ( =, <, >,
and, or, not)
2) Move/store data
 Assignment
 I/O (Read)
Mem  Mem
KB/mouse/text-on-screen/touchscreen/file/…  Mem
 I/O (Write)
Mem  screen/printer/file/…
35
3) Control the flow
(what instruction executes next)




default: do next line
maybe do this line
jump to specific line
goto & return
(if, switch)
(loop, break)
(call)
4) Packaging
 Methods (procedures)
 Classes
36
1st & 2nd Generation
Programming Languages
Machine Languages (ML)
 11010010001010011110000111000111
 1940’s programmers wrote in ML
 Machine-dependent
- each CPU has its own ML (Mac vs. PC)
Assembly Languages
 Add
210(8,13)
 Machine-dependent
37
3rd Generation Languages
High Level Languages (HLL)
 Java, C, C#, C++, Python, Ruby,
PHP, Visual BASIC, COBOL, Javascript
 Not processor-dependent
 But usually needs own machine-specific
compiler
average = (ex1 + ex2 + ex3) / 3;
38
3rd Generation Languages
2 main programming paradigms
 Procedural (PP)
 C, COBOL, Fortran, Basic, ... ,
 any OOP language can do PP
 Object-oriented (OOP)
 Java, C#, C++, Visual Basic
 Revised versions of COBOL
39
4th & 5th Generation Languages
Application-specific Languages
e.g., SQL for DBS (describe WHAT not HOW)
Select name, phone from student
where major = “CS” and state = “MI”;
Natural Languages (English, . . .)
If patient is age 65 or older
and is disoriented
and has pain in his/her left arm
then patient could have had a heart attack
40
Download