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

DSA Lecture 2 - 3

The document discusses concepts of object-oriented design, including composition, aggregation, and inheritance. It provides examples of each: 1) Composition - A University class contains Department objects, owning the departments. 2) Aggregation - A Department class contains references to Professor objects, which can exist outside the department. 3) Inheritance - A Professor class inherits from a Person class, with professors being a type of person.

Uploaded by

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

DSA Lecture 2 - 3

The document discusses concepts of object-oriented design, including composition, aggregation, and inheritance. It provides examples of each: 1) Composition - A University class contains Department objects, owning the departments. 2) Aggregation - A Department class contains references to Professor objects, which can exist outside the department. 3) Inheritance - A Professor class inherits from a Person class, with professors being a type of person.

Uploaded by

Husnain
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Data Structures and

Algorithms

Lecture 2 & 3
Outline
• UML Diagrams
• Concepts of Object Oriented Design
UML
Diagrams
Concepts of Object
Oriented Design

• There are several different ways to associate one


class with another: composition, aggregation, and
inheritance.

•When a class A contains references to instances of


a class B and controls all access to those instances,
we say that A is a composition of B.

•For example, a University class would be a


composition of Department objects.
Composition and
Aggregation
•Each Department object belongs to a unique
University object, which controls access to its
department.

•If A is a composition of B, we say that an A object


“owns a” B object. For example, a university owns
a department.

•When a class A contains references to a class B


whose instances exist and are accessible outside
of A, we say that A is an aggregation of B.
Composition and
Aggregation
•For example, in a university software system, a
Department class would contain references to
Professor objects who are members of the
department, but who also exist outside the
department.
•In fact, a professor could be a member of two
different departments.
•If A is an aggregation of B, we say that an A
object “has a” B object. For example, a
department has a professor.
Extension

•When a class A includes all the members of a class


B, we say that A is an extension of B, and that it
inherits all the properties of B.
•For example, a Professor class would be an extension
of a Person objects. If A is an extension of B, we say
that an A object “is a” B object.
•For example, a professor is a person. Composition Contains-a A book contains a
chapter
Aggregation Has-a A book has a
publisher
Inheritance Is-a A book is a printed
resource
Code Snippet
(Composition)
public class University{
String Uname;
Department department;
……………
…………..
public class Department{
//composition with inner class
String Dname;
Professor professor;
}
}
Note: Composition and inner class concepts are
different, however it is one way of implementing
composition .
Code Snippet
(Composition)
public class University{
String Uname;
private final Department department=new Department();
……………
…………..
}
public class Department{ // composition with object initialization
String Dname;
Professor professor;
…..
}
Public class Professor{………}
Short summary
•The University class is a composite of
Department objects.
• The existence of a department is dependent
upon the existence of its university.
Therefore, the Department class should be
completely controlled and insulated by the
University class.
•The University.Department class is an
aggregate of Professor objects.
• The existence of a professor is independent
of his or her department’s existence.
Therefore, the Professor class is defined
separately from the University.Department
class.
Pre-conditions and post conditions

•A pre-condition for an operation is a Public void setDay(int day){


condition that is assumed to be true before //pre-condition
the operation begins.
If (day<1 || day31) throw new
• A post condition is a condition that is
guaranteed to be true after the operation
IllegalArgumentException();
ends, provided the pre-condition is //post-condition
satisfied. this.day=day;
}
ARRAYS
The array data structure
• An array is an indexed sequence of components
• Typically, the array occupies sequential storage locations
• The length of the array is determined when the array is
created, and cannot be changed
• Each component of the array has a fixed, unique index
• Indices range from a lower bound to an upper
bound
• Any component of the array can be
inspected or updated by using its index
• This is an efficient operation: O(1) = constant time

13
Array variations
• The array indices may be integers (C, Java) or other
discrete data types (Pascal, Ada)

• The lower bound may be zero (C, Java), one (Fortran),


or chosen by the programmer (Pascal, Ada)

14
Arrays in Java I
• Array indices are integers
• An array of length n has bounds 0 and n-1
• Arrays are homogeneous
• However, an array of an object type may contain objects
of any subtype of that object
• For example, an array of Animal may contain objects of type
Cat and objects of type Dog
• An array of Object may contain any type of object (but cannot
contain primitives)

