0% found this document useful (0 votes)
121 views

Liang, Introduction To Java Programming, Eighth Edition, (C) 2011 Pearson Education, Inc

Uploaded by

AwadAyoub
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
121 views

Liang, Introduction To Java Programming, Eighth Edition, (C) 2011 Pearson Education, Inc

Uploaded by

AwadAyoub
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc.

What is a Computer ?
- A computer is an electronic device that stores and processes data.

- The computer consists of: Hardware and Software.

- Hardware refers to the physical elements of the computer.

- Major Hardware Components:


1. Central Processing Unit (CPU).
2. Main memory (such as RAM).
3. Secondary storage devices (such as hard disk, CDs, and DVDs)
4. Input devices ( such as keyboard, mouse, and microphone).
5. Output devices (such as monitor, speakers, and printers).

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
What is a Computer Hardware?
A computer consists of a CPU, memory, hard disk, floppy disk,
monitor, printer, and communication devices.

Bus

Storage Communication Input Output


Devices Memory CPU Devices Devices Devices

e.g., Disk, CD, e.g., Modem, e.g., Keyboard, e.g., Monitor,


and Tape and NIC Mouse Printer

3
CPU
Bus

Storage Communication Input Output


Memory CPU Devices Devices Devices
Devices
e.g., Disk, CD, e.g., Modem, e.g., Keyboard, e.g., Monitor,
and Tape and NIC Mouse Printer

• The Central Processing Unit (CPU) is the brain of a computer.


• It retrieves instructions from memory and executes them.
• The CPU speed is measured in megahertz (MHz)
• 1 megahertz equals 1 million pulses per second.

• The speed of the CPU has been improved continuously.


• If you buy a PC now, you can get an Intel Pentium 4 Processor at
3 gigahertz (1 gigahertz is 1000 megahertz).
4
Memory
Bus

Storage Communication Input Output


Memory CPU Devices Devices Devices
Devices
e.g., Disk, CD, e.g., Modem, e.g., Keyboard, e.g., Monitor,
and Tape and NIC Mouse Printer

• Memory is used to store data and program instructions for CPU


to execute.
• Memory is volatile, because information is lost when the power is
off.
• A memory unit is an ordered sequence of bytes, each holds eight
bits.
• A program and its data must be brought to memory before they
can be executed.
5
Storage Devices
Bus

Storage Communication Input Output


Memory CPU Devices Devices Devices
Devices
Hard Disk, CD, e.g., Modem, e.g., Keyboard, e.g., Monitor,
and Tape and NIC Mouse Printer

• Programs and data are permanently stored on storage devices


and are moved to memory when the computer actually uses them.

• There are three main types of storage devices:


• Disk drives (Hard Disks and floppy disks)
• CD drives (CD-R and CD-RW)
• Tape drives
6
Output Devices: Monitor
Bus

Storage Communication Input Output


Devices Memory CPU Devices Devices Devices

e.g., Disk, CD, e.g., Modem, e.g., Keyboard, e.g., Monitor,


