0% found this document useful (0 votes)
42 views42 pages

CIS 265: Data Structure and Algorithms: Java Programming Review

This document provides an overview of topics covered in a data structures and algorithms course, including fundamental data structures like linked lists, trees, and graphs. It outlines the course structure, which begins with a review of Java programming concepts like elementary programming, selection, loops, and methods. Specific topics that will be covered are input/output, variables, operations, if/else statements, switch statements, loops (while, do-while, for), methods, and parameter passing.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views42 pages

CIS 265: Data Structure and Algorithms: Java Programming Review

This document provides an overview of topics covered in a data structures and algorithms course, including fundamental data structures like linked lists, trees, and graphs. It outlines the course structure, which begins with a review of Java programming concepts like elementary programming, selection, loops, and methods. Specific topics that will be covered are input/output, variables, operations, if/else statements, switch statements, loops (while, do-while, for), methods, and parameter passing.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 42

CIS 265: Data Structure and

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

Executing a Java Program


• You can use any text
editor or IDE to create
and edit a Java source-
code file.
• The source file must end Creating java source file
with the extension .java
and must have exactly
the same name as the
public class name.
• You must first install
and configure JDK
before compiling and
running programs
The execution process
7

Development Process

The Java program development process


8

Outline
• Introduction to computers, programs, and Java
• Elementary programming
• Selection
• Loops
• Methods
9

Writing Simple Programs


Example: Write a program to calculate the area of
a circle.
10

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:

Numeric data types


12

Operations & Expressions


• Operations:

• 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

Boolean Data Type


• Example:

• Java provides six comparison operators:


16

if statements
• One way if statement

• The boolean expression is enclosed in


parenthesis.
17

Block braces
• The block braces can be omitted if they enclose a
single statement.

• Example:
18

Two Way if Statements

• Example:
19

Nested if Statements
• Example:

• Problem: Compute Body Mass Index


20

Body mass index program


21

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.

• How the truth table will look like?


22

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

Formatting Console Output


25

Conditional Expressions
• Example:

• Alternative way:
• Example:

• Find out a number is even or odd:


26

Outline
• Introduction to computers, programs, and Java
• Elementary programming
• Selection
• Loops
• Methods
27

Loops
• Suppose you need to print a string a hundred
times.

• Using a loop statement, you simply tell the


computer to print a string a hundred times
without having to code the print statement a
hundred times, as follows:
28

The while loop


• Example:
29

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

The do-while Loop


• The loop body is executed first. Then the loop-
continuation-condition is evaluated. If the
evaluation is true, the loop body is executed
again; if it is false, the do-while loop
terminates.
31

Example
32

The For loop


• It is a quite common loop:

• A for loop can be used to simplify the proceeding


loop:
33

Which loop to use?


• The while loop and for loop are called pretest loops
because the continuation condition is checked
before the loop body is executed.

• The do-while loop is called a posttest loop because


the condition is checked after the loop body is
executed.
• The three forms of loop statements, while, do-
while, and for, are expressively equivalent; that is,
you can write a loop in any of these three forms.
34

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

Passing parameters by values


• When calling a method, you need to provide arguments,
which must be given in the same order as their
respective parameters in the method signature.
• When you invoke a method with a parameter, the value
of the argument is passed to the parameter. This is
referred to as pass-by-value.
40

Swap two values


41

Look at stack
42

The scope of variables


• The scope of a variable is the part of the program where
the variable can be referenced. A variable defined inside
a method is referred to as a local variable.
• The scope of a local variable starts from its declaration
and continues to the end of the block that contains the
variable.

You might also like