introduction structured programming 256 chapter 1

43
Introduction Structured Programming 256 Chapter 1

Upload: rose-dawson

Post on 29-Dec-2015

226 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction Structured Programming 256 Chapter 1

Introduction

Structured Programming 256

Chapter 1

Page 2: Introduction Structured Programming 256 Chapter 1

© 2000 Scott S Albert

Hardware Platforms

• Platform• Combination of hardware and operating system• Examples

– Intel Pentium and Windows NT– SUN SPARC and Solaris– IBM RS/600 and AIX

• Programs are either designed for a specific platform or to run on many platforms

– For example many programs will only run on the WinTel (Windows/Intel) platform

Page 3: Introduction Structured Programming 256 Chapter 1

© 2000 Scott S Albert

Hardware Components

• CPU (Central Processing Unit)– “Brains” of the computer. All arithmetic,

logical (Boolean), character manipulation etc.– Pentium III is a common processor in new PC’s– Faster CPU’s have higher clock rates, measured

by MHz (megahertz). Pentium III 600 MHz

Page 4: Introduction Structured Programming 256 Chapter 1

© 2000 Scott S Albert

Common Hardware Components

• Processor (CPU)– Central Processing Unit

– Interprets and executes the instructions

• Memory– main & auxiliary

– holds data and instructions

• Input device(s)– mouse, keyboard, etc.

• Output device(s)– video display, printer, etc.

• CPU and memory are physically housed together

Standard Hardware Organization

Memory(main & auxiliary)

Processor(CPU)

Input Devices(such as mouse and

keyboard)

Output Devices(such as video

display or printer)

Page 5: Introduction Structured Programming 256 Chapter 1

© 2000 Scott S Albert

Memory or Main Memory

• Holds your program and its data as well as the Operating System– Basic unit of memory is a bit (binary digit). Each bit

can store either a 0 or a 1

– Bits are grouped into bytes, 8 bits to a byte

– Bytes are grouped into words. Most machines today have 4 bytes (32 bit) words

– Total memory size is measured in megabytes (Mb). Systems today have more than 32Mb

Page 6: Introduction Structured Programming 256 Chapter 1

© 2000 Scott S Albert

Main Memory Organization

• Bit = one binary digit

– Binary digit can have only one of two values, 0 or 1

• Byte = 8 bits

• “Byte Addressable”

– Main memory is a list of numbered locations that contain one byte of data in each location

• Number of bytes per data item may vary

Address Data Byte

3021 1111 0000 Item 1: 2 bytesstored

3022 1100 1100

3023 1010 1010 Item 2: 1 bytestored

3024 1100 1110 Item 3: 3 bytesstored

3025 0011 0001

3026 1110 0001

3027 0110 0011 Item 4: 2 bytesstored

3028 1010 0010

3029 … Next Item, etc.

Page 7: Introduction Structured Programming 256 Chapter 1

© 2000 Scott S Albert

Auxiliary Storage

• Permanent storage of – Programs, data files, help files,

documentation…

• Storage devices– Hard disk, diskette, CD-ROM, CD/RW, tape…

• Sizes– Hard disks(disks) 4-20 gigabytes (billion bytes)– CD-ROM’s 650 megabytes, Diskette 1.44 Mb

Page 8: Introduction Structured Programming 256 Chapter 1

© 2000 Scott S Albert

Memory and Storage

• Memory is volatile, storage is not• Memory is more expensive than storage• Systems are configured with over 100 times

storage as memory (or more)• Memory is much faster than storage• Programs have to be in memory for the

CPU to run or execute them

Page 9: Introduction Structured Programming 256 Chapter 1

© 2000 Scott S Albert

Access Speeds

• Time to access one byte of memory– 8-70 ns

• Time to access one byte of disk storage– 200-1000 x Memory Speed

• Time to access one byte of CD-ROM– 50-100 x Disk Speed

• Time to access one byte of diskette– 50-100 x CD-ROM Speed

Page 10: Introduction Structured Programming 256 Chapter 1

© 2000 Scott S Albert

Software

• Software -- computer programs• Operating System

– Program that manages hardware• Disks, CPU, memory etc.

– And provides an operations user interface • How to start and stop programs, get help etc.

• Examples– Windows 98, MacOS 8.x, Solaris, Linux

Page 11: Introduction Structured Programming 256 Chapter 1

© 2000 Scott S Albert

Many tools to write programs

• High level tools– Very easy to use. Fast coding. Limited in

scope, designed for one or two purposes

• General purpose tools– Harder to use. Harder to code and debug.

Virtually unlimited scope. Designed for general purposes.

Page 12: Introduction Structured Programming 256 Chapter 1

© 2000 Scott S Albert

Many tools to write programs

• High level tools– Visual Basic for Applications (VBA).

Javascript. Word Perfect Macros.– SAS, SPSS

• General purpose tools– C++, Java, Visual Basic, Cobol, Fortran, Pascal

Page 13: Introduction Structured Programming 256 Chapter 1

© 2000 Scott S Albert

Running a program

– The only language a CPU understands is machine language