and Tape and Mouse Printer
NIC(Network

The monitor displays information (text and graphics).

7
What is a Computer Software?
-Software (Computer Program) provides the instructions and data.
The software controls the hardware and makes it perform specific
tasks.
-Software categorized into:
1. System Software: programs that operate, control and manage the
basic operations of a computer , such as Operating System.
2. Application Software: programs that perform special tasks, such as
MS Office, Adobe Photoshop, payroll .

 Without programs, a computer is an empty machine. Computers do not


understand human languages, so you need to use computer languages to
communicate with them.

 Programs are written using programming languages.


8
Programming Languages
Machine Language Assembly Language High-Level Language

 Machine language is a set of primitive instructions built into


every computer.

 The instructions are in the form of binary code, so you have to


enter binary codes for various instructions.

 The program with native machine language are highly difficult


to read and modify.

 Ex.: to add two numbers, you might write an instruction in binary


like this:1101101010011010

9
Programming Languages
Machine Language Assembly Language High-Level Language

 Assembly language is close to the machine language and


developed to provide better readable code.
 Writing code in assembly language is easier than in machine
language.
 Since the computer cannot understand assembly language,
however, a program called assembler is used to convert assembly
language programs into machine code.
 Ex.: to add two numbers, you might write an instruction in
assembly code like this: ADDF3 R1, R2, R3

10
Programming Languages
Machine Language Assembly Language High-Level Language

 The high-level languages are English-like easy to learn


and program.

 This type of languages provide more readable and


understandable code. (for example: Java, C#, C++, …).

 Ex.: the following is a high-level language statement


that computes the area of a circle with radius 5:
area = 5 * 5 * 3.1415

11
Interpreting/Compiling Source Code

 A program written in a high-level language is called a


source program or source code.

 A computer cannot understand a source program, a


source program must be translated into machine code
for execution.

 The translation can be done using another programming


tool called an interpreter or a compiler.

12
Interpreting Source Code
An interpreter reads one statement from the source code,
translates it to the machine code or virtual machine code,
and then executes it right away.

Note that a statement from the source code may be translated


into several machine instructions.

13
Compiling Source Code
A compiler translates the entire source code into a
machine-code file, and the machine-code file is then
executed.
- In other words, it translates the code from high level
language into machine language.

Syntax error

Source Binary
Compile Run Result
code code
High Level Machine
Language Language

Logical error or runtime error


14
How Do We Write a Program?

 A Computer is not intelligent. It cannot analyze a problem


and come up with a solution.
 A human (the programmer) must analyze the problem,
develop the instructions for solving the problem, and then
have the computer carry out the instructions.
 To write a program for a computer to follow, we must go
through a two-phase process: problem solving and
implementation.

15
Steps in program development
1. Define the problem into three separate components:
 inputs
 outputs
 processing steps to produce required outputs.

2. Outline the solution.


 Decompose the problem to smaller steps.
 Establish a solution outline.

3. Develop the outline into an algorithm.


 The solution outline is now expanded into an algorithm.
4. Test the algorithm for correctness.
 Very important in the development of a program, but often forgotten
 Major logic errors can be detected and corrected at an early stage.

5. Code the algorithm into a specific programming language.


Steps in Program Development

6. Run the program on the computer.


 This step uses a program compiler and programmer-
designed test data to machine-test the code for
 syntax errors
 logic errors

7. Document and maintain the program.

17
Algorithms & Flowcharts
 What is an algorithm?
 Lists the steps involved in accomplishing a task
(like a recipe)
 An algorithm must:
 Be clear, precise and unambiguous

 Give the correct solution in all cases

 Eventually end

What is pseudo code?


English (formalized and abbreviated to look
like high-level computer language)
18
Pseudocode & Algorithm
 Example 1: Write an algorithm that will read the two
sides of a rectangle and calculate its area.

 Input the width (W) and Length (L) of a rectangle


 Calculate the area (A) by multiplying L with W
 Print A

19
 Write an algorithm to compute the average of three
marks.

 Input : 3 marks M1,M2,M3


 Calculate the average using equation :
Average=(sum of marks /3)

 Output :Print the average


Flowchart
 A graphical representation of the sequence of
operations in an information system or program.

 Program flowcharts show the sequence of


instructions in a single program or subroutine.
 shows logic of an algorithm
 emphasizes individual steps and their interconnections
 e.g. control flow from one action to the next
Note: Different symbols are used to draw each type of
flowchart.

21
Flowchart Symbols
Name Symbol Use in Flowchart

Oval Denotes the beginning or end of the program

Parallelogram Denotes an input and output operation

Rectangle Denotes a process to be carried out


ex . addition, subtraction, division etc .

Diamond Denotes a decision or branch) to be made.


The program should continue along one of
two routes . (e.g IF/THEN/ELSE)

connector

Flow line Denotes the direction of logic flow in the program


22
Example 1
 Write an algorithm and draw a flowchart to convert
the length in feet to centimeter.
Pseudocode:
 Input the length in feet (Lft)
 Calculate the length in cm (Lcm) by multiplying LFT
with 30
 Print length in cm (LCM)

23
Example 1
Flowchart
Algorithm
 Step 1: Input Lft START

 Step 2: Lcm  Lft x 30 Input


Lft
 Step 3: Print Lcm
Lcm  Lft x 30

print
Lcm

STOP

24
Example 2
Write an algorithm and draw a flowchart that will read the
two sides of a rectangle and calculate its area.

Pseudocode
 Input the width (W) and Length (L) of a rectangle
 Calculate the area (A) by multiplying L with W
 Print A

25
Example 2
Algorithm Flowchart
START
 Step 1: Input W,L
 Step 2: AL x W Input
W, L
 Step 3: Print A

ALxW

Print
A

STOP
26
Example 3
 Write an algorithm and draw a flowchart that
will calculate the roots of a quadratic equation
ax 2  bx  c  0
 Hint: d = sqrt ( b 2  4ac ), and the roots are:
x1 = (–b + d)/2a and x2 = (–b – d)/2a

27
Example 3
Pseudocode:
 Input the coefficients (a, b, c) of the quadratic equation
 Calculate d
 Calculate x1
 Calculate x2
 Print x1 and x2

28
Example 3 START

Input
 Algorithm: a, b, c

 Step 1: Input a, b, c
d  sqrt(b x b – 4 x a x c)
 Step 2: d  sqrt ( b  b  4  a  c )
 Step 3: x1  (–b + d) / (2 x a) x1 (–b + d) / (2 x a)
 Step 4: x2  (–b – d) / (2 x a)
 Step 5: Print x1, x2 X2  (–b – d) / (2 x a)

Print
x1,x2

STOP

29
PROBLEM SOLVING
WITH DECISIONS

Chapter 6
The decision logic structure
 Uses the If/Then/Else instruction .
 It tells the computer that If a condition is true , then
execute a set of instructions, or Else execute another
set of instructions.
 The else part is optional
 As there is not always a set of instruction if the
condition are false .
Common Forms of decision structure
If <condition (s)>•
Then•
T <True instructions>•
Else•
F
<False instructions>•

A condition can be one of four things :


Logical expression: (AND, OR NOT)•
relational expression(>, <=, <, >=,…..)•
Variable logic data type.•
Combination of Relational , logic and mathematical operators.•
Note: the condition part can combine more than one condition using the •
logical operators.
Single condition
one possible action or set of action
write an algorithm and corresponding flowchart that
read a student grade on a test which is out of 10, then
alerts the student if his grade is under 4.
Algorithm Flowchart

StudentAlert()
1.Start
2.Enter grade
3.If grade < 4
1. Print “Alert: You must study hard” start
4.end

Enter grade

F T
If grade < 4

Print “Alert: You


must study hard”

End
Single Condition
one Possible Actions or Sets of Actions

 assume you are calculating pay at an hourly rate , and


overtime pay ( over 40 hours ) at 1.5 times the hourly
rate .
Single Condition
Two Possible Actions or Sets of Actions

start
Data is put into
HOURS and RATE

IF
HOURS > 40

PAY= RATE *
PAY = RATE * (40 + 1.5 *
HOURS (HOURS – 40))

end
Looping : Repeated Execution
• Loop: Group of instructions that are executed repeatedly
while some condition remains true.

Lecture #01: © DSamanta CS 10001 : Programming and Data Structures 37


Counter Controlled Loop

Read 5 integers and


display the value of
their summation.

Lecture #01: © DSamanta CS 10001 : Programming and Data Structures 38


Nested If/Then/Else Instructions
Simple Java Program

statement(s);

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
MODIFIED BY: Yousef Al-Raba'nah
Simple Java Program
- Syntax is a set of rules that must be strictly followed to write
computer-understandable instructions.
- Class: is a template (framework) for defining data (attributes),
methods and theirs properties.
- The name of previous class is Welcome, it is defined immediately
after the reserved words public class.
- Java source programs are case sensitive (Uppercase and Lowercase
alphabets). For example, Area differs than aRea and area.
- A pair of curly braces { } in a program forms a block that groups the
program's components.
- A block: is a construct that groups of programming statements.

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
MODIFIED BY: Yousef Al-Raba'nah
Simple Java Program
- In Java, each block begins with an opening brace { and ends with a
closing brace }.
- Every class has a class block that groups the data and methods of
the class. Similarly, every method has a method block that groups
the statements in the method. Blocks can be nested, meaning that
one block can be placed within another.
- The previous program contains two blocks ( The class block and the
main method block). The main method block is nested with the
class block.

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Simple Java Program
- A Java program is executed from the main method in the class. A
class may contain several methods.
- The main method is the entry point where the program begins
execution.
- The main method's header is always the same.
- A method is a construct that contains or groups statements that
perform an operation.

Q: Write a program called Welcome, that display “Welcome to Java”.

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy