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

Topic 9: Constructor: DEFINITION: A Constructor Is A Special Method That Is Used To Initialize A

The document discusses constructors in object-oriented programming. It defines a constructor as a special method used to initialize objects. Constructors can initialize objects to default or desired values. Parameterized constructors take parameters and allow initializing each instance to different values, while default constructors do not take parameters and initialize all instances the same way. The document provides an example of a parameterized constructor in C# that initializes object fields based on passed parameter values.

Uploaded by

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

Topic 9: Constructor: DEFINITION: A Constructor Is A Special Method That Is Used To Initialize A

The document discusses constructors in object-oriented programming. It defines a constructor as a special method used to initialize objects. Constructors can initialize objects to default or desired values. Parameterized constructors take parameters and allow initializing each instance to different values, while default constructors do not take parameters and initialize all instances the same way. The document provides an example of a parameterized constructor in C# that initializes object fields based on passed parameter values.

Uploaded by

nelufer
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

TOPIC 9: CONSTRUCTOR

DEFINITION: A constructor is a special method that is used to initialize a


newly created object and is called just after the memory is allocated for the
object. It can be used to initialize the objects to desired values or default
values at the time of object creation. It is not mandatory for the coder to
write a constructor for a class.
Parameterized Constructor
A constructor with at least one parameter is called a parametrized
constructor. The advantage of a parametrized constructor is that you can
initialize each instance of the class to different values.

using System;

namespace Constructor

class paraconstrctor

public int a, b;

public paraconstrctor(int x, int y) // decalaring Paremetrized Constru


ctor with ing x,y parameter

a = x;

b = y;

class MainClass

