Liang, Introduction To Java Programming, Eighth Edition, (C) 2011 Pearson Education, Inc
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.
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
3
CPU
Bus
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 .
9
Programming Languages
Machine Language Assembly Language High-Level Language
10
Programming Languages
Machine Language Assembly Language High-Level Language
11
Interpreting/Compiling Source Code
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.
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
15
Steps in program development
1. Define the problem into three separate components:
inputs
outputs
processing steps to produce required outputs.
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
Eventually end
19
Write an algorithm to compute the average of three
marks.
21
Flowchart Symbols
Name Symbol Use in Flowchart
connector
23
Example 1
Flowchart
Algorithm
Step 1: Input Lft START
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: AL x W Input
W, L
Step 3: Print A
ALxW
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>•
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
End
Single Condition
one 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.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807