0% found this document useful (0 votes)
104 views34 pages

Itec103-Module2 2022

This document provides information on a self-paced learning module for the course "Intermediate Programming" at Laguna State Polytechnic University. The module discusses data structures and file streaming as well as object-oriented programming in C#. Students will learn how to implement common data structures like arrays, stacks, and queues in C# and understand object-oriented concepts such as classes, objects, encapsulation, inheritance and polymorphism. The intended learning outcomes are for students to understand and be able to program using various data structures in C# as well as understand and apply OOP principles. The module activities include online discussions, learning guide questions, and an offline lecture on arrays in C#.

Uploaded by

chichi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views34 pages

Itec103-Module2 2022

This document provides information on a self-paced learning module for the course "Intermediate Programming" at Laguna State Polytechnic University. The module discusses data structures and file streaming as well as object-oriented programming in C#. Students will learn how to implement common data structures like arrays, stacks, and queues in C# and understand object-oriented concepts such as classes, objects, encapsulation, inheritance and polymorphism. The intended learning outcomes are for students to understand and be able to program using various data structures in C# as well as understand and apply OOP principles. The module activities include online discussions, learning guide questions, and an offline lecture on arrays in C#.

Uploaded by

chichi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Republic of the Philippines

Laguna State Polytechnic University


ISO 9001:2015 Certified
Province of Laguna
Level I Institutionally Accredited

LSPU Self-Paced Learning Module (SLM)


Course Intermediate Programming
Sem/AY Second Semester/2020-2021
Module No. 2
Lesson Title DATA STRUCTURE AND FILE STREAMING/OBJECT ORIENTED PROGRAMMING IN C#
Week
7-10
Duration
Date
This lesson will discuss the Data Structure and File Streaming, in this lesson we consider the
Description common data structures that are used in various computational problems. You will learn how
of the these data structures are implemented in C# programming languages and will practice
Lesson implementing them in our programming assignments. This will help you to understand what is
going on inside a particular built-in implementation of a data structure and what to expect
from it. This lesson will discuss the object-oriented programming language includes classes
and objects, encapsulation, inheritance and polymorphism. The fundamental idea behind OOP
is to combine into a single unit both data and the method that operate on the data such units
are called objects.

Learning Outcomes
Intended Students should be able to meet the following intended learning outcomes:
Learning  Understand the use of Array in C# Programming.
Outcomes  Understand how to declare Array in C#.
 Identify the difference of Program using one dimensional and two dimensional
Array.
 Understand the definition of stacks and queues.
 Understand the declaration of stacks and queues.
 Understand and learn to Add and remove of stack and queues in C#.
 Understand the programs using Stacks and Queues.
 Discuss and Learn Different Object-Oriented Programming
 Discuss the OOP important features
 Discuss and learn the differences between Abstraction and Encapsulation 

Targets/ At the end of the lesson, students should be able to:


Objectives  Learn and understand the use of Array in C# Programming.
 Learn how to declare Array in C#.

LSPU SELF-PACED LEARNING MODULE: TECHNOLOGY FOR TEACHING AND LEARNING


Republic of the Philippines
Laguna State Polytechnic University
ISO 9001:2015 Certified
Province of Laguna
Level I Institutionally Accredited

 Learn how to program using one dimensional and two-dimensional Array.


 To Understand the use of stacks and queues
 To know and understand the declaration of stacks and queues
 To learn to add and remove of stack and queues in c#
 Example programs using stacks and queues
 Understand and apply Classes and Objects to programming
 Understand and apply Encapsulation techniques in Programming
 Understand and apply Inheritance in Programming
 Understand and apply Polymorphism in C# Programming

Student Learning Strategies

Online Activities 1. Online Discussion via Google Meet


(Synchronous/ You will be directed to attend in a Three-Hour class discussion (two-
hour Synchronous Online and 1 HR Asynchronous Online) on the
Asynchronous) Data Structure And File Streaming . To have access to the Online Discussion,
refer to this link: ____________________.

The online discussion will happen on

(For further instructions, refer to your Google Classroom and see the
schedule of activities for this module)

2. Learning Guide Questions:


3. What is an Array?
4. What are the important concepts related to array which should be clear
to a C# programmer?
5. What are the difference of program using one dimensional and two
dimensional Array?
6. What is the definition of Stacks and Queues?
7. What are the Declaration of Stack and Queues?
8. What the example Programs that use Stack and Queues.
9. What are three types of public methods in Stack class?
10. What OOP and its features?
11. What are the differences between abstraction and encapsulation?

LSPU SELF-PACED LEARNING MODULE: TECHNOLOGY FOR TEACHING AND LEARNING


Republic of the Philippines
Laguna State Polytechnic University
ISO 9001:2015 Certified
Province of Laguna
Level I Institutionally Accredited

Note: The insight that you will post on online discussion forum using Learning Management
System (LMS) will receive additional scores in class participation.

LSPU SELF-PACED LEARNING MODULE: TECHNOLOGY FOR TEACHING AND LEARNING


