0% found this document useful (0 votes)
32 views15 pages

CJP Unit-3

Java unit 3

Uploaded by

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

CJP Unit-3

Java unit 3

Uploaded by

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

Object Oriented Programming -I (3140705)

Unit-03
Methods and Arrays

Computer Engineering Department


Darshan Institute of Engineering & Technology, Rajkot
[email protected]
9624822202
 Outline
Looping

 Array
 Method
Array
 An array is a collection of similar type of elements that have contiguous memory location and
shares a common name.
 Syntax : data_type variable_name[] = new type[size_of_array];
Example : int a[] = new int[10];

35 13 28 106 35
a 42 5 83 97 14
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]

 The data_type specifies the type of the elements that can be stored in an array, like int, float, char etc...
 The size_of_array indicates the maximum number of elements that can be stores inside the array.
 In the example, data type of an array is int and maximum elements that can be stored in an array are 10.
 Important point about Java array.
 An array is derived datatype.
 An array is dynamically allocated.
 The individual elements of an array is refereed by their index/subscript value.
 The subscript for an array always begins with 0.
Prof. Arjun V. Bala #3140705 (OOP-I)  Unit 03 – Methods and Arrays 3
One-Dimensional Array
 An array using one subscript to represent the list of elements is called one dimensional array.
 A One-dimensional array is essentially a list of like-typed variables.
 Array declaration: type var-name[];
Example: int student_marks[];
 Above example will represent array with no value (null).
 To link student_marks with actual array of integers, we must allocate one using new keyword.
Example: int student_marks[] = new int[20];

Prof. Arjun V. Bala #3140705 (OOP-I)  Unit 03 – Methods and Arrays 4


Example (Array)
public class ArrayDemo{
public static void main(String[] args) {
int a[]; // or int[] a
// till now it is null as it does not assigned any memory

a = new int[5]; // here we actually create an array


a[0] = 5;
a[1] = 8;
a[2] = 15;
a[3] = 84;
a[4] = 53;

/* in java we use length property to determine the length


* of an array, unlike c where we used sizeof function */
for (int i = 0; i < a.length; i++) {
System.out.println("a["+i+"]="+a[i]);
}
}
}

Prof. Arjun V. Bala #3140705 (OOP-I)  Unit 03 – Methods and Arrays 5


Multi-Dimensional Array
 In java, multidimensional array is actually array of arrays.
 Example: int runPerOver[][] = new int[3][6];
First Over (a[0]) 4 0 1 3 6 1
a[0][0] a[0][1] a[0][2] a[0][3] a[0][4] a[0][5]

Second Over (a[1]) 1 1 0 6 0 4


a[1][0] a[1][1] a[1][2] a[1][3] a[1][4] a[1][5]

Third Over (a[2]) 2 1 1 0 1 1


a[2][0] a[2][1] a[2][2] a[2][3] a[2][4] a[2][5]
 length field:
 If we use length field with multidimensional array, it will return length of first dimension.
 Here, if runPerOver.length is accessed it will return 3
 Also if runPerOver[0].length is accessed it will be 6

Prof. Arjun V. Bala #3140705 (OOP-I)  Unit 03 – Methods and Arrays 6


Multi-Dimensional Array (Example)
Scanner s = new Scanner(System.in);
int runPerOver[][] = new int[3][6];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 6; j++) {
System.out.print("Enter Run taken" +
" in Over numner " + (i + 1) +
" and Ball number " + (j + 1) + " = ");
runPerOver[i][j] = s.nextInt();
}
}
int totalRun = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 6; j++) {
totalRun += runPerOver[i][j];
}
}
double average = totalRun / (double) runPerOver.length;
System.out.println("Total Run = " + totalRun);
System.out.println("Average per over = " + average);

Prof. Arjun V. Bala #3140705 (OOP-I)  Unit 03 – Methods and Arrays 7


Multi-Dimensional Array (Cont.)
 manually allocate different size:
int runPerOver[][] = new int[3][];
runPerOver[0] = new int[6];
runPerOver[1] = new int[7];
runPerOver[2] = new int[6];
 initialization:
int runPerOver[][] = {
{0,4,2,1,0,6},
{1,-1,4,1,2,4,0},
{6,4,1,0,2,2},
}
Note : here to specify extra runs (Wide, No Ball etc.. ) negative values are used

