45
BBS514 Structured Programming (with Java) 1 BBS 514 Structured Programming (with Java)

BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

BBS514 Structured Programming (with Java) 1

BBS 514

Structured Programming

(with Java)

Page 2: BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

BBS514 Structured Programming (with Java) 2

• Computers and Programs

• Algorithms

Page 3: BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

• A computer is a device capable of performing computations and making logical

decisions at very fast speeds.

• Computers are tools that can be programmed to perform many functions, such as:

– Spreadsheets, databases, word processing, games, etc.

• Computers are versatile because they can be programmed.

• Computer programmers implement programs that perform these functions.

• They are widely used in many areas of society.

– banking, scientific research, schools, airplanes, space program, etc.

Computers

BBS514 Structured Programming (with Java) 3

Page 4: BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

• Abacus

– performed mathematical computations

– Can do square roots and cube roots

• Babbage Difference Engine (1822)

The First Computers

BBS514 Structured Programming (with Java) 4

Page 5: BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

• ENIAC (1946)

– built in 1946 at University of Pennsylvania

– used to compute ballistic tables

– it was very huge

The First Electronic Digital Computer

BBS514 Structured Programming (with Java) 5

Page 6: BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

• Since from the first computer, computers are getting smaller, but they are getting much

powerful.

ENIAC then …

small chips today …

(more powerful than ENIAC)

Computer Size

BBS514 Structured Programming (with Java) 6

Page 7: BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

Hardware

• Computer hardware components are the physical pieces of the computer.

– the physical, tangible parts of a computer

– keyboard, monitor, disks, wires, chips, etc.

Software

• The set of instructions controls computers are known as computer programs

(software).

– programs and data

– a program is a series of instructions

• A computer system requires both hardware and software

– Each is essentially useless without the other.

Computer Systems

Hardware & Software

BBS514 Structured Programming (with Java) 7

Page 8: BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

• The major hardware

components of a computer are:

– The central processing unit

(CPU)

– Main memory

– Secondary storage devices

– Input and Output devices

Computer Systems: Hardware

BBS514 Structured Programming (with Java) 8

Page 9: BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

Central Processing Unit (CPU)

BBS514 Structured Programming (with Java) 9

Arithmetic

Logic

Unit

Control

Unit

CPU

Instruction (input) Result (output)

CPU (Central Processing Unit)

• Process and manipulate information stored in

memory.

• It can be divided into two units: CU (Control Unit)

and ALU (Arithmetic Logic Unit)• CU coordinates activities of the computer and

controls other devices of computer.

• ALU processes arithmetic and logical instructions.

Page 10: BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

• The CPU performs the fetch, decode, execute cycle in order to process program

information.

Central Processing Unit (CPU)

BBS514 Structured Programming (with Java) 10

Fetch

The CPU’s control unit fetches, from main memory, the

next instruction in the sequence of program instructions.

Decode

The instruction is encoded in the form of a

number. The control unit decodes the

instruction and generates an electronic signal.

ExecuteThe signal is routed to the appropriate

component of the computer (such as the ALU,

a disk drive, or some other device). The signal

causes the component to perform an operation.

Page 11: BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

• The speed of a CPU is controlled by the system clock

• The system clock generates an electronic pulse at regular intervals

• The pulses coordinate the activities of the CPU

• The speed is usually measured in gigahertz (GHz)

Central Processing Unit (CPU)

BBS514 Structured Programming (with Java) 11

Page 12: BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

Memory

• Store information (data + instructions)

• A sequence of memory cells.

– a byte is 8 bits

– a bit is smallest unit (0 or 1)

• Store, retrieve, update

– changing pattern of 0 and 1s in memory cells

– copying these patterns into some internal registers

• Stored information in memory is volatile.

– when the computer is turned off, the contents of RAM are erased.

• Size of a memory of a typical PC system: 2GB, 4GB, 8GB, 16GB

– KB, MG, GB, TB

Main Memory (RAM)

BBS514 Structured Programming (with Java) 12

Page 13: BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

CPU and Main Memory

BBS514 Structured Programming (with Java) 13

Central

Processing

Unit

Main

Memory

Chip that executes

program commands

Intel Pentium

Primary storage area

for programs and data

that are in active use

Synonymous with

RAM