15
Arrays in Java II
• Arrays are objects
• Arrays are allocated by new, manipulated by reference, and garbage
collected
• However, the usual bracket notation a[i] is provided as syntactic sugar
• Arrays are reflective
• a.length is the length of array a
• a.getClass() is the type of array a
• An array of integers has type [I
• An array of Strings has type [Ljava.lang.String;

16
Arrays in Java III
• Here’s one way to visualize an array in Java:
myArray

class tag [I
length
4
0 17
1 23
2 948
3 3
17
Subarrays
• A subarray is a consecutive portion of an array

0 1 2 3 4 5 6 7 8 9
array a [I 10 1 1 2 3 5 8 13 21 34 55

subarray a[2...6]

• Java provides no language support for subarrays


• To use a subarray, you must keep track of (1) the array itself,
(2) the lower bound, and (3) the upper bound
• Typically, these will be three parameters to a method that
does something with the subarray
18
Array as an ADT
• An array is an Abstract Data Type
• The array type has a set of values
• The values are all the possible arrays
• The array type has a set of operations that can be
applied uniformly to each of these values
• The only operation is indexing
• Alternatively, this can be viewed as two operations:
• inspecting an indexed location
• storing into an indexed location
• It’s abstract because the implementation is hidden: all
access is via the defined operations

19
ADT (abstract
datatype)
• The abstract datatype is special
kind of datatype, whose
behavior is defined by a set of
values and set of operations.
The keyword “Abstract” is used
as we can use these datatypes,
we can perform different
operations. But how those
operations are working that is
totally hidden from the user.
Two-dimensional arrays I
• Some languages (Fortran, Pascal) support two-dimensional (2D)
arrays: columns

a b c d • A two-dimensional array may be stored


rows e f g h in one-dimensional computer memory in
i j k l either of two ways:
logical view
row 0 row 1 row 2

row major order: a b c d e f g h i j k l

col 0 col 1 col 2 col 3

column major order: a e i b f j c g k d h l


21
Two-dimensional arrays II
• In a 2D array, we generally consider the first index to be the row,
and the second to be the column: a[row, col]
columns
0 1 2 3 4

0 0,0 0,1 0,2 0,3 0,4


1
rows 2 1,0 1,1 1,2 1,3 1,4
3 2,0 2,1 2,2 2,3 2,4
3,0 3,1 3,2 3,3 3,4

• In most languages we don’t need to know the implementation--


we work with this abstraction
• In C and C++, we do need to know the implementation
22
2D arrays in Java
• Java doesn’t have “real” 2D arrays, but array elements can
themselves be arrays:
• int x[][] denotes an array x of array components, each of which is
an array of integer components
• We can define the above array like this:
x = new int[5][8];
and treat it as a regular 2D array
• This is an array of 5 arrays
• Each subarray is an array of 8 ints

• However, we can do fancier things than this with arrays in


Java

23
Operations in arrays

Operations on linear structures such as arrays include,


1: Traversal: Processing each element in the list
2. Searching: Finding the location of the element with a given
value or the record with a given key.
3. Insertion: Adding a new element
4. Deletion: Removing an element
5: Sorting: Arranging the elements in some type of order.
Traversal in Arrays
Swapping in Arrays
Code
int[] a= new int[] {2,4,6,7,8}; void swap(int[] a, int i, int j){
for (int i=0;i<a.length; i++) int ai= a[i];
System.out.println(a[i]); int aj =a[j];
If (ai==aj) return;
a[i]=aj;
a[j]=ai;
}
Resizing array usingSystem.arraycopy
Method
public static int[] resize(int[]a, int n) System.arraycopy(a, m,aa,mm,k)

{
a= source array
If (n<a.length) throw new aa= target array
IllegalArgumentException(); m= starting index of source array
Int[] aa=new int[n]; mm= starting index of target array
System.arraycopy(a,0,aa,0, a.length); k= number of elements copied

return aa;
}
Inserting an element into an array
• Suppose we want to insert the value 8 into this sorted array
(while keeping the array sorted)

1 3 3 7 12 14 17 19 22 30
• We can do this by shifting all the elements after the mark right by
one location
• Of course, we have to discard the 30 when we do this

1 3 3 7 8 12 14 17 19 22 30

• Moving all those elements makes this a slow


operation (linear in the size of the array)
27
Deleting an element from an array
• Deleting an element is similar--again, we have to move all
the elements after it

1 3 3 7 8 12 14 17 19 22

1 3 3 7 12 14 17 19 22 ?

• Deletion is a slow operation; we don’t want to do it very often


• Deletion leaves a “vacant” location at the end
• How do we mark it vacant?
• Every bit pattern represents a valid integer
• We must keep a count of how many valid elements are in the array

28
Arrays
• Here are the advantages of arrays:
• Very efficient, in terms of both time and space
• Convenient syntax
• Here is the main disadvantage:
• Fixed size

29
Conclusions
• Arrays are not identical in all languages

• Arrays have the following advantages:


• Accessing an element by its index is very fast (constant
time)

• Arrays have the following disadvantages:


• All elements must be of the same type
• The array size is fixed and can never be changed
• Insertion into arrays and deletion from arrays is very slow

30

You might also like