Program Design & Discrete Mathematics

Download Report

Transcript Program Design & Discrete Mathematics

Software – Computer
Programs
“Computers do exactly what you tell them
to do. This is often quite different from
what you meant to tell them to do.”
Programming

What is programming?



Creation of order
Planning or scheduling the performance of
a task
What is computer programming?

“The process of specifying the data types
and the operations for a computer to
apply to data in order to solve a
problem”.
Data and Information

Information


any knowledge that can be communicated.
Data
information in a form the computer can use
 comes in different forms : letters, words, integer
numbers, real numbers, dates, times, coordinates
on a map etc.
Each type of data is said to have a specific data
type

Computer program
“Data type specifications and instructions
for carrying out operations that are
used by a computer to solve a problem”
How do we write a Program?
PROBLEM-SOLVING PHASE
IMPLEMENTATION PHASE
Analysis and
specification
General solution
Concrete solution
(program)
(algorithm)
Test
verify
Maintenance phase
Programming Language



Set of rules, symbols and special words used
to construct a computer program
Simplified form of English ( with math
symbols) that adheres to a strict set of
grammatical rules
Why not translate from English directly into
instructions for the computer?


Too complicated
Too ambiguous
Other terms

Coding


Running code


Execution of code
Debugging


Translating an algorithm into a programming
language
Determining what is wrong with a program and
modifying it, or the algorithm, to fix it.
Implementation

Coding and testing of an algorithm
Differences in implementation
John’s Java code
PROBLEM
John’s C++ code
John’s Pascal code
Algorithm
Peter’s Java code
Mary’s Java code
Simon’s Java code
Programming shortcut: More time spent debugging
and revising program
“Think first and code later!”
“Programmers who pay close attention to
their programs by checking them before
running them actually spend
significantly less time producing
successful programs. In contrast those
programmers who simply write a
program and then run it to see what it
does spend more time.”
How is a program converted into
a form that a computer can use?

What is the form of data that the
computer uses?



All data is converted into a set of binary
codes, strings of 1s and 0s
Binary codes are distinguished by the
manner in which the computer uses them.
Machine language
Machine language to Highlevel languages

Machine language




made up binary-coded instructions used directly
by the computer
tedious and error prone
programs were difficult to read and modify
Assembly language
Low-level programming language in which a
mnemonic is used to represent each of the
machine language instructions for a particular
computer
Assembly Language
Machine Language
ADD
100101
SUB
010011

Machine language to Highlevel languages

Assembly language



Computer not able to process instructions directly
hence a program, written in machine language,
was used to translate from assembly language to
machine language: Assembler
Easier for humans to use than machine language
however programmers still forced to think in terms
of individual machine instructions
High-level languages

Closer to English, and other natural languages,
than assembly and machine languages.
PROBLEM SOLVING TECHNIQUES
HUMAN
THOUGHT
ALGORITHMS
PSEUDOCODE
JAVA
NATURAL LANGUAGE
HIGH LEVEL
PROGRAMMING LANGUAGE
COMPILER
MACHINE CODE
Converting a high-level
language to machine language

Compiler


Source code


Data type specifications and instructions written in
a high-level programming language
Object code


A program that translates a program written in a
high-level language into machine code
A machine language version of a source code
Bytecode

A standard machine language into which Java
source code is compiled
Windows PC
Running JVM
Java program
Java
Compiler
Java
Bytecode
UNIX Workstation
Running JVM
Macintiosh
Running JVM
Java compiler produces Bytecode that can be run on any machine with the JVM
•
Java Virtual Machine (JVM) a program that serves as a language
interpreter
•
Bytecode is the machine language for the JVM
Program
running
Java
JVM
Bytecode Running on X
INTERPRETATION
Machine
Language for
machine X
Type of instructions


Reflect the operations a computer can
perform
A computer can






transfer data from one place to another
receive input from and input device and write it to
an output device
compare data values for equality or inequality
perform arithmetic operations
branch to a different section of the instructions
Programming languages require control
structures to express algorithms as source
code.
Sequence
Statement
Selection
Statement
True
Statement
Statement1
Condition
False
Statement2
Selection
False
Condition
True
Statement1
Subprogram/Method/Function
STATEMENT1
SUBPROGRAM1
STATEMENT2
SUBPROGRAM1
A meaningful collection of
any of the other control
statements
Asynchronous Control
(Asynchronous –“not at the same time”, events can occur at any time)
EVENT
EVENTHANDLER
A subprogram executed
when an event occurs
Object-Oriented Programming
Languages



Earlier programming languages focused on
the operations and control structures of
programming – procedural languages
Procedural languages paid little explicit
attention to the relationship between the
operations and the data. There were few
simple data types
Object-oriented languages focuses on the
relationship between the operations and data
Object-Oriented Programming
Languages



OOPL allow us to collect a data type
and its associated operations into a
single entity called an object.
Objects make the relationship between
the data type and operations explicit.
Objects are complete and self-contained
hence promote reusability
Object-oriented terminology



Class
 A description of an object that specifies
the types of data values that it can hold
and the operations that it can perform
Classes are usually collected into packages
Instantiate
 To create an object based on the
description supplied by a class