48
Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

Embed Size (px)

Citation preview

Page 1: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

Chapter 4Programming Concepts

Dept. of Computer Engineering

Khon Kaen University

Page 2: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

Major Concepts Computer Software Processing a high-level language

program The software development method Applying the software

development method Professional ethics for computer

programmers

Page 3: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

Computer Software Two major categories of software

System software: operating systems

Example: Windows, Apple OS X, Unix Application software

Example: Microsoft PowerPoint, Microsoft Word Processing

Page 4: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

Operating Systems Communicating with the user Managing allocation of resources Collecting input from input devices Conveying program output to output

devices Accessing data from secondary

storage Writing data to secondary storage

Page 5: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

Some Widely Used OSs Command-Line Interface

Unix MS-DOS Linux

Graphical User Interface Mac OS X Windows 9x, NT, 2000, Me, XP UNIX + X Windows System Linux + X Windows System

Page 6: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

OSs: Command-Line Interface

This OS displays a brief message called prompt that indicates readiness to receive input.

The user then types a command at the keyboard

Then, the OS finds the executable program that corresponds to the command and then invoke the program

Page 7: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

MS-DOS Command Interface

C:\>dir temp/w Volume in drive C has no label. Volume Serial Number is 8835-FDA8 Directory of C:\temp[.] [..][C#] [DirectX9][License]

SSHSecureShellClient-3.2.2.exe 1 File(s) 6,070,272 bytes 5 Dir(s) 28,122,365,952 bytes free

Page 8: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

UNIX Command Interfacegear.kku.ac.th:/home/

krunapon/public_html %ls

168493 Kanda.htm index.html srProjs03.zip DotNET

index-en.html pubs

Page 9: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

OSs: Graphical User Interface (GUI)

The GUI interface provides the user with a system of icons and menus

To issue commands Users move the mouse cursor to

point to the appropriate icon or menu selection

Users then click a button once or twice

Page 10: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

OSs: GUI (Cont.) Icons are pictures that represent

computer resources and files, such as printers, documents, and programs

You double-click an icon to choose (activate, launch) a program

The Windows OS offers two unique tools Taskbar Start button

Page 11: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

Icons Window control buttons

Scroll box

Scroll bar

DesktopTask barStart menuStart button

Dialog box

Program running in a window

Page 12: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

Application Software A program used for a specific task such

as word processing, accounting, or database management

Application software tells the computer how to accomplish tasks the user requires, such as creating a document

Programmers use programming languages to write most application software

Page 13: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

Programming Languages Commands or lists of instructions for

a computer to execute Types of programming languages

Machine language: binary numbers directly understood by a computer

Assembly language: in the form of mnemonic codes and variable names.

converted to machine codes by the assembler and linker

Page 14: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

Programming Languages (Cont.)

Types of programming (Cont.) High-level language

A programming language that combines algebraic expressions and English words

Converted to machine codes by a compiler or an interpreter or a compiler

Examples: BASIC, C, C++, COBOL, FORTRAN, Java, Lisp, Pascal

Page 15: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

Assembler & Linker

Page 16: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

Machine and Assembly Languages

Memory Address

Machine Language Instructions

Assembly Language Instructions

00000000 00010101 RTV A

00000001 00010110 ADD B

00000010 00110101 STO A

00000011 01110111 HLT

A = A + B

Page 17: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

Object-Oriented Programming (OOP)

Programs are organized as cooperative collections of objects

What is an Object? An object is a software bundle of related

properties Software objects are used to model real-

world objects you find in everyday life Objects communicate with one another

by exchanging messages

Page 18: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

OOP (Cont.) Objects that share common properties

can be grouped into classes Classes can be organized into a

hierarchy of subclasses and superclasses

A class inherits state and behavior from its superclass.

OOP languages C++, Java, C#

Page 19: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

Classes and Objects

Class Automobile

Object1 Object2

Color Bronze Silver

Maker Toyota Honda

Model Vios City

Year 2003 2002

Page 20: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

A Class Hierarchy

Vehicle has: driver’s seat four wheels

Truck has:driver’s seatfour wheelsTruck bed

Automobile has:driver’s seatfour wheelspassengers’ seats

Sports car has:driver’s seat

four wheels rear spoiler

Page 21: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

Processing a High-Level Language Programming

A source file is a file program written in a high-level language

Syntax is the grammar rules when writing a source file

Compiler translates the high-level language program (a source file) to file containing the machine language

An object file is an output of the compiler.

Page 22: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University
Page 23: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

Executing a Program To execute a program, the CPU

Must load each program instruction in memory

Send out the command signals required to carry out the instruction

The program can have both input data and program output

Page 24: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

Input Data Input data is the data that is the

input of the program During execution, data can be

entered into memory and manipulated in some specific way.

Special program instructions are used for accepting the input data from the users and this input data then is loaded into memory

Page 25: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

Program Output After input data has been processed,

Special program instructions for displaying or printing values in memory can be executed to

Display the program results on a computer monitor

Save the program results in a file

Page 26: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

An Execution of a Program

Page 27: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

Major Concepts Computer Software Processing a high-level language

program The software development method Applying the software

development method Professional ethics for computer

programmers

Page 28: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

Software Development Method

1. Specify the problem 2. Analyze the problem 3. Design the algorithm to solve the

problem 4. Implement the algorithm 5. Test and verify the completed

program 6. Maintain and update the program

Page 29: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

SW Development (Cont.) 1. Specifying the problem requirement

State the problem clearly and unambiguously

Have a clear understanding of what is required for its solution

2. Analyzing the problem Identifying the problem inputs and outputs Identifying any additional requirements or

constraints on the solution Develop a list of problem variables and

their relationships

Page 30: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

SW Development (Cont.) Daily problem: I want to know the total

cost of 5 kilograms of mangoes that are 30 baht per kilogram

Step 1. Specify the problem requirement Translate the daily problem into the

computer programming problemCompute and display the total cost of mangoes given the number of kilograms of mangoes purchased and the cost per kilogram of mangoes.

Page 31: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

SW Development (Cont.) Step 2. Analyze the problem

Underline phases in the problem statement that identify the inputs and output

Compute and display the total cost of mangoes given the number of kilograms of mangoes purchased and the cost per kilogram of mangoes.

Page 32: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

SW Development (Cont.) Step 2. Analyzing the problem

Problem Inputs Quantity of mangoes purchases (in

kilograms) Cost per kilogram of mangoes (in

Baht per kilogram)Problem Outputs Total cost of mangoes (in Baht)

Page 33: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

SW Development (Cont.) Step 2. Analyzing the

problem Develop a list of formulas that specify relationships between the inputs and the outputs

Total costs of mangoes = Cost per kilogram x the number of kilograms of mangoes

Page 34: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

SW Development (Cont.) Step 2. Analyzing the problem

Sometimes we may need to make certain assumptions or simplifications to derive these relationships

The process of modeling a problem by extracting the essential variables and their relationships is called abstraction

Page 35: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

SW Development (Cont.) Step 3. Designing the

algorithm to solve the problem Develop a list of steps called algorithm to solve the problem

An algorithm is usually designed based on the divide and conquer approach or the top-down design approach

Page 36: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

Divide and Conquer Divide the problem into the major steps

or sub-problems that need to be solved Conquer each sub-problem and then

combine the solutions of sub-problems to form the solution of the original problem. This process is called algorithm refinement

Sub-problems are further divided until they are easily solved by program code

Page 37: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

SW Development (Cont.) Step 3. Designing the algorithm Algorithm of a Sample Problem

1. Get the input data 2. Perform the computations 3. Display results

Then verify that the algorithm solves the problem as intended by desk-checking Carefully perform each step (or its

refinement) just as a computer would and verify that the algorithm works as expected

Page 38: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

SW Development (Cont.) Step 4. Implementing the algorithm

Write the algorithm as a program coding Convert each algorithm step into one or

more statements in a programming language

Step 5. Test and verify the completed program Compile and run the program Test the program with different data sets Ensure that the program works correctly for

the given problem specification

Page 39: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

SW Development (Cont.) Step 6. Maintain and update the program

Modify a program to remove previously undetected errors

Keep the program up-to-date as government regulations or company policies change

Coding Hints Make very small additions/changes and test

the partly finished code Don’t write a complete program, and then

expect it to work first time

Page 40: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

Applying the SW Development

Case study: Converting miles to kilometers

Problem:Your summer survey job requires you to

study some maps that give distances in kilometers and some that use miles. You and your coworkers prefer to deal in measurements.

Instruction:Write a program that performs the

necessary conversation

Page 41: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

Problem Analysis Since you prefer to deal in metric

measurements so you must convert distance measurements in miles to kilometers

Therefore, the problem input is distance in miles and the problem output is distance in kilometers

Page 42: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

Problem Analysis (Cont.)

To write the program, you need to know the relationship between miles and kilometers.

One mile is equal to 1.609 kilometers

Page 43: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

Problem Analysis (Cont.)

Data requirements Problem input

Miles the distance in miles Problem output

Kms the distance in kilometers

Relevant formula 1 mile = 1.609 kilometers

Page 44: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

Algorithm Design Steps in the algorithm

1. Get the distance in miles 2. Convert the distance to kilometers 3. Display the distance in kilometers

Now decide whether any step of the algorithm needs further refinement

Page 45: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

Algorithm Design (Cont.) Algorithm refinement

The distance in kilometers is 1.609 times the distance in miles

Algorithm with refinements Step 1. Get the distance in miles Step 2. Convert the distance in kilometers:

2.1 The distance in kilometers is 1.609 times the distance in miles

Step 3. Display the distance in kilometers

Page 46: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

Algorithm Design (Cont.) Desk-checking the algorithm

If step 1 gets a distance of 10.0 miles

Step 2.1 would convert it to 1.609x10.00 = 16.09 kilometers

And then step 3 should display number “16.09” as a program output

Page 47: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

Implementation// Miles.cpp // Converts distance in miles to kilometers. #include <iostream> // class for stream input/outputusing namespace std; // use the standard namespaceint main() // start of main function{ const float km_per_mile = 1.609; // 1.609 km in a mile float miles, // input: distance in miles kms; // output: distance in kilometers // Get the distance in miles. cout << "Enter the distance in miles: "; cin >> miles; // Convert the distance to kilometers. kms = km_per_mile * miles; // Display the distance in kilometers. cout << "The distance in kilometers is " << kms << endl; return 0; }Enter the distance in miles: 10.0 The distance in kilometers is 16.09

Page 48: Chapter 4 Programming Concepts Dept. of Computer Engineering Khon Kaen University

Professional Ethics for Computer Programmers

Computer programmers and software engineers should follow certain standards of professional conduct: Privacy and misuse of data Computer hacking Plagiarism and software piracy Misuse of a computer resource