You are on page 1of 39

COMP6598

Introduction to Programming

Introduction to Algorithm and Java Programming


Week 1
Session 2
Acknowledgement
These slides have been adapted from
Introduction to Java Programming. 9ed. D.
Liang. 2013. Chapter 1
Learning Outcomes
• LO 1: Design and apply the right algorithms
to solve problem
Lecture Outline :
• Algorithm Definition
• Program and Programming Languages
• Pseudocode
• Flowchart
• NS Diagram
• Introduction of IDE and Java
• Create, Compile and Execute
• Exercise (Discussion forum 1)
ALGORITHM DEFINITION
Definition of Algorithm
• What is an algorithm?
– A set of detailed, unambiguous and ordered
instructions developed to describe the progress
necessary to produce the desired output from a
given input.
Algorithm Example
• Sorting. This algorithm is used to arrange of items or number according
of its size.
– Input : a set of positive numbers (a1, a2, a3, …, an)
e.g. 5, 3, 4, 2, 1
– Output : a set of arranged numbers (a’1, a’2, a’3, …, a’n)
e.g. 1, 2, 3, 4, 5

• Example of problems which solved using an Algorithm.


– Human Genome Project to identify 100.000 of human DNA genes.
– Search Engines (Yahoo, Google, etc)
– GPS Tracking System.
– E-Banking
Characteristics of Algorithm
An algorithm should have the following five
characteristics :
• Input
• Output
• Definiteness
• Finiteness
• Effectiveness
PROGRAM AND PROGRAMMING
LANGUAGE
Programming Language
• Computer programs, known as software, are
instructions that tell a computer what to do.
• Computers do not understand human languages,
so programs must be written in a language a
computer can use .
• Programming Language
– Machine Language
– Assembly Language
– High Level Language
Programming Language
• High-level languages are English-like and easy to learn and use. The
instructions in a high-level programming language are called statements.
• Popular high-level programming language:
– ADA
– Basic
–C
– C++
– C#
– COBOL
– FORTRAN
– JAVA
– Pascal
– Phyton
– Visual Basic
Expressing an Algorithm
Expressing an Algorithm can be expressed using a
method below :
– Pseudocode
– Flowchart
– NS Diagram
PSEUDOCODE
Pseudocode
Pseudocode (pronounced SOO-doh-kohd) is a
detailed yet readable description of what a
computer program or algorithm must do, expressed
in a formally-styled natural language rather than in
a programming language
Some Keywords That Should
be Used
• Input (a computer can receive information)
Read, Get, Input, Baca, or Key-In
example :
Read student_name
Get account_number
• Output (a computer can put out information)
Print, Write, Put, Output, or Display
example :
Print “Algorithm and Programming”
Output student_name
Some Keywords That Should
be Used
• Compute (a computer can perform arithmetic)
Compute, Calculate, Add, Sum, Multiply, Increment, Substract, Divide
Example :
add 1 to total_student
total_student = total_student+1
• Initialize or Store Value to An Identifier (a computer
can assign a value to a variable or memory location)
Set, Initialize, Store, Save, =
example :
set total to zero
total = price - discount
Some Keywords That Should
be Used
• Looping (a computer can repeat a group of actions)
DOWHILE and ENDDO, REPEAT and UNTIL, WHILE, DO..ENDDO
example
DOWHILE Item < 10
Total = Total + Price
add 1 to item
ENDDO

• Selection (a computer can compare two variables and select


one alternatives actions)
IF and ENDIF, CASE and ENDCASE
example :
IF (Number%2) == 0 THEN
Print Number,” is even ”
ELSE
Print Number, “”is odd”
ENDIF
Pseudocode
Example of Pseudocode to determine odd-even number

BEGIN
Number = Input Number
Result = Number % 2
IF Result = 0
THEN Print “The number is even number”
ELSE
THEN Print “The number is odd number”
END
FLOWCHART
Flowchart
• Schematic representation of process or algorithm.
• Schematic is a illustration of system in simplified or symbolic
form.
Flowchart Symbols
NS DIAGRAM
NS Diagram
• NS Diagram is a graphical illustration of structured
progamming design.
• Its method is by drawing a table for illustrating an algorithm.
• Founded in 1972 by Isaac Nassi & Ben Shneiderman.
• Known as structograms.
NS Diagram
• Process notation : Process