• Each machine type has its own unique language. For example the language of a Pentium III is different from a Pentium II

• It is very difficult to code in machine language and very few programmers do so.

– Before your can run a program it must be translated or compiled into machine language

– A Compiler does this translation

Page 14: Introduction Structured Programming 256 Chapter 1

© 2000 Scott S Albert

Creating SoftwareCPUCPU

Machine Language

High Level Languages Application Languages

Macro and scripting

Page 15: Introduction Structured Programming 256 Chapter 1

© 2000 Scott S Albert

Operation efficiency

RunsSlow

Executesfastest

Script

PowerBuilder

Java

C++

C

Machinelanguage

Page 16: Introduction Structured Programming 256 Chapter 1

© 2000 Scott S Albert

Programming efficiency

Task programmedslowly

Task programmedrapidly

Script

PowerBuilder

Java

C++

C

Machinelanguage

Page 17: Introduction Structured Programming 256 Chapter 1

© 2000 Scott S Albert

Which is cheaper?

• CPU’s– Fact: CPU prices have dropped every year for

the past 15 years– CPU speed has doubled every 18 months

• Or Programmers– Programmer salaries have increased faster than

the average– There is a worldwide shortage of programmers

Page 18: Introduction Structured Programming 256 Chapter 1

© 2000 Scott S Albert

Conclusion

• Use the most effective tool for the problem– Can you use a script language?– Can you use an application language?– Can you use a general purpose language?

Page 19: Introduction Structured Programming 256 Chapter 1

© 2000 Scott S Albert

From Art to Science

• Programming is a young discipline– First programs were written 50 years ago– Contrast with Civil Engineering

• Early programs were more works of art– No methodology employed, results were not

repeatable

• Today we strive for repeatable results using documented methodologies

Page 20: Introduction Structured Programming 256 Chapter 1

© 2000 Scott S Albert

Object-Oriented Programming

• OOP

• A design and programming technique

• Some terminology:– object - usually a person, place or thing (a noun)

– method - an action performed by an object (a verb)

– type or class - a category of similar objects (such as automobiles)

• Objects have both data and methods

• Objects of the same class have the same data elements and methods

• Objects send and receive messages to invoke actions

Page 21: Introduction Structured Programming 256 Chapter 1

© 2000 Scott S Albert

Example of an Object Class

Data Items:– manufacturer’s name

– model name

– year made

– color

– number of doors

– size of engine

– etc.

Methods:– Define data items (specify

manufacturer’s name, model, year, etc.)

– Change a data item (color, engine, etc.)

– Display data items

– Calculate cost

– etc.

Class: automobile

Page 22: Introduction Structured Programming 256 Chapter 1

© 2000 Scott S Albert

From Problem to Solution

• Analysis

• Design

• Programming

• Testing

• Documentation

• Packaging

Page 23: Introduction Structured Programming 256 Chapter 1

© 2000 Scott S Albert

Analysis

• The process of going from a business problem to a list of tasks or specifications.– User interface (input and output)– Business rules or processing specifications– Error, auditing and logging

• Most of your assignments are written as specifications– Analysis has been performed by the professor

Page 24: Introduction Structured Programming 256 Chapter 1

© 2000 Scott S Albert

Design

• The process of developing one or more object models to implement the analysis specifications– Designers develop a set of classes for major

components of the problem.– Large problems may be divided into separate

components or layers.– Each component consists of one or more classes and a

set of relationships between classes and components.

• All of the problems in this course will use just a few classes in a single component

Page 25: Introduction Structured Programming 256 Chapter 1

© 2000 Scott S Albert

Implementation

• Translation of the design into a programming language– Selection of correct algorithm

• Set of instructions for solving a problem

– Coding class(es) and methods

Page 26: Introduction Structured Programming 256 Chapter 1

© 2000 Scott S Albert

Testing

• Proving that the implementation matches the model and the specifications– Unit testing

• Does the class, or even a method within a class perform as it should?

– System testing• Do all the components work together to implement

the solution?

Page 27: Introduction Structured Programming 256 Chapter 1

© 2000 Scott S Albert

Documentation

• How should the program be used?– Documentation for users– Today user documentation is increasingly integrated

into program operations to reduce the learning curve and improve help system responsiveness.

• How can others modify the system?– Documentation for other programmers– A detailed recording of the final list of classes and

operations– A list of tasks that we didn’t we have time to do

Page 28: Introduction Structured Programming 256 Chapter 1

© 2000 Scott S Albert

Packaging

• Preparing a program for delivery to the customer– Identification of all needed components– Preparation of the installation program– Sample data or other verification process

Page 29: Introduction Structured Programming 256 Chapter 1

© 2000 Scott S Albert

First C++ Program

Page 30: Introduction Structured Programming 256 Chapter 1

© 2000 Scott S Albert

# include <iostream.h>

void main() { cout<<“Hello out there.”;

cout<<“Want to talk some more?”; cout<<“Answer y for yes, n for no.”; char answerLetter; cin>>answerLetter ; if (answerLetter == ‘y’) cout<<“Nice weather we

have”; cout<<“Good-bye”;

}