Prof. Arjun V. Bala #3140705 (OOP-I)  Unit 03 – Methods and Arrays 8


Searching in Array
 Searching is the process of looking for a specific element in an array. for example, discovering
whether a certain element is included in the array.
 Searching is a common task in computer programming. Many algorithms and data structures are
devoted to searching.
 We will discuss two commonly used approaches,
 Linear Search: The linear search approach compares the key element key sequentially with each element in the
array. It continues to do so until the key matches an element in the array or the array is exhausted without a
match being found.
 Binary Search: The binary search first compares the key with the element in the middle of the array. Consider
the following three cases:
 If the key is less than the middle element, you need to continue to search for the key only in the first half of the array.
 If the key is equal to the middle element, the search ends with a match.
 If the key is greater than the middle element, you need to continue to search for the key only in the second half of the
array.
Note: Array should be sorted in ascending order if we want to use Binary Search.

Prof. Arjun V. Bala #3140705 (OOP-I)  Unit 03 – Methods and Arrays 9


Linear Search
import java.util.Scanner;
public class LinearSearchDemo{
public static void main(String[] args){
int[] myArray = {5,3,6,8,4,6,2,8,9,11};
Scanner sc = new Scanner(System.in);
System.out.println("Enter number to search");
int searchNumber = sc.nextInt();
sc.close();
boolean flag = false;
for(int i=0;i<myArray.length;i++){
if(myArray[i]==searchNumber){
System.out.println("Found at = "+i);
flag = true;
break;
}
}
if(!flag){
System.out.println("Number does not exist in array");
}
}
}
Prof. Arjun V. Bala #3140705 (OOP-I)  Unit 03 – Methods and Arrays 10
Binary Search (Animation)

Prof. Arjun V. Bala #3140705 (OOP-I)  Unit 03 – Methods and Arrays 11


Binary Search
import java.util.Scanner; while(high>=low) {
public class BinarySearchDemo { int mid = (high + low) / 2 ;
if(searchNumber < myArray[mid]) {
public static void main(String[] args) { high = mid - 1;
}
int[] myArray else if(searchNumber == myArray[mid]) {
= {2,3,5,6,8,9,11,18,29,65}; System.out.println("Found at = "+mid);
isFound = true;
Scanner sc = new Scanner(System.in); break;
}
System.out.println else {
("Enter number to search"); low = mid + 1;
int searchNumber = sc.nextInt(); }
sc.close(); }

int low = 0; if(!isFound) {


int high = myArray.length - 1; System.out.println
boolean isFound = false; ("Number does not exist in array");
}
}
}
Prof. Arjun V. Bala #3140705 (OOP-I)  Unit 03 – Methods and Arrays 12
Sorting Array
 Sorting, like searching, is a common task in computer programming. Many different algorithms
have been developed for sorting.
 There are many sorting techniques available, we are going to explore selection sort.
 Selection sort
 finds the smallest number in the list and swaps it with the first element.
 It then finds the smallest number remaining and swaps it with the second element, and so on, until only a
single number remains.

Prof. Arjun V. Bala #3140705 (OOP-I)  Unit 03 – Methods and Arrays 13


Selection Sort (Example)
int a[] = { 5, 2, 9, 3, 4, 1, 8, 6, 7 };
for (int i = 0; i < a.length - 1; i++) {
// Find the minimum in the list[i..a.length-1]
int currentMin = a[i];
int currentMinIndex = i;
for (int j = i + 1; j < a.length; j++) {
if (currentMin > a[j]) {
currentMin = a[j];
currentMinIndex = j;
}
}
// Swap a[i] with a[currentMinIndex] if necessary
if (currentMinIndex != i) {
a[currentMinIndex] = a[i];
a[i] = currentMin;
}
}
for(int temp: a) { // this is foreach loop
System.out.print(temp + ", ");
}

Prof. Arjun V. Bala #3140705 (OOP-I)  Unit 03 – Methods and Arrays 14


Methods
 We will cover all the topics related to methods in the next Unit

Prof. Arjun V. Bala #3140705 (OOP-I)  Unit 03 – Methods and Arrays 15

You might also like