{
static void Main()

paraconstrctor v = new paraconstrctor(100, 175); / Creating obj


ect of Parameterized Constructor and ing values

Console.WriteLine("-----------
parameterized constructor example by vithal wadje---------------");

Console.WriteLine("\t");

Console.WriteLine("value of a=" + v.a );

Console.WriteLine("value of b=" + v.b);

Console.Read();

Default Constructor
A constructor without any parameters is called a default constructor; in
other words this type of constructor does not take parameters. The
drawback of a default constructor is that every instance of the class will
be initialized to the same values and it is not possible to initialize each
instance of the class to different values. The default
constructor initializes:

1. All numeric fields in the class to zero.


2. All string and object fields to null.
TOPIC 10: RECURSION
DEFINITION: Recursion in computer science is a method of solving a
problem where the solution depends on solutions to smaller instances of
the same problem (as opposed to iteration).[1] The approach can be applied
to many types of problems, and recursion is one of the central ideas of
computer science.
Most computer programming languages support recursion by
allowing a function to call itself from within its own code. Some functional
programming languages do not define any looping constructs but rely
solely on recursion to repeatedly call code. Computability theory proves
that these recursive-only languages are Turing complete; they are as
computationally powerful as Turing complete imperative languages,
meaning they can solve the same kinds of problems as imperative
languages even without iterative control structures such as while and for.

public int sum(int n) public int sumR(int n)


{ {
int res = 0; if(n == 1)
for(int i = 1; i = n; i++) return 1;
res = res + i; else
return n + sumR(n-1);
return res; }
}
TOPIC 12: EXCEPTIONAL HANDLING
DEFINITION: Exception handling is the process of responding to the occurrence,
during computation, of exceptions – anomalous or exceptional conditions requiring
special processing – often changing the normal flow of program execution. It is provided
by specialized programming language constructs, computer hardware mechanisms
like interrupts or operating system IPC facilities like signals.
In general, an exception breaks the normal flow of execution and executes a pre-
registered exception handler. The details of how this is done depends on whether it is a
hardware or software exception and how the software exception is implemented. Some
exceptions, especially hardware ones, may be handled so gracefully that execution can
resume where it was interrupted.
Alternative approaches to exception handling in software are error checking, which
maintains normal program flow with later explicit checks for contingencies reported
using special return values or some auxiliary global variable such as C's errno or
floating point status flags; or input validation to preemptively filter exceptional cases .
Types of Exception in Java
Java defines several types of exceptions that relate to its various class libraries.
Java also allows users to define their own exceptions.
TOPIC 11: ARRAYS
DEFINITION: In computer science, an array data structure, or simply an array, is
a data structure consisting of a collection of elements (values or variables), each
identified by at least one array index or key. An array is stored such that the position of
each element can be computed from its index tuple by a mathematical
formula.[1][2][3] The simplest type of data structure is a linear array, also called one-
dimensional array.
For example, an array of 10 32-bit integer variables, with indices 0 through 9, may be
stored as 10 words at memory addresses 2000, 2004, 2008, ... 2036, so that the
element with index i has the address 2000 + 4 × i.[4]
The term array is often used to mean array data type, a kind of data
type provided by most high-level programming languages that consists of a collection of
values or variables that can be selected by one or more indices computed at run-time.
Array types are often implemented by array structures; however, in some languages
they may be implemented by hash tables, linked lists, search trees, or other data
structures.
The term is also used, especially in the description of algorithms, to
mean associative array or "abstract array", a theoretical computer science model
(an abstract data type or ADT) intended to capture the essential properties of arrays.
Arrays have better cache locality as compared to linked lists.
TOPIC 13: SEARCHING
LINEAR SEARCH
DEFINITION: In computer science, linear search or sequential search is a
method for finding an element within a list. It sequentially checks each
element of the list until a match is found or the whole list has been
searched.[1]
Linear search runs in at worst linear time and makes at
most n comparisons, where n is the length of the list. If each element is
equally likely to be searched, then linear search has an average case
of n/2 comparisons, but the average case can be affected if the search
probabilities for each element vary. Linear search is rarely practical
because other search algorithms and schemes, such as the binary search
algorithm and hash tables, allow significantly faster searching for all but
short lists.[2]

Linear search

Class Search algorithm

Worst-case performance O(n)

Best-case performance O(1)

Average performance O(n)

Worst-case space complexity O(1) iterative


public class MyLinearSearch {

public static int linerSearch(int[] arr, int key){

int size = arr.length;


for(int i=0;i<size;i++){
if(arr[i] == key){
return i;
}
}
return -1;
}

public static void main(String a[]){

int[] arr1= {23,45,21,55,234,1,34,90};


int searchKey = 34;
System.out.println("Key "+searchKey+" found at index:
"+linerSearch(arr1, searchKey));
int[] arr2= {123,445,421,595,2134,41,304,190};
searchKey = 421;
System.out.println("Key "+searchKey+" found at index:
"+linerSearch(arr2, searchKey));
}
}

BINARY SEARCH
DEFINITION: Binary Search is a searching technique used to search an
element in a sorted array. In this article, we will learn about how to
implement Binary Search in PHP using iterative and recursive way. Given a
array of numbers, we need to search for the presence of element x in the
array using Binary Search.
TOPIC 14: SORTING

BUBBLE SORT
DEFINITION: Bubble sort, sometimes referred to as sinking sort, is a
simple sorting algorithm that repeatedly steps through the list, compares
adjacent pairs and swaps them if they are in the wrong order. The pass
through the list is repeated until the list is sorted. The algorithm, which is
a comparison sort, is named for the way smaller or larger elements
"bubble" to the top of the list. Although the algorithm is simple, it is too slow
and impractical for most problems even when compared to insertion
sort.[2] Bubble sort can be practical if the input is in mostly sorted order with
some out-of-order elements nearly in position.
int main()
{
int a[50],n,i,j,temp;
printf("Enter the size of array: ");
scanf("%d",&n);
printf("Enter the array elements: ");

for(i=0;i<n;++i)
scanf("%d",&a[i]);

for(i=1;i<n;++i)
for(j=0;j<(n-i);++j)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}

printf("\nArray after sorting: ");


for(i=0;i<n;++i)
printf("%d ",a[i]);

return 0;
}
SELECTION SORT
DEFINITION: In computer science, selection sort is a sorting algorithm,
specifically an in-place comparison sort. It has O(n2) time complexity,
making it inefficient on large lists, and generally performs worse than the
similar insertion sort. Selection sort is noted for its simplicity, and it has
performance advantages over more complicated algorithms in certain
situations, particularly where auxiliary memory is limited.
The algorithm divides the input list into two parts: the sublist of items
already sorted, which is built up from left to right at the front (left) of the list,
and the sublist of items remaining to be sorted that occupy the rest of the
list. Initially, the sorted sublist is empty and the unsorted sublist is the entire
input list. The algorithm proceeds by finding the smallest (or largest,
depending on sorting order) element in the unsorted sublist, exchanging
(swapping) it with the leftmost unsorted element (putting it in sorted order),
and moving the sublist boundaries one element to the right.
// Logic of selection sort algorithm
for(i=0;i<count;i++){
for(j=i+1;j<count;j++){
if(number[i]>number[j]){
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
}

printf("Sorted elements: ");


for(i=0;i<count;i++)
printf(" %d",number[i]);

return 0;
}
BIBLIOGRAPHY
WWW.google.com
WWW.wikipedia.com
WWW.javapoint.com
Computer science with java- Sumitra Arora
INDEX
SL NO TOPIC PAGE NUMBER

1.

2.

3.

4.

5.

6.

7.

8.

9.

10.

11.

12.

13.

14.

15.

You might also like