Page 14: BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

• A bit is a digit with a value of either 0 or 1.

• A byte consists of 8 bits.

– 01001010

– As there are 8 bits per byte, each byte can hold 28=256 values

– 01001010 = 74

– All computing measurements are in terms of bytes

• RAM is divided into bytes. Each byte in main memory resides at a numbered location

called its address.

Bits, Bytes, and Addresses

BBS514 Structured Programming (with Java) 14

1 1 1 1

0 0 0 0

• A bit is either on or off:

1 = on

0 = off

• The bits form a pattern that

represents a character or a

number.

Page 15: BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

Main Memory

BBS514 Structured Programming (with Java) 15

Main memory is divided

into many memory

locations (or cells)

9278

9279

9280

9281

9282

9283

9284

9285

9286

Each memory cell has a

numeric address, which

uniquely identifies it

Page 16: BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

Storing Information in Memory

BBS514 Structured Programming (with Java) 16

9278

9279

9280

9281

9282

9283

9284

9285

9286

Large values are

stored in consecutive

memory locations

10011010

Each memory cell stores a

set number of bits (usually

8 bits, or one byte)

Page 17: BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

• Data of all kinds (numbers, letters, strings of characters, audio, video, even programs)

are encoded and stored using 1s and 0s.

• When more than a single byte is needed, several adjacent bytes are used.

– The address of the first byte is the address of the unit of bytes.

Storing Data

BBS514 Structured Programming (with Java) 17

Page 18: BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

• Every memory device has a storage capacity, indicating the number of bytes it can

hold

• Capacities are expressed in various units:

Storage Capacity

BBS514 Structured Programming (with Java) 18

KB 210

= 1024

MB 220

(over 1 million)

GB 230

(over 1 billion)

TB 240

(over 1 trillion)

Unit Symbol Number of Bytes

kilobyte

megabyte

gigabyte

terabyte

Page 19: BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

Secondary Storage

• Computers have limited main memory and information stored in main memory is

volatile. i.e. when a computer is switched off, information in its main memory

disappears.

• There are additional data storage unit, called secondary storage units.

• Data stored in these secondary storage units are permanent, i.e. data does not disappear

when you switch off the computer.

• Some secondary storage units:

– Floppy Disks

– Hard Disks

– Tape Drive

– Optic Disk (CD Drive, DVD Drive)

– Flash Disk (USB Disks)

Secondary Storage

BBS514 Structured Programming (with Java) 19

Page 20: BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

Secondary Memory Devices

BBS514 Structured Programming (with Java) 20

Central

Processing

Unit

Main

Memory

USB Disk

Hard Disk

Secondary memory

devices provide

long-term storage

Information is moved

between main memory

and secondary memory

as needed

Hard disks

Floppy disks

ZIP disks

Writable CDs

Writable DVDs

Tapes

Page 21: BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

• Input is any data the computer collects from the outside world.

• That data comes from devices known as input devices.

• Common input devices:

– Keyboard

– Mouse

– Scanner

– Digital camera

Input Devices

BBS514 Structured Programming (with Java) 21

Page 22: BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

• Output is any data the computer sends to the outside world.

• That data is displayed on devices known as output devices

• Common output devices:

– Monitors

– Printers

• Some devices such as disk drives perform input and output and are called I/O devices

(input/output).

Output Devices

BBS514 Structured Programming (with Java) 22

Page 23: BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

Input / Output Devices

BBS514 Structured Programming (with Java) 23

Central

Processing

Unit

Main

Memory

USB Disk

Hard Disk

Monitor

Keyboard

I/O devices facilitate

user interaction

Monitor screen

Keyboard

Mouse

Joystick

Bar code scanner

Touch screen

Page 24: BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

• Consider the following specification for a personal computer:

– 3.2 GHz Intel Core i7

– 16 GB RAM

– 1 TB Hard Disk

– 256 GB Solid State Disk

– 22” Video Display with 1366 x 768 resolution

• What does it all mean?

A Computer Specification

BBS514 Structured Programming (with Java) 24

Page 25: BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

• Software refers to the programs that run on a computer.

• Two classifications of software:

– Operating Systems

– Application Software

SOFTWARE

BBS514 Structured Programming (with Java) 25

Page 26: BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

