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

Lecture 1 Introduction

The document provides an overview of Abstract Data Types (ADTs) and their implementation through data structures and algorithms. It explains the concepts of data types, algorithms, pseudocode, flowcharts, and object-oriented programming (OOP) with examples. Additionally, it covers various data structures like queues and linked lists, as well as inheritance and abstract classes in OOP.

Uploaded by

Joshua Dalmas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lecture 1 Introduction

The document provides an overview of Abstract Data Types (ADTs) and their implementation through data structures and algorithms. It explains the concepts of data types, algorithms, pseudocode, flowcharts, and object-oriented programming (OOP) with examples. Additionally, it covers various data structures like queues and linked lists, as well as inheritance and abstract classes in OOP.

Uploaded by

Joshua Dalmas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Introduction

1
Outline
• Abstract Data Types
• What is Data Structures and Algorithms all about?
• Review of Algorithms, Pseudocode and Flowchart
• Review of OOP

2
Abstract Data Types

3
Data types
• A data type is characterized by:
• a domain of values
• a set of operations (e.g add, subtract, square root), which can be applied uniformly to all
these values
• Examples: int, float, double, char
• Data types depend on the programming language

4
Abstract Data Types
• An Abstract Data Type (ADT) is:
• a set of values
• a set of operations, which can be applied uniformly to all these values
• Thus, an ADT couples its data (the set of values) and operations
(methods/functions)
• From Latin abstract means: to ‘pull out’—the essentials
• To defer or hide the details
• Abstraction emphasizes essentials and defers/hides the details
• An ADT is a formal description, not code; independent of any programming
language

5
List ADT - Example
• List of values
• Operations:
• Insert
• Delete
• Order

6
Queue ADT - Example
• Queue operations
• create
• destroy
• enqueue G enqueue FEDCB
dequeue A
• dequeue
• is_empty
• FIFO: First In First Out

7
Applications of the Queue
• Hold jobs for a printer
• Store packets on network routers
• Make waitlists fair
• Breadth first search

8
What is Data Structures and
Algorithms all about?

9
Data Structures and Algorithms
• Data Structure:
• Data structures implement ADTs.
• They contain operations (implemented) to manipulate data elements.
• ADT is in the logical level while a data structure is in the implementation level.
• So a list can be described in terms of an abstract data type (we can insert into it, get the nth
element, delete an element, etc.),
• A linked list, for example, is an implementation of a list abstract data type -> it implements
the specified behavior by, for example, inserting an element or deleting an element.
• We could implement the same list abstract data type in many other ways, for example with
an array, or binary tree.
• Algorithm:
• A high level, language independent description of a step-by-step process for solving a
problem

10
Example Data Structures
• Queue
• Stack
• Linked list
• Graphs
• Binary trees

11
Example Algorithms
• Search Algorithms
• Sort Algorithms

12
Review of Algorithms,
Pseudocode and Flowchart

13
Steps in Problem Solving
• First produce a general algorithm (one can use pseudocode)
• Pseudocode is an artificial and informal language that helps programmers
develop algorithms.
• Pseudocode is very similar to everyday English.
• Refine the pseudocode successively to get step by step detailed algorithm that is
very close to a computer language.

14
Pseudocode & Algorithm – Example 1
• Write an algorithm to determine a student’s final grade and indicate whether it is
passing or failing. The final grade is calculated as the average of four marks.

15
Pseudocode & Algorithm – Example 1
Pseudocode:
• Input a set of 4 marks
• Calculate their average by summing and dividing by 4
• if average is below 50
Print “FAIL”
else
Print “PASS”

16
Pseudocode & Algorithm - – Example 1
• Detailed Algorithm:
Step 1: Input M1,M2,M3,M4
Step 2: GRADE  (M1+M2+M3+M4)/4
Step 3: if (GRADE < 50) then
Print “FAIL”
else
Print “PASS”
endif

17
Pseudocode & Algorithm - Example 2
• Write a pseudocode, 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

18
Pseudocode & Algorithm - Example 2
Pseudocode:
• Input the coefficients (a, b, c) of the quadratic equation
• Calculate d
• Calculate x1
• Calculate x2
• Print x1 and x2

19
Pseudocode & Algorithm - Example 2
START

• Algorithm:
Input
• Step 1: Input a, b, c a, b, c
• Step 2: d  sqrt ( b  b  4  a  c)
• Step 3: x1  (–b + d) / (2 x a) d  sqrt(b x b – 4 x a x c)