Republic of the Philippines
Laguna State Polytechnic University
ISO 9001:2015 Certified
Province of Laguna
Level I Institutionally Accredited

Lecture Guide:

ARRAY

What is an Array?

An array stores a fixed-size sequential collection of elements of the same type. An array is
used to store a collection of data, but it is often more useful to think of an array as a
collection of variables of the same type stored at contiguous memory locations.

• Instead of declaring individual variables, such as number0, number1, ..., and


number99, you declare one array variable such as numbers and use numbers[0],
numbers[1], and ..., numbers[99] to represent individual variables. A specific
element in an array is accessed by an index.

• All arrays consist of contiguous memory locations. The lowest address corresponds
to the first element and the highest address to the last element.

• Declaring Arrays
Offline Activities • To declare an array in C#, you can use the following syntax −
(e-Learning/Self-Paced)
datatype[] arrayName;

where,

• datatype is used to specify the type of elements in the array.

• [ ] specifies the rank of the array. The rank specifies the size of the array.

• arrayName specifies the name of the array.

For example,

double[] balance;

• Initializing an Array

• Declaring an array does not initialize the array in the memory. When the array
variable is initialized, you can assign values to the array.

• Array is a reference type, so you need to use the new keyword to create an


instance of the array. For example,

double[] balance = new double[10];

• Assigning Values to an Array

LSPU SELF-PACED LEARNING MODULE: TECHNOLOGY FOR TEACHING AND LEARNING


Republic of the Philippines
Laguna State Polytechnic University
ISO 9001:2015 Certified
Province of Laguna
Level I Institutionally Accredited
You can assign values to individual array elements, by using the index number, like −

double[] balance = new double[10];

balance[0] = 4500.0;

You can assign values to the array at the time of declaration, as shown −

double[] balance = { 2340.0, 4523.69, 3421.0};

You can also create and initialize an array, as shown −

int [] marks = new int[5] { 99, 98, 92, 97, 95};

You may also omit the size of the array, as shown −

int [] marks = new int[] { 99, 98, 92, 97, 95};

You can copy an array variable into another target array variable. In such case, both the
target and source point to the same memory location −

int [] marks = new int[] { 99, 98, 92, 97, 95};

int[] score = marks;

• Accessing Array Elements

An element is accessed by indexing the array name. This is done by placing the index of the
element within square brackets after the name of the array. For example,

double salary = balance[9];

• The following example, demonstrates the above-mentioned concepts declaration,


assignment, and accessing arrays −

using System;