• Selection notation: True


Condition False

• Loop notation : Unfulfilled


– WHILE Condition Process

Process
– DO-WHILE Unfulfilled
Condition
NS Diagram
• Example of NS Diagram to determine odd-even
numbers

Insert number

Number %Equal
2 to 1
Equal to 0

Print “Even Print “Odd


Number” Number”
INTRODUCTION OF IDE AND JAVA
Java
• Java was developed by a team led by James
Gosling at Sun Microsystems.
• (1991) Java was originally called Oak. Java was for
use in embedded chips in consumer electronic
appliances.
• (1995), renamed Java, it was redesigned for
developing web applications
• (2010) Sun Microsystems was purchased by Oracle
Java Language
Specification and API
• Java language specification and Java API define the Java
standard.
• Java language specification is a technical definition of the
language that includes the syntax, and semantics of the Java
programming language.
• The application program interface (API) contains pre-
defined classes and interfaces for developing Java programs.
• The java language specification is stable, but the API is still
expanding.
Java Edition and JRE
• Java Edition:
– Java Micro Edition : mobile devices, embedded system, etc.
– Java Standard Edition : Desktop, Server, etc.
– Java Enterprise Edition : Multi – tier client – server apps.

• Java Runtime Environment (JRE)


– A Software that execute Java based application
– Java Virtual Machine (JVM) is a collection of programs to execute
java bytecode on any computer platform.
Java Development Toolkit
• Java Development Toolkit (JDK) consist of set of separate program,
each invoked from a command line, for developing and testing Java
programs.
• Java development tool is a software that provides an Integrated
Development Environment (IDE) for rapidly developing Java programs.
Example:
– JBuilder by Borland (www.borland.com)
– NetBeans Open Source by Sun (www.netbeans.org)
– Eclipse Open Source by IBM (www.eclipse.org)
– Code Warrior by Metrowerks (www.metrowerks.com)
– TextPad Editor (www.textpad.com)
– JCreator LE (www.jcreator.com)
– JEdit (www.jedit.org)
Most Commonly Application
Programs in JDK
• Compiler: javac
• Interpreter: java
• Debugger: jdb
• Applet Viewer : appletviewer
• Documentation: javadoc
• Compressor: jar

Welcome.java Welcome.class
Java
(Java source (Java bytecode JVM
code file) Compiler executable fle)
Compiled by generates Executed by
CREATING, COMPILING, AND
EXECUTE
Creating, Compiling, &
Executing
Create/Modify Source Code
Source code (developed by the programmer)
public class Welcome { Save on the
Public static void main(String [] args) disk
{ Source Code
System.out.println(“Welcome to Java!”);
}
}
Compile Source Code
e.g., javac Welcome.java

Bytecode (generated by the compiler for JVM to If compilation


read and interpret, not for you to understand) Stored in the errors
disk

… Bytecode
Method Welcome()
0 aload_0
If runtime
… errors or
Run Bytecode
Incorrect
Method void main(java.lang.String[]) e.g., java Welcome result
0 getstatic #2 …
3 ldc #3 <String “Welcome to Java!”>
5 invokevirtual #4
Result
8 return
EXERCISE
Exercise
1. From the following pseudocode example of grade average
calculation, create the flowchart and NS Diagram
BEGIN
read balloons
DOWHILE balloons is greater than zero
Print “i have “, balloons
ballons = ballons - 1
ENDDO
END
Exercise

2. Using the Pseudo-code and flowchart, create:


a. an algorithm to calculate a circle area!
b. an algorithm to receive two numbers, and display
the series of even number between numbers1 and
numbers 2
c. construct an algorithm that will prompt an operator
to input salary per hour and number of working
hours. display the amount salary that will be
recieved by the worker
REFERENCES
References
• Lesley Anne Robertson, 2006, Simple program design : a
step by step approach, Vol.05, Course Technology. Chapter
1 and 2.
• Y. Daniel, 2013, Introduction to Java Programming, 9th
Edition, Chapter 1
Thank You

You might also like