• Step 4: x2  (–b – d) / (2 x a) x1 (–b + d) / (2 x a)


• Step 5: Print x1, x2
X2  (–b – d) / (2 x a)

Print
x1 ,x2

STOP

20
Pseudocode & Algorithm - Example 3
• Write an algorithm that reads two values, determines the largest value and prints
the largest value with an identifying message.
ALGORITHM
Step 1: Input VALUE1, VALUE2
Step 2: if (VALUE1 > VALUE2) then
MAX  VALUE1
else
MAX  VALUE2
endif
Step 3: Print “The largest value is”, MAX

21
Pseudocode & Algorithm - Example 3
START

Input
VALUE1,VALUE2

Y is
N
VALUE1>VALUE2

MAX  VALUE1 MAX  VALUE2

Print
“The largest value is”,
MAX

STOP 22
Example 4
• Write an algorithm that reads three numbers and prints the value of
the largest number.

23
Pseudocode & Algorithm - Example 4
Step 1: Input N1, N2, N3
Step 2: if (N1>N2) then
if (N1>N3) then
MAX  N1 [N1>N2, N1>N3]
else
MAX  N3 [N3>N1>N2]
endif
else
if (N2>N3) then
MAX  N2 [N2>N1, N2>N3]
else
MAX  N3 [N3>N2>N1]
endif
endif
Step 3: Print “The largest number is”, MAX

24
Review of OOP

25
Example 1
public class BankAccount
{
private String ownersName;
private int accountNumber;
protected float balance;

public BankAccount(int anAccountNumber, String aName)


{
accountNumber = anAccountNumber;
ownersName = aName;
}

public void deposit(float anAmount)


{
if (anAmount>0.0)
balance = balance + anAmount;
}

public void withdraw(float anAmount)


{
if ((anAmount>0.0) && (balance>anAmount))
balance = balance - anAmount;
}

public float getBalance()


{
return balance;
}
}

26
Example 2 - Inheritance
public class BankAccount
{
private String ownersName;
private int accountNumber;
private float balance;

public BankAccount(int anAccountNumber, String aName)


{
accountNumber = anAccountNumber;
ownersName = aName;
}
[...]
}

public class OverdraftAccount extends BankAccount


{
private float overdraftLimit;

public OverdraftAccount(int anAccountNumber, String aName, float aLimit)


{
super(anAccountNumber, aName);
overdraftLimit = aLimit;
}
}

27
Example 2 – Inheritance – Method Overriding

public class OverdraftAccount extends BankAccount


{
private float limit;

public void withdraw(float anAmount)


{
if ((anAmount>0.0) && (getBalance()+limit>anAmount))
balance = balance - anAmount;
}

28
Abstract Classes
• Superclasses are created through the process called "generalization“
o Common features (methods or variables) are factored out of object
classifications (ie. classes).
o Those features are formalized in a class. This becomes the superclass
o The classes from which the common features were taken become subclasses
to the newly created super class
• Often, some superclass do not have a "meaning" or does not directly relate to a
"thing" in the real world
• It is an artifact of the generalization process
• Because of this, abstract classes cannot be instantiated
• They act as place holders for abstraction

29
Abstract Classes
• In the following example, the subclasses represent objects taken from the problem
domain.
• The superclass represents an abstract concept that does not exist "as is" in the real
world.

Abstract superclass: Vehicle Note: UML represents abstract


- make: String classes by displaying their name
- model: String in italics.
- tireCount: int

Car Truck
- trunkCapacity: int - bedCapacity: int

30
Defining Abstract Classes

• Inheritance is declared using the "extends" keyword


• If inheritance is not defined, the class extends a class called Object

public abstract class Vehicle


{ Vehicle
private String make; - make: String
private String model; - model: String
private int tireCount; - tireCount: int
[...]

public class Car extends Vehicle


{
Car Truck
private int trunkCapacity;
- trunkCapacity: int - bedCapacity: int
[...]

public class Truck extends Vehicle


{
private int bedCapacity; Often referred to as "concrete" classes
[...]

31
Example

32
Example

33
Example

34
35
Here, we cannot instantiate a new Employee, but if we instantiate a new
Salary object, the Salary object will inherit the three fields and seven
methods from Employee.

36
37

You might also like