namespace ArrayApplication {

class MyArray {

static void Main(string[] args) {

int [] n = new int[10]; /* n is an array of 10 integers */

int i,j;

/* initialize elements of array n */

for ( i = 0; i < 10; i++ ) {

n[ i ] = i + 100;

LSPU SELF-PACED LEARNING MODULE: TECHNOLOGY FOR TEACHING AND LEARNING


Republic of the Philippines
Laguna State Polytechnic University
ISO 9001:2015 Certified
Province of Laguna
Level I Institutionally Accredited

/* output each array element's value */

for (j = 0; j < 10; j++ ) {

Console.WriteLine("Element[{0}] = {1}", j, n[j]);

Console.ReadKey();

• When the above code is compiled and executed, it produces the following result −

Element[0] = 100

Element[1] = 101

Element[2] = 102

Element[3] = 103

Element[4] = 104

Element[5] = 105

Element[6] = 106

Element[7] = 107

Element[8] = 108

Element[9] = 109

• Using the foreach Loop

In the previous example, we used a for loop for accessing each array element. You can also
use a foreach statement to iterate through an array.

using System;

namespace ArrayApplication {

class MyArray {

static void Main(string[] args) {

int [] n = new int[10]; /* n is an array of 10 integers */

LSPU SELF-PACED LEARNING MODULE: TECHNOLOGY FOR TEACHING AND LEARNING


Republic of the Philippines
Laguna State Polytechnic University
ISO 9001:2015 Certified
Province of Laguna
Level I Institutionally Accredited

/* initialize elements of array n */

for ( int i = 0; i < 10; i++ ) {

n[i] = i + 100;

/* output each array element's value */

foreach (int j in n ) {

int i = j-100;

Console.WriteLine("Element[{0}] = {1}", i, j);

Console.ReadKey();

When the above code is compiled and executed, it produces the following result −

Element[0] = 100

Element[1] = 101

Element[2] = 102

Element[3] = 103

Element[4] = 104

Element[5] = 105

Element[6] = 106

Element[7] = 107

Element[8] = 108

Element[9] = 109

• C# Arrays

• There are following few important concepts related to array which should be clear

LSPU SELF-PACED LEARNING MODULE: TECHNOLOGY FOR TEACHING AND LEARNING


Republic of the Philippines
Laguna State Polytechnic University
ISO 9001:2015 Certified
Province of Laguna
Level I Institutionally Accredited
to a C# programmer −

Multi-dimensional arrays

C# supports multidimensional arrays. The simplest form of the multidimensional array is


the two-dimensional array.

C# allows multidimensional arrays. Multi-dimensional arrays are also called rectangular


array. You can declare a 2-dimensional array of strings as −

string [,] names;

or, a 3-dimensional array of int variables as −

int [ , , ] m;

• Two-Dimensional Arrays

The simplest form of the multidimensional array is the 2-dimensional array. A 2-


dimensional array is a list of one-dimensional arrays.

A 2-dimensional array can be thought of as a table, which has x number of rows and y
number of columns. Following is a 2-dimensional array, which contains 3 rows and 4
columns −

• Thus, every element in the array a is identified by an element name of the form a[ i
, j ], where a is the name of the array, and i and j are the subscripts that uniquely
identify each element in array a.

• Initializing Two-Dimensional Arrays

Multidimensional arrays may be initialized by specifying bracketed values for each row.
The Following array is with 3 rows and each row has 4 columns.

int [,] a = new int [3,4] {

{0, 1, 2, 3} , /* initializers for row indexed by 0 */

{4, 5, 6, 7} , /* initializers for row indexed by 1 */

{8, 9, 10, 11} /* initializers for row indexed by 2 */

};

• Accessing Two-Dimensional Array Elements

An element in 2-dimensional array is accessed by using the subscripts. That is, row index
and column index of the array.

For example,

int val = a[2,3];

LSPU SELF-PACED LEARNING MODULE: TECHNOLOGY FOR TEACHING AND LEARNING


Republic of the Philippines
Laguna State Polytechnic University
ISO 9001:2015 Certified
Province of Laguna
Level I Institutionally Accredited
The above statement takes 4th element from the 3rd row of the array. You can verify it in
the above diagram. Let us check the program to handle a two dimensional array −

using System;

namespace ArrayApplication {

class MyArray {

static void Main(string[] args) {

/* an array with 5 rows and 2 columns*/

int[,] a = new int[5, 2] {{0,0}, {1,2}, {2,4}, {3,6}, {4,8} };

int i, j;

/* output each array element's value */

for (i = 0; i < 5; i++) {

for (j = 0; j < 2; j++) {

Console.WriteLine("a[{0},{1}] = {2}", i, j, a[i,j]);

Console.ReadKey();

a[0,0]: 0

a[0,1]: 0

a[1,0]: 1

a[1,1]: 2

a[2,0]: 2

a[2,1]: 4

a[3,0]: 3

LSPU SELF-PACED LEARNING MODULE: TECHNOLOGY FOR TEACHING AND LEARNING


Republic of the Philippines
Laguna State Polytechnic University
ISO 9001:2015 Certified
Province of Laguna
Level I Institutionally Accredited
a[3,1]: 6

a[4,0]: 4

a[4,1]: 8

Jagged arrays

C# supports multidimensional arrays, which are arrays of arrays.

A Jagged array is an array of arrays. You can declare a jagged array named scores of
type int as −

int [][] scores;

Declaring an array, does not create the array in memory. To create the above array −

int[][] scores = new int[5][];

for (int i = 0; i < scores.Length; i++) {

scores[i] = new int[4];

You can initialize a jagged array as −

int[][] scores = new int[2][]{new int[]{92,93,94},new int[]{85,66,87,88}};

Where, scores is an array of two arrays of integers - scores[0] is an array of 3 integers and
scores[1] is an array of 4 integers.

• The following example illustrates using a jagged array

using System;

namespace ArrayApplication {

class MyArray {

static void Main(string[] args) {

/* a jagged array of 5 array of integers*/

int[][] a = new int[][]{new int[]{0,0},new int[]{1,2},

new int[]{2,4},new int[]{ 3, 6 }, new int[]{ 4, 8 } };

int i, j;

/* output each array element's value */

LSPU SELF-PACED LEARNING MODULE: TECHNOLOGY FOR TEACHING AND LEARNING


Republic of the Philippines
Laguna State Polytechnic University
ISO 9001:2015 Certified
Province of Laguna
Level I Institutionally Accredited
for (i = 0; i < 5; i++) {

for (j = 0; j < 2; j++) {

Console.WriteLine("a[{0}][{1}] = {2}", i, j, a[i][j]);

Console.ReadKey();

Passing arrays to functions

You can pass to the function a pointer to an array by specifying the array's name without
an index.

You can pass an array as a function argument in C#. The following example demonstrates
this −

using System;

namespace ArrayApplication {

class MyArray {

double getAverage(int[] arr, int size) {

int i;

double avg;

int sum = 0;

for (i = 0; i < size; ++i) {

sum += arr[i];

avg = (double)sum / size;

return avg;

static void Main(string[] args) {

LSPU SELF-PACED LEARNING MODULE: TECHNOLOGY FOR TEACHING AND LEARNING


Republic of the Philippines
Laguna State Polytechnic University
ISO 9001:2015 Certified
Province of Laguna
Level I Institutionally Accredited
MyArray app = new MyArray();

/* an int array with 5 elements */

int [] balance = new int[]{1000, 2, 3, 17, 50};

double avg;

/* pass pointer to the array as an argument */

avg = app.getAverage(balance, 5 ) ;

/* output the returned value */

Console.WriteLine( "Average value is: {0} ", avg );

Console.ReadKey();

Param arrays

This is used for passing unknown number of parameters to a function.

At times, while declaring a method, you are not sure of the number of arguments passed
as a parameter. C# param arrays (or parameter arrays) come into help at such times.

The following example demonstrates this −

using System;

namespace ArrayApplication {

class ParamArray {

public int AddElements(params int[] arr) {

int sum = 0;

foreach (int i in arr) {

sum += i;

return sum;

LSPU SELF-PACED LEARNING MODULE: TECHNOLOGY FOR TEACHING AND LEARNING


Republic of the Philippines
Laguna State Polytechnic University
ISO 9001:2015 Certified
Province of Laguna
Level I Institutionally Accredited
}

class TestClass {

static void Main(string[] args) {

ParamArray app = new ParamArray();

int sum = app.AddElements(512, 720, 250, 567, 889);

Console.WriteLine("The sum is: {0}", sum);

Console.ReadKey();

The Array Class

Defined in System namespace, it is the base class to all arrays, and provides various
properties and methods for working with arrays.

The Array class is the base class for all the arrays in C#. It is defined in the System
namespace. The Array class provides various properties and methods to work with arrays.

• Properties of the Array Class

The following table describes some of the most commonly used properties of the Array
class −

• METHODS OF THE ARRAY CLASS

• METHODS OF ARRAY CLASS PART 2

using System;

namespace ArrayApplication {

class MyArray {

static void Main(string[] args) {

int[] list = { 34, 72, 13, 44, 25, 30, 10 };

int[] temp = list;

Console.Write("Original Array: ");

LSPU SELF-PACED LEARNING MODULE: TECHNOLOGY FOR TEACHING AND LEARNING


Republic of the Philippines
Laguna State Polytechnic University
ISO 9001:2015 Certified
Province of Laguna
Level I Institutionally Accredited

foreach (int i in list) {

Console.Write(i + " ");

Console.WriteLine();

// reverse the array

Array.Reverse(temp);

Console.Write("Reversed Array: ");

foreach (int i in temp) {

Console.Write(i + " ");

Console.WriteLine();

//sort the array

Array.Sort(list);

Console.Write("Sorted Array: ");

foreach (int i in list) {

Console.Write(i + " ");

Console.WriteLine();

Console.ReadKey();

LSPU SELF-PACED LEARNING MODULE: TECHNOLOGY FOR TEACHING AND LEARNING


Republic of the Philippines
Laguna State Polytechnic University
ISO 9001:2015 Certified
Province of Laguna
Level I Institutionally Accredited

STACKS AND QUEUES

What is a STACK?

The stack is a special case collection which represents a last in first out (LIFO) concept. To
first understand LIFO, let's take an example. Imagine a stack of books with each book kept
on top of each other.

The concept of last in first out in the case of books means that only the top most book can
be removed from the stack of books. It is not possible to remove a book from between,
because then that would disturb the setting of the stack.

• Hence in C#, the stack also works in the same way. Elements are added to the
stack, one on the top of each other. The process of adding an element to the stack
is called a push operation. To remove an element from a stack, you can also
remove the top most element of the stack. This operation is known as pop.

• Let's look at the operations available for the Stack collection in more detail.

• Declaration of the stack

A stack is created with the help of the Stack Data type. The keyword "new" is used to
create an object of a Stack. The object is then assigned to the variable st.

Stack st = new Stack()

• Adding elements to the stack

The push method is used to add an element onto the stack. The general syntax of the
statement is given below.

Stack.push(element)

• Removing elements from the stack

The pop method is used to remove an element from the stack. The pop operation will
return the topmost element of the stack. The general syntax of the statement is given
below

LSPU SELF-PACED LEARNING MODULE: TECHNOLOGY FOR TEACHING AND LEARNING


Republic of the Philippines
Laguna State Polytechnic University
ISO 9001:2015 Certified
Province of Laguna
Level I Institutionally Accredited
Stack.pop()

• Count

This property is used to get the number of items in the Stack. Below is the general syntax
of this statement.

Stack.count

• Contains

This method is used to see if an element is present in the Stack. Below is the general
syntax of this statement. The statement will return true if the element exists, else it will
return the value false.

Stack.Contains(element)

• Now let's see this working at a code level. All of the below-mentioned code will be
written to our Console application. The code will be written to our Program.cs file.

• In the below program, we will write the code to see how we can use the above-
mentioned methods.

using System;

using System.Collections;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace DemoApplication

class Program

static void Main(string[] args)

Stack st = new Stack();

st.Push(1);

st.Push(2);

st.Push(3);

LSPU SELF-PACED LEARNING MODULE: TECHNOLOGY FOR TEACHING AND LEARNING


Republic of the Philippines
Laguna State Polytechnic University
ISO 9001:2015 Certified
Province of Laguna
Level I Institutionally Accredited
foreach (Object obj in st)

Console.WriteLine(obj);

Console.WriteLine(); Console.WriteLine();

Console.WriteLine("The number of elements in the stack " +st.Count);

Console.WriteLine("Does the stack contain the elements 3 "+st.Contains(3));

Console.ReadKey();

1.The first step is used to declare the Stack. Here we are declaring "st" as a variable to hold
the elements of our stack.

2.Next, we add 3 elements to our stack. Each element is added via the Push method.

3.Now since the stack elements cannot be accessed via the index position like the array
list, we need to use a different approach to display the elements of the stack. The Object
(obj) is a temporary variable, which is declared for holding each element of the stack. We
then use the foreach statement to go through each element of the stack. For each stack
element, the value is assigned to the obj variable. We then use the Console.Writeline
command to display the value to the console.

4.We are using the Count property (st.count) to get the number of items in the stack. This
property will return a number. We then display this value to the console.

5.We then use the Contains method to see if the value of 3 is present in our stack. This will
return either a true or false value. We then display this return value to the console.

If the above code is entered properly and the program is run the following output will be
displayed.

• Example 2

• Now let's look at the "remove" functionality. We will see the code required to
remove the topmost element from the stack.

using System;

using System.Collections;

using System.Collections.Generic;

LSPU SELF-PACED LEARNING MODULE: TECHNOLOGY FOR TEACHING AND LEARNING


Republic of the Philippines
Laguna State Polytechnic University
ISO 9001:2015 Certified
Province of Laguna
Level I Institutionally Accredited
using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace DemoApplication

class Program

static void Main(string[] args)

Stack st = new Stack();

st.Push(1);

st.Push(2);

st.Push(3);

st.Pop();

foreach (Object obj in st)

Console.WriteLine(obj);

Console.ReadKey();

• QUEUES

What is Queue in C#?

The Queue is a special case collection which represents a first in first out concept. Imagine
a queue of people waiting for the bus. Normally, the first person who enters the queue will
be the first person to enter the bus. Similarly, the last person to enter the queue will be
the last person to enter into the bus. Elements are added to the queue, one on the top of

LSPU SELF-PACED LEARNING MODULE: TECHNOLOGY FOR TEACHING AND LEARNING


Republic of the Philippines
Laguna State Polytechnic University
ISO 9001:2015 Certified
Province of Laguna
Level I Institutionally Accredited
each other.

• The process of adding an element to the queue is the enqueuer operation. To


remove an element from a queue, you can use the dequeuer operation. The
operation in queues is similar to stack we saw previously.

• Let's look at the operations available for the Queue collection in more detail.

• Declaration of the Queue

The declaration of a Queue is provided below. A Queue is created with the help of the
Queue Data type. The "new" keyword is used to create an object of a Queue. The object is
then assigned to the variable qt.

Queue qt = new Queue()

• Adding elements to the Queue

The enqueue method is used to add an element onto the queue. The general syntax of the
statement is given below.

Queue.enqueue(element)

• Removing elements from the queue

The dequeue method is used to remove an element from the queue. The dequeue
operation will return the first element of the queue. The general syntax of the statement is
given below

Queue.dequeue()

• Count

This property is used to get the number of items in the queue. Below is the general syntax
of this statement.

Queue.Count

• Contains

This method is used to see if an element is present in the Queue. Below is the general
syntax of this statement. The statement will return true if the element exists, else it will
return the value false.

Queue.Contains(element)

• Now, let's see this working at a code level. All of the below-mentioned code will be
written to our Console application.

• The code will be written to our Program.cs file. In the below program, we will write
the code to see how we can use the above-mentioned methods.

LSPU SELF-PACED LEARNING MODULE: TECHNOLOGY FOR TEACHING AND LEARNING


Republic of the Philippines
Laguna State Polytechnic University
ISO 9001:2015 Certified
Province of Laguna
Level I Institutionally Accredited
• Example

• In this example, we will see how a queue gets created. Next, we will see how to
display the elements of the queue, and use the Count and Contain methods.

using System;

using System.Collections;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace DemoApplication

class Program

static void Main(string[] args)

Queue qt = new Queue();

qt.Enqueue(1);

qt.Enqueue(2);

qt.Enqueue(3);

foreach (Object obj in qt)

Console.WriteLine(obj);

Console.WriteLine(); Console.WriteLine();

Console.WriteLine("The number of elements in the Queue " + qt.Count);

Console.WriteLine("Does the Queue contain " + qt.Contains(3));

Console.ReadKey();

LSPU SELF-PACED LEARNING MODULE: TECHNOLOGY FOR TEACHING AND LEARNING


Republic of the Philippines
Laguna State Polytechnic University
ISO 9001:2015 Certified
Province of Laguna
Level I Institutionally Accredited
}

• Code Explanation

1.The first step is used to declare the Queue. Here we are declaring qt as a variable to hold
the elements of our Queue.

2.Next, we add 3 elements to our Queue. Each element is added via the "enqueue"
method.

3. Now one thing that needs to be noted about Queues is that the elements cannot be
accessed via the index position like the array list. We need to use a different approach to
display the elements of the Queue. So here's how we go about displaying the elements of
a queue.

• We first declare a temporary variable called obj. This will be used to hold each
element of the Queue.

• We then use the foreach statement to go through each element of the Queue.

• For each Queue element, the value is assigned to the obj variable.

• We then use the Console.Writeline command to display the value to the console.

4. We are using the "Count" property to get the number of items in the Queue. This
property will return a number. We then display this value to the console.

5. We then use the "Contains" method to see if the value of 3 is present in our Queue. This
will return either a true or false value. We then display this return value to the console.

If the above code is entered properly and the program is run the following output will be
displayed.

• C# Queue Dequeue

• Now let's look at the remove functionality. We will see the code required to
remove the last element from the queue.

using System;

using System.Collections;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

LSPU SELF-PACED LEARNING MODULE: TECHNOLOGY FOR TEACHING AND LEARNING


Republic of the Philippines
Laguna State Polytechnic University
ISO 9001:2015 Certified
Province of Laguna
Level I Institutionally Accredited
namespace DemoApplication

class Program

static void Main(string[] args)

Queue qt = new Queue();

qt.Enqueue(1);

qt.Enqueue(2);

qt.Enqueue(3);

qt.Dequeue();

foreach (Object obj in qt)

Console.WriteLine(obj);

Console.ReadKey();

• Code Explanation

1. Here we just issue the "dequeue" method, which is used to remove an element from the
queue. This method will remove the first element of the queue.

• If the above code is entered properly and the program is run the following output
will be displayed.

• SUMMARY

• A Stack is based on the last in first out concept. The operation of adding an
element to the stack is called the push operation. The operation of removing an
element to the stack is called the pop operation.

• A Queue is based on the first in first out concept. The operation of adding an
element to the queue is called the enqueue operation. The operation of removing

LSPU SELF-PACED LEARNING MODULE: TECHNOLOGY FOR TEACHING AND LEARNING


Republic of the Philippines
Laguna State Polytechnic University
ISO 9001:2015 Certified
Province of Laguna
Level I Institutionally Accredited
an element to the queue is called the dequeue operation.

WHAT IS OOP?

Object Oriented Programming (OOP) is one of the most popular programming languages.
This article is an introduction to Object Oriented Programming (OOP) and how to
implement OOP in C# including abstraction, encapsulation, inheritance and
polymorphism. 

FEATURES

Object Oriented Programming (OOP) is a programming model where programs are


organized around objects and data rather than action and logic.

OOP allows decomposition of a problem into a number of entities called objects and then
builds data and functions around these objects. 

1. The software is divided into a number of small units called objects. The data and
functions are built around these objects.
2. The data of the objects can be accessed only by the functions associated with that
object.
3. The functions of one object can access the functions of another object.

OOP has the following important features.

CLASSES

A class is the core of any modern Object Oriented Programming language such as C#.
In OOP languages it is mandatory to create a class for representing data.
A class is a blueprint of an object that contains variables for storing data and functions to
perform operations on the data.

LSPU SELF-PACED LEARNING MODULE: TECHNOLOGY FOR TEACHING AND LEARNING


Republic of the Philippines
Laguna State Polytechnic University
ISO 9001:2015 Certified
Province of Laguna
Level I Institutionally Accredited

A class will not occupy any memory space and hence it is only a logical representation of
data.
To create a class, you simply use the keyword "class" followed by the class name:
class Employee  
{  
  
}

Object

Objects are the basic run-time entities of an object oriented system. They may represent a
person, a place or any item that the program must handle.

"An object is a software bundle of related variable and methods."

"An object is an instance of a class" 

 A class will not occupy any memory space. Hence to work with the data
represented by the class you must create a variable for the class, that is called an
object.
 When an object is created using the new operator, memory is allocated for the
class in the heap, the object is called an instance and its starting address will be

LSPU SELF-PACED LEARNING MODULE: TECHNOLOGY FOR TEACHING AND LEARNING


Republic of the Philippines
Laguna State Polytechnic University
ISO 9001:2015 Certified
Province of Laguna
Level I Institutionally Accredited
stored in the object in stack memory.

 When an object is created without the new operator, memory will not be
allocated in the heap, in other words an instance will not be created and the
object in the stack contains the value null.
 When an object contains null, then it is not possible to access the members of the
class using that object.

class Employee  
{  
  
}

Syntax to create an object of class Employee:


Employee objEmp = new Employee();

All the programming languages supporting Object Oriented Programming will be


supporting these three main concepts, 
 Encapsulation
 Inheritance
 Polymorphism

Abstraction

Abstraction is "To represent the essential feature without representing the background
details."

Abstraction lets you focus on what the object does instead of how it does it.

Abstraction provides you a generalized view of your classes or objects by providing


relevant information.

Abstraction is the process of hiding the working style of an object, and showing the
information of an object in an understandable manner.

 Real-world Example of Abstraction


 Suppose you have an object Mobile Phone.

Suppose you have 3 mobile phones as in the following:

LSPU SELF-PACED LEARNING MODULE: TECHNOLOGY FOR TEACHING AND LEARNING


Republic of the Philippines
Laguna State Polytechnic University
ISO 9001:2015 Certified
Province of Laguna
Level I Institutionally Accredited
Nokia 1400 (Features: Calling, SMS)
Nokia 2700 (Features: Calling, SMS, FM Radio, MP3, Camera)
Black Berry (Features:Calling, SMS, FM Radio, MP3, Camera, Video Recording,
Reading Emails)

Abstract information (necessary and common information) for the object "Mobile
Phone" is that it makes a call to any number and can send SMS.

 So that, for a mobile phone object you will have the abstract class as in the
following,
abstract class MobilePhone {  
    public void Calling();  
    public void SendSMS();  
}  
public class Nokia1400: MobilePhone {}  
public class Nokia2700: MobilePhone {  
    public void FMRadio();  
    public void MP3();  
    public void Camera();  
}  
public class BlackBerry: MobilePhone {  
    public void FMRadio();  
    public void MP3();  
    public void Camera();  
    public void Recording();  
    public void ReadAndSendEmails();  

 Abstraction means putting all the variables and methods in a class that are
necessary.
 For example: Abstract class and abstract method.
 Abstraction is a common thing.

LSPU SELF-PACED LEARNING MODULE: TECHNOLOGY FOR TEACHING AND LEARNING


Republic of the Philippines
Laguna State Polytechnic University
ISO 9001:2015 Certified
Province of Laguna
Level I Institutionally Accredited

Example

 If somebody in your college tells you to fill in an application form, you will provide
your details, like name, address, date of birth, which semester, percentage you
have etcetera.
 If some doctor gives you an application to fill in the details, you will provide the
details, like name, address, date of birth, blood group, height and weight.
 See in the preceding example what is in common?
 Age, name and address, so you can create a class that consists of the common
data. That is called an abstract class.
 That class is not complete and it can be inherited by other classes.

Encapsulation
 Wrapping up a data member and a method together into a single unit (in other
words class) is called Encapsulation.
 Encapsulation is like enclosing in a capsule. That is enclosing the related
operations and data related to an object into that object.

Encapsulation is like your bag in which you can keep your pen, book etcetera. It
means this is the property of encapsulating members and functions.
class Bag {  
    book;  
    pen;  
    ReadBook();  

Encapsulation means hiding the internal details of an object, in other words how an object
does something.

Encapsulation prevents clients from seeing it’s inside view, where the behavior of the
abstraction is implemented.

Encapsulation is a technique used to protect the information in an object from another


object.

Hide the data for security such as making the variables private, and expose the property to
access the private data that will be public. So, when you access the property you can
validate the data and set it.

LSPU SELF-PACED LEARNING MODULE: TECHNOLOGY FOR TEACHING AND LEARNING


Republic of the Philippines
Laguna State Polytechnic University
ISO 9001:2015 Certified
Province of Laguna
Level I Institutionally Accredited

Example
class Demo {  
    private int _mark;  
    public int Mark {  
     get {  
            return _mark;  
        }  
        set {  
            if (_mark > 0) _mark = value;  
            else _mark = 0;  
        }  
    }  
}

Real-world Example of Encapsulation

 Let's use as an example Mobile Phones and Mobile Phone Manufacturers.


 Suppose you are a Mobile Phone Manufacturer and you have designed and
developed a Mobile Phone design (a class). Now by using machinery you are
manufacturing Mobile Phones (objects) for selling, when you sell your Mobile
Phone the user only learns how to use the Mobile Phone but not how the Mobile
Phone works.

This means that you are creating the class with functions and with objects
(capsules) of which you are making available the functionality of your class by that
object and without the interference in the original class.

Example 2

 TV operation
 It is encapsulated with a cover and we can operate it with a remote and there is no
need to open the TV to change the channel.
Here everything is private except the remote, so that anyone can access the
remote to operate and change the things in the TV.

LSPU SELF-PACED LEARNING MODULE: TECHNOLOGY FOR TEACHING AND LEARNING


Republic of the Philippines
Laguna State Polytechnic University
ISO 9001:2015 Certified
Province of Laguna
Level I Institutionally Accredited

Inheritance

 When a class includes a property of another class it is known as inheritance.


 Inheritance is a process of object reusability.
 For example, a child includes the properties of its parents.

public class ParentClass {  

    public ParentClass() {  


        Console.WriteLine("Parent Constructor.");  
    }  
    public void print() {  
        Console.WriteLine("I'm a Parent Class.");  
    }  
}  
public class ChildClass: ParentClass {  
    public ChildClass() {  
        Console.WriteLine("Child Constructor.");  
    }  
    public static void Main() {  
        ChildClass child = new ChildClass();  
        child.print();  
    }  

Output
Parent Constructor.
Child Constructor.
I'm a Parent Class.

LSPU SELF-PACED LEARNING MODULE: TECHNOLOGY FOR TEACHING AND LEARNING


Republic of the Philippines
Laguna State Polytechnic University
ISO 9001:2015 Certified
Province of Laguna
Level I Institutionally Accredited

Polymorphism

 Polymorphism means one name, many forms.

Example 1

 Real-world Example of Polymorphism


A teacher behaves students.
A teacher behaves with his/her seniors.
Here the teacher is an object but the attitude is different in different situations.

 Example 2
 A person behaves the son in a house at the same time that the person behaves as
an employee in an office.
 Example 3
 Your mobile phone, one name but many forms:
 As phone
 As camera
 As mp3 player
 As radio

Differences between Abstraction and Encapsulation 

LSPU SELF-PACED LEARNING MODULE: TECHNOLOGY FOR TEACHING AND LEARNING


Republic of the Philippines
Laguna State Polytechnic University
ISO 9001:2015 Certified
Province of Laguna
Level I Institutionally Accredited

Real-world Example

 Use an example of a Mobile Phone


 You have a Mobile Phone; you can dial a number using keypad buttons. You don't
even know how these are working internally. This is called Abstraction. You only
have the information that is necessary to dial a number. But not the internal
working of the mobile.

But how does the Mobile Phone work internally? How are the keypad buttons
connected to the internal circuit? That is called Encapsulation.

Summary
 "Encapsulation is accomplished using classes. Keeping data and methods that
access that data into a single unit."
 "Abstraction is accomplished using an Interface. Just giving the abstract
information about what it can do without specifying the details."
 "Information/Data hiding is accomplished using modifiers by keeping the instance
variables private or protected."

Performance Tasks

LSPU SELF-PACED LEARNING MODULE: TECHNOLOGY FOR TEACHING AND LEARNING


Republic of the Philippines
Laguna State Polytechnic University
ISO 9001:2015 Certified
Province of Laguna
Level I Institutionally Accredited

I. Answer the following questions

1. What OOP?
2. Write the basic concept of OOP?
3. What is class?
4. What is an object?
5. What are manipulators?
6. What is the main difference between a class and an object?
7. What is the default access modifier in a class?
8. What is an accessibility modifier and how many are there in C#?
9. Can “this “keyword is used within a static method?
10. How can you implement multiple inheritance in C#?
11. Are private class members inherited to the derived class?
12. Can you create object of class with private constructor n C#?
13. When do you use abstract class?
14. What are the fundamental principles of OO programming?
15. How does OO simplify development?

Laboratory Activity

1. Write a program of sorting an array. Declare single dimensional array and accept 5 integer values from the user.
Then sort the input in ascending order and display output.
2. Write a program to create two multidimensional arrays of same size. Accept value from user and store them in
first array. Now copy all the elements of first array are second array and print output.

LSPU SELF-PACED LEARNING MODULE: TECHNOLOGY FOR TEACHING AND LEARNING


Republic of the Philippines
Laguna State Polytechnic University
ISO 9001:2015 Certified
Province of Laguna
Level I Institutionally Accredited

Understanding Directed Assessment

Rubric for Evaluating Assignments (PT 2)

Criteria Beginner (0-3) Intermediate (4-7) Skilled (8-10) Weight Score

Prompt Submission  Report is submitted a  Report is submitted  Report is submitted 0. 2


week after the after the schedule on time.
schedule but within the day
Appearance and  Untidy -Numerous  Evident marks of  No marks of
Completeness of marks of erasures erasures erasures
Report  Sheets are not bind,  All sheets are bind  All sheets are bind
crumpled and most together and some together and
parts are missing parts are missing complete 0. 2
 Fonts used was not  Appropriate use of  Appropriate use of
clear fonts fonts
 Most parts are not  Some parts are not  All parts are
complete complete complete, organized
and synchronized

Accuracy of research  Contents gathered are  Most of the contents  Contents gathered 0. 2
contents limited, or are are correct. are all accurate.
imprecise

LSPU SELF-PACED LEARNING MODULE: TECHNOLOGY FOR TEACHING AND LEARNING


Republic of the Philippines
Laguna State Polytechnic University
ISO 9001:2015 Certified
Province of Laguna
Level I Institutionally Accredited
Presentation skills  Voice modulation is not  Voice modulation is  Well-modulated
and time appropriate somewhat voice
management  Delivery is not clear appropriate  Delivery is very
 Topics are poorly  Delivery is satisfactory
designed and presented satisfactory  Topics are well
 Topics are not  Some topics are not designed
documented and well designed  Contents are well
 Contents are partially
0. 2
required contents are not documented and
followed documented and topics are all
 There are members not topics are partially presented
able to present or all presented  All members
members presented but  All members presented within the
exceeded time by > 3 presented but time allotted
minutes exceeded time by 1-3
minutes
Discussion of the  Inappropriate use of  Appropriate choice  Use of rich
topic and answer to words, poor grammar of language language, excellent
the questions and ideas are not clearly  Can express ideas grammar, and ideas
expressed  Discussion was well are expressed 0. 2
 Does not point out versed. precisely
discussion well  Discussion was
clear and accurate.

TOTAL /50

Learning Resources

 https://www.completecsharptutorial.com/basic/array-examples.php
 https://www.c-sharpcorner.com/blogs/how-to-use-queue-and-stacks-in-c-sharp
 https://www.w3schools.com/cs/default.asp

LSPU SELF-PACED LEARNING MODULE: TECHNOLOGY FOR TEACHING AND LEARNING

You might also like