• An operating system is a set of programs that manages the computer’s hardware

devices and controls their processes.

• Provides the user interface to the computer.

• Manages resources such as CPU and memory

• Most all modern operating systems are multitasking.

• Widows 10, Unix, Linux, Mac OS, …

• A multitasking operating system is capable of running multiple programs at once.

– The technique is called time sharing.

– A multitasking system divides the allocation of hardware resources and the attention of the CPU among all the executing programs

• Most operating systems and application programs have a graphical user interface (GUI)

SOFTWAREOperating Systems

BBS514 Structured Programming (with Java) 26

Page 27: BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

• Application software refers to programs that make the computer useful to the user.

• Application software provides a more specialized type of environment for the user to work in.

• Common application software:

– Spreadsheets

– Word processors

– Accounting software

– Tax software

– Games

– Your Java programs

SOFTWAREApplication Software

BBS514 Structured Programming (with Java) 27

Page 28: BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

• File contains a document and Directory contains files or other directories.

• You will be able to create files and directories

• a file can contain different types of information, and normally the extension of that file

reflects the content of that file.

– .docx : a word document

– .c : A C program file

– .exe : an executable file

– .java : a java program

– .class : a class file

– .html : a hypertext file used to represent a web page

File/Directory

BBS514 Structured Programming (with Java) 28

Page 29: BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

• A network is two or more computers that are connected so that data and resources can

be shared

• Most computers are connected to some kind of network

• Each computer has its own network address, which uniquely identifies it among the

others

• A file server is a network computer dedicated to storing programs and data that are

shared among network users

Networks

BBS514 Structured Programming (with Java) 29

Page 30: BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

Local-Area Networks

BBS514 Structured Programming (with Java) 30

LAN

A Local-Area Network

(LAN) covers a small

distance and a small

number of computers

A LAN often connects the machines

in a single room or building

Page 31: BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

Wide-Area Networks

BBS514 Structured Programming (with Java) 31

LAN

A Wide-Area Network (WAN)

connects two or more LANs,

often over long distances

A LAN usually is owned

by one organization, but

a WAN often connects

groups in different

countries

LAN

Page 32: BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

• The Internet is a WAN which spans the entire planet

– The word Internet comes from the term internetworking

– It started as a United States government project, sponsored by the Advanced Research

Projects Agency (ARPA) - originally it was called the ARPANET

TCP/IP

– A protocol is a set of rules that determine how things communicate with each other

– The software which manages Internet communication follows a suite of protocols called

TCP/IP

– The Internet Protocol (IP) determines the format of the information as it is transferred

– The Transmission Control Protocol (TCP) dictates how messages are reassembled and

handles lost information

The Internet

BBS514 Structured Programming (with Java) 32

Page 33: BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

• The set of instructions controls computers are known as computer programs

(software).

• A software is a series of instructions

• In this course, we will how to write computer programs using Java programming

language.

• Before, we start to learn Java programming language we will talk about: Algorithms

• ALGORITHMS: a sequence of instructions to solve a problem

Software

BBS514 Structured Programming (with Java) 33

Page 34: BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

BBS514 Structured Programming (with Java) 34

Algorithms

Page 35: BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

From problem to program

BBS514 Structured Programming (with Java) 35

• In “real world”…• Problem in Natural language• Top Down Design in pseudo-code• Program in computer code

(code in almost any computer language.)

Requirements:

A program that does this and that and theother. It must take data fromthis source and

IMPLEMENTReduce cognitive load• separating design and coding• using top-down design

The Problem

Do this

repeat

if something then

do this

else

do the other

Print “all done”

DESIGN

The Design

(Algorithm)

The Program

Page 36: BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

1. Problem Understanding

– Read the problem carefully and try to understand what is required for its solution.

2. Analysis

– Identify problem inputs and outputs.

3. Design

– Develop a list of steps (algorithm) to solve the problem

– Refine steps of this algorithm. (Divide and Conquer)

– Verify that the algorithm solves the problem, i.e. the algorithm is correct.

4. Implementation

– Implement the algorithm as a (java) program.

– You have to know a specific programming language (java)

– Convert steps of the algorithm into programming language statements.

5. Testing and Verification

– Test the completed program, and verify that it works as expected.

– Use different test cases (not one) including critical test cases.