Page 31: Introduction Structured Programming 256 Chapter 1

© 2000 Scott S Albert

Compiler Directive

# include <iostream.h>

void main() { cout<<“Hello out there.”;

cout<<“Want to talk some more?”; cout<<“Answer y for yes, n for no.”; char answerLetter; cin>>answerLetter ; if (answerLetter == ‘y’) cout<<“Nice weather we

have”; cout<<“Good-bye”;

}

Page 32: Introduction Structured Programming 256 Chapter 1

© 2000 Scott S Albert

The O/S calls the main method to start your program. main is called after your program is loaded into memory. All the statements in main are between { }

# include <iostream.h>

void main() { cout<<“Hello out there.”;

cout<<“Want to talk some more?”; cout<<“Answer y for yes, n for no.”; char answerLetter; cin>>answerLetter ; if (answerLetter == ‘y’) cout<<“Nice weather we

have”; cout<<“Good-bye”;

}

Page 33: Introduction Structured Programming 256 Chapter 1

© 2000 Scott S Albert

Cout<< will print constant or variable data towhat C++ calls the console. It will be your output screen.

# include <iostream.h>

void main() { cout<<“Hello out there.”;

cout<<“Want to talk some more?”; cout<<“Answer y for yes, n for no.”; char answerLetter; cin>>answerLetter ; if (answerLetter == ‘y’) cout<<“Nice weather we

have”; cout<<“Good-bye”;

}

Page 34: Introduction Structured Programming 256 Chapter 1

© 2000 Scott S Albert

This statement defines or declares a variable (identifier). Eachvariable has a type and a name.

# include <iostream.h>

void main() { cout<<“Hello out there.”;

cout<<“Want to talk some more?”; cout<<“Answer y for yes, n for no.”; char answerLetter; cin>>answerLetter ; if (answerLetter == ‘y’) cout<<“Nice weather we

have”; cout<<“Good-bye”;

}

Page 35: Introduction Structured Programming 256 Chapter 1

© 2000 Scott S Albert

This statement gets input from the keyboard.

# include <iostream.h>

void main() { cout<<“Hello out there.”;

cout<<“Want to talk some more?”; cout<<“Answer y for yes, n for no.”; char answerLetter; cin>>answerLetter ; if (answerLetter == ‘y’) cout<<“Nice weather we

have”; cout<<“Good-bye”;

}

Page 36: Introduction Structured Programming 256 Chapter 1

© 2000 Scott S Albert

The if statement tests conditions. Only if the Boolean expression istrue will the next statement or statement group be executed.

# include <iostream.h>

void main() { cout<<“Hello out there.”;

cout<<“Want to talk some more?”; cout<<“Answer y for yes, n for no.”; char answerLetter; cin>>answerLetter ; if (answerLetter == ‘y’) cout<<“Nice weather we

have”; cout<<“Good-bye”;

}

Page 37: Introduction Structured Programming 256 Chapter 1

© 2000 Scott S Albert

C++Syntax

• Syntax -- grammar required by a language– English syntax: First word of sentence

capitalized. Sentences end in punctuation. etc.

• C++ syntax– Each statement ends in a semi-colon ; – C++ is case sensitive. The variable fooBar is

different from FooBar which is different from FOOBAR and foobar.

Page 38: Introduction Structured Programming 256 Chapter 1

© 2000 Scott S Albert

C++ Syntax -- Reserved words

• Reserved words• C++ has a set of terms which may only be used in

certain contexts. You can never use these terms as variable names or other identifiers. A list is in Appendix 1

• Your compiler will highlight reserved words using a different color

Page 39: Introduction Structured Programming 256 Chapter 1

© 2000 Scott S Albert

C++ Syntax -- Grouping

• Often you wish to treat a set of statements as a block.– Example: you may wish to execute more than

one statement when an if condition is true.

• To group two or more statements in C++– { begins a block– } ends a block

• There are no limits to block nesting.

Page 40: Introduction Structured Programming 256 Chapter 1

© 2000 Scott S Albert

C++ Syntax -- Identifiers

• Identifiers are names of– Variables -- like anyString and answerLetter– Methods -- like main– Classes -- like FirstProgram

• Identifiers must start with a letter, may contain letters or numbers or the _– There is essentially no limit to the length of an

identifier

Page 41: Introduction Structured Programming 256 Chapter 1

© 2000 Scott S Albert

Syntax -- Invoking Methods

– A method is an operation that can be performed by an object (class).

• A stoplight class can change.• TV class can goOn, goOFF, changeChannel etc

– A method is invoked by specifying its name and the object it belongs to as well as any parameters or arguments that it needs

Page 42: Introduction Structured Programming 256 Chapter 1

© 2000 Scott S Albert

The compilation processC++

File(s)

C++Compiler

FirstProgram.cppinclude.h Other.cpp} C++

Linker

Your Program

Page 43: Introduction Structured Programming 256 Chapter 1

© 2000 Scott S Albert

Summary

• Reviewed computing hardware and software basics

• Discussed methodology of going from a problem to a software solution

• Introduction to C++ and our first C++ program