CIS 265: Data Structure and Algorithms: Java Programming Review
CIS 265: Data Structure and Algorithms: Java Programming Review
Algorithms
Lecture 01
Java Programming Review
2
Topics
• Fundamental Data Structures
▫ Linked lists, stacks, queues, priority queues
▫ Sets and maps
▫ Trees
Binary trees
AVL trees
▫ Hashing
▫ Graphs
• Algorithm basics
▫ Big O notation
▫ Sorting and searching
3
Outline
• Introduction to computers, programs, and Java
• Elementary programming
• Selection
• Loops
• Methods
4
Outline
• Introduction to computers, programs, and Java
• Elementary programming
• Selection
• Loops
• Methods
5
Introduction to programs
• Writing a program involves designing
algorithms and translating algorithms into code.
• An algorithm describes how a problem is solved
in terms of the actions to be executed and the
order of their execution.
6
Development Process
Outline
• Introduction to computers, programs, and Java
• Elementary programming
• Selection
• Loops
• Methods
9
Input/output
• Use scanner class to create input object
• Use methods of a scanner objects: nextDouble()
11
Variables
• They represent data of a certain type.
• They are basic structures.
• Examples:
• Java expressions
13
Example
• Write a program that obtains minutes and
remaining seconds from an amount of time in
seconds.
14
Outline
• Introduction to computers, programs, and Java
• Elementary programming
• Selection
• Loops
• Methods
15
if statements
• One way if statement
Block braces
• The block braces can be omitted if they enclose a
single statement.
• Example:
18
• Example:
19
Nested if Statements
• Example:
Logical Operators
• Sometimes, whether a statement is executed is
determined by a combination of several
conditions. You can use logical operators to
combine these conditions.
Switch Statements
• The if statement usually makes selection based
on a single true or false condition. Switch
statement can work based on different
conditions.
23
Caution
• Do not forget to use a break statement when
one is needed. Once a case is matched, the
statements starting from the matched case are
executed until a break statement or the end of
the switch statement is reached.
24
Conditional Expressions
• Example:
• Alternative way:
• Example:
Outline
• Introduction to computers, programs, and Java
• Elementary programming
• Selection
• Loops
• Methods
27
Loops
• Suppose you need to print a string a hundred
times.
Examples
• Write a program to receive 100 numbers and
calculate their average.
• Write a program to receive 100 numbers and
count even and odd numbers.
30
Example
32
Outline
• Introduction to computers, programs, and Java
• Elementary programming
• Selection
• Loops
• Methods
35
Why methods?
• Code looks similar except that starting and
ending integers are different. Wouldn’t it be nice
if we could write the common code once and
reuse it without rewriting it?
36
Defining a method
• The syntax is as follows:
37
Calling a method
• In creating a method,
you define what the
method is to do. To use
a method, you have to
call or invoke it.
• If the method returns a
value, a call to the
method is usually
treated as a value. For
example:
38
Call Stacks
• Each time a method is invoked, the system
stores parameters and variables in an area of
memory known as a stack, which stores
elements in last-in, first-out fashion.
39
Look at stack
42