Steps of Software Development

BBS514 Structured Programming (with Java) 36

Page 37: BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

• An algorithm is a design written in pseudo code. (in English, but a formal style)

• Algorithms contains:

– Sequence

– Decision

– Repetition

Algorithm

BBS514 Structured Programming (with Java) 37

Page 38: BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

Everyday Algorithms

BBS514 Structured Programming (with Java) 38

Problem: To drink a glass of milk.

Algorithm:

1. Enter the kitchen

2. Get a glass

3. Get the milk from the refrigerator

4. Fill the glass with milk

5. Drink it

Refine step 1:

1. Walk to the kitchen door

2. If the door is close then

2.T. Open it

3. Walk into the kitchen

Refine step 3:

1. Open the refrigerator

2. Get the milk

3. Close the refrigerator

Refine step 4:

1. while the glass is not full

1.1. Pour some milk

into the glass

Page 39: BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

Area-Circumference Problem

BBS514 Structured Programming (with Java) 39

Requirements:

Design an algorithm to find and report the area and

circumference of a circle whose radius the user gives.

Welcome to circle computer…

Please enter the radius: 5

The area of a circle of radius 5 is 78.55

and its circumference is 31.42

Example interaction

Page 40: BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

Area-Circumference Problem

BBS514 Structured Programming (with Java) 40

To find area & circumference of circle…

1. Print welcome message

2. Ask for & get radius from user

3. Compute area as pi.radius.radius

4. Compute circumference as 2.pi.radius

5. Report area, circumference & radius

Identify preconditions & ensure they are satisfied.

Once we are sure that this is correct, move on to solve any non-

trivial sub-problems.

Page 41: BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

Area-Circumference Problem (cont.)

BBS514 Structured Programming (with Java) 41

Solve…

2. Ask for & get radius from user

1. Ask (prompt) the user to

enter radius

2. Get radius value from user

Solve…

5. Report area, circumference & radius

1. Print msg “The area of a circle with

radius ”

2. Print radius value

3. Print msg “ is ”

4. Print area value and move to next

line

5. Print msg “ and its circumference

is ”

6. Print circumference value

7. Print blank line

Page 42: BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

Problem: Calculating the average score of an exam

Inputs : Scores of an exam

Output: Average score of the exam

Algorithm:

1. Set count to zero

2. Set sum to zero

3. while there is an exam sheet do

3.1. Read the score on the sheet

3.2. Add the score on the sheet to sum

3.3. Add one to count

3.4. Move to the next sheet

4. Compute average as sum/count

5. Display average

Calculating the average score of an exam

BBS514 Structured Programming (with Java) 42

Page 43: BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

Prime Number

BBS514 Structured Programming (with Java) 43

Problem: Deciding that a given integer number is prime or not.

Input : An integer number N

Output: A sentence indicating that N is prime or not.

Algorithm:

1. Read the number N

2. Test whether N is prime or not

3. Display the result.

Refine step 2:

1. set counter to 2

2. set PrimeFlag to true

3. while (counter < N and PrimeFlag = true) do

3.1. if N is divisible by counter then

3.1.T. set PrimeFlag to false

3.2. Increment counter by 1

Refine step 3:

1. if PrimeFlag = true then

1.T. Display that N is prime

else

1.F. Display that N is not prime

Page 44: BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

Factorial

BBS514 Structured Programming (with Java) 44

Problem: Calculating N!

Inputs: A positive integer N

Output: N!

Algorithm:

1. Read a positive integer N

2. Calculate NFactorial

3. Display NFactorial

Refine step 2:

1. Set NFactorial to 1

2. Set counter to 2

3. while (counter <= N) do

3.1. Multiply NFactorial by counter

3.2. Increment counter by 1

Page 45: BBS 514 Structured Programming - Hacettepe Üniversitesiilyas/Courses/BBS514/lec01... · BBS 514 Structured Programming ... • Computer programmers implement programs that perform

Types & Layout (of algorithm steps)

BBS514 Structured Programming (with Java) 45

n. dostep

while condition

indent

1. step 2. step3. step4. step

Sequence

n. if condition thennT step

elsenF step

indent

Decision

n. while condition dostep

indent

Repetition

Any step can be replaced with one of the other

types

n. for so many times dostep