DSA Lec 2 Spring 2022

Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

CS211 – DATA STRUCTURES AND ALGORITHMS

LECTURE 3: JAVA PROGRAMMING


WEEK -02

Asst. Prof. Syed Faisal Ali


[email protected]
Computer Science
(Software Engineering)
Sec A
SPRING 2022
FIRST PROGRAM IN JAVA

COPY RIGHT: ASST. PROF SYED FAISAL ALI Tuesday, March 29, 2022 2
PRINTING A LINE OF TEXT WITH MULTIPLE STATEMENTS

COPY RIGHT: ASST. PROF SYED FAISAL ALI Tuesday, March 29, 2022 3
PRINTING IN MULTIPLE LINES

COPY RIGHT: ASST. PROF SYED FAISAL ALI Tuesday, March 29, 2022 4
ESCAPE SEQUENCES

COPY RIGHT: ASST. PROF SYED FAISAL ALI Tuesday, March 29, 2022 5
USING FORMAT SPECIFIERS FOR PRINTING

COPY RIGHT: ASST. PROF SYED FAISAL ALI Tuesday, March 29, 2022 6
PROGRAM TO ADD TWO INTEGERS

COPY RIGHT: ASST. PROF SYED FAISAL ALI Tuesday, March 29, 2022 7
DECLARING VARIABLES AND INPUT FROM USER

COPY RIGHT: ASST. PROF SYED FAISAL ALI Tuesday, March 29, 2022 8
ARITHMETIC

COPY RIGHT: ASST. PROF SYED FAISAL ALI Tuesday, March 29, 2022 9
RULES OF OPERATOR PRECEDENCE

 Java applies the operators in arithmetic expressions in a precise sequence determined by the rules of
operator precedence, which are generally the same as those followed in algebra:
1. Multiplication, division and remainder operations are applied first. If an expression contains several such
operations, they’re applied from left to right. Multiplication, division and remainder operators have the same level
of precedence.
2. Addition and subtraction operations are applied next. If an expression contains several such operations, the
operators are applied from left to right. Addition and subtraction operators have the same level of precedence.
 These rules enable Java to apply operators in the correct order. When we say that operators are applied from
left to right, we’re referring to their associativity. Some operators associate from right to left.

COPY RIGHT: ASST. PROF SYED FAISAL ALI Tuesday, March 29, 2022 10
OPERATOR PRECEDENCE

COPY RIGHT: ASST. PROF SYED FAISAL ALI Tuesday, March 29, 2022 11
EQUALITY AND RELATIONAL OPERATORS

COPY RIGHT: ASST. PROF SYED FAISAL ALI Tuesday, March 29, 2022 12
COMPARE INTEGERS USING IF STATEMENT AND RELATIONAL
OPERATORS

COPY RIGHT: ASST. PROF SYED FAISAL ALI Tuesday, March 29, 2022 13
PRECEDENCE AND ASSOCIATIVITY OF OPERATORS

COPY RIGHT: ASST. PROF SYED FAISAL ALI Tuesday, March 29, 2022 14
WRITE A PROGRAM TO CALCULATE BMI (BODY MASS INDEX)

 Use the following formulas according to the input from the patient.
 Also display the following information from the department of health.
 So the user can evaluate his / her BMI.

COPY RIGHT: ASST. PROF SYED FAISAL ALI Tuesday, March 29, 2022 15
CAR POOL SAVING CALCULATOR
 (Car-Pool Savings Calculator) Research several car-pooling websites. Create an application
that calculates your daily driving cost, so that you can estimate how much money could be saved by car
pooling, which also has other advantages such as reducing carbon emissions and reducing traffic
congestion. The application should input the following information and display the user’s cost per day of
driving to work:
a) Total miles driven per day.
b) Cost per gallon of gasoline.
c) Average miles per gallon.
d) Parking fees per day.
e) Tolls per day.
COPY RIGHT: ASST. PROF SYED FAISAL ALI Tuesday, March 29, 2022 16
ARRAY
COLLECTION OF SIMILAR DATA TYPES

COPY RIGHT: ASST. PROF SYED FAISAL ALI Tuesday, March 29, 2022 17
ARRAY

 An Array is a collection of similar types of data.


 For examples if I want to store the names of 28 students of my Data Structures & Algorithms class
then I can create an array of the string type that can store 28 names.
String[] std_Name = new String[28];
 Here, the above array cannot store more than 28 names. The number of values in a Java array is always
fixed
dataType[] arrayName;

•dataType - it can be primitive data types like int, char, double, byte, etc. or Java objects
•arrayName - it is an identifier

COPY RIGHT: ASST. PROF SYED FAISAL ALI Tuesday, March 29, 2022 18
ARRAY IN JAVA

 Here data is an array that can hold values of type double.

double[] data

// declare an array
double[] data;
// allocate memory
data = new double[10];
// declare ad allocate memory
double[] data = new double[10];
COPY RIGHT: ASST. PROF SYED FAISAL ALI Tuesday, March 29, 2022 19
DECLARE AND INITIALIZE ARRAY IN JAVA

//declare and initialize and array


int[] age = {12, 4, 5, 2, 5}; Memory Allocations according to data type
12 4 5 2 5
// declare an array age[0] age[1] age[2]age[3] age[4]
int[] age = new int[5];
// initialize array
age[0] = 12;
age[1] = 4;
age[2] = 5;

COPY RIGHT: ASST. PROF SYED FAISAL ALI Tuesday, March 29, 2022 20
HOW TO ACCESS ELEMENTS OF AN ARRAY IN JAVA?

 We can access the element of an array using the index number. Here is the syntax for accessing
elements of an array. class Test_Array {
public static void main(String[] args) {
// access array elements
array[index] // create an array
int[] age = {12, 4, 5, 2, 5};

// access each array elements


System.out.println("Accessing Elements of Array:");
System.out.println("First Element: " + age[0]);
System.out.println("Second Element: " + age[1]);
System.out.println("Third Element: " + age[2]);
System.out.println("Fourth Element: " + age[3]);
COPY RIGHT: ASST. PROF SYED FAISAL ALI
System.out.println("Fifth Element: " + age[4]); Tuesday, March 29, 2022 21
}
}
ACCESSING VIA FOR LOOP
class Test_Array {
public static void main(String[] args) {

// create an array
int[] age = {12, 4, 5};

// loop through the array


// using for loop
System.out.println("Using for Loop:");
for(int i = 0; i < age.length; i++) {
System.out.println(age[i]);
}}}

COPY RIGHT: ASST. PROF SYED FAISAL ALI Tuesday, March 29, 2022 22
WRITE A PROGRAM TO COMPUTE SUM AND AVERAGE OF ARRAY
class Test_Array { // get the total number of elements
public static void main(String[] args) { int arrayLength = numbers.length;

int[] numbers = {2, -9, 0, 5, 12, -25, 22, 9, 8, 12};


// calculate the average
int sum = 0;
// convert the average from int to double
Double average;
average = ((double)sum / (double)arrayLength);

// access all elements using for each loop


// add each element in sum System.out.println("Sum = " + sum);
for (int number: numbers) { System.out.println("Average = " + average);
sum += number; }
} }

COPY RIGHT: ASST. PROF SYED FAISAL ALI Tuesday, March 29, 2022 23
JAVA MULTIDIMENSIONAL ARRAY

 A multidimensional array is an array of arrays. Each element of a multidimensional array is an array itself. For
example,
int[][] Array_2D = new int[3][4];
Here, we have created a multidimensional array named Array_2D. It is a 2-dimensional array, that can hold a
maximum of 12 elements,

Col 1 Col 2 Col 3 Col 4


Row 1 Array_2D[0][0] Array_2D[0][1] Array_2D[0][2] Array_2D[0][3]
Row 2 Array_2D[1][0] Array_2D[1][1] Array_2D[1][2] Array_2D[1][3]
Row 3 Array_2D[2][0] Array_2D[2][1] Array_2D[2][2] Array_2D[2][3]

COPY RIGHT: ASST. PROF SYED FAISAL ALI Tuesday, March 29, 2022 24
3D ARRAY

 String[][][] data = new String[3][4][2];

Col 1 Col 2 Col 3 Col 4


Row 1 Col 1
Array_2D[0][0] Col 2
Array_2D[0][1] Col 3
Array_2D[0][2] Col 4
Array_2D[0][3]
Row Row
1 2 Array_2D[0][0] Array_2D[0][1]
Array_2D[1][0] Array_2D[0][2]
Array_2D[1][1] Array_2D[0][3]
Array_2D[1][2] Array_2D[1][3]
Row Row
2 3 Array_2D[1][0] Array_2D[1][1]
Array_2D[2][0] Array_2D[1][2]
Array_2D[2][1] Array_2D[1][3]
Array_2D[2][2] Array_2D[2][3]
Row 3 Array_2D[2][0] Array_2D[2][1] Array_2D[2][2] Array_2D[2][3]

COPY RIGHT: ASST. PROF SYED FAISAL ALI Tuesday, March 29, 2022 25
INITIALIZATION OF 3D ARRAY

// test is a 3d array
int[][][] test = {
{
{1, -2, 3},
{2, 3, 4}
},
{
{-4, -5, 6, 9},
{1},
{2, 3}
}
};

COPY RIGHT: ASST. PROF SYED FAISAL ALI Tuesday, March 29, 2022 26
2-DIMENSIONAL ARRAY

class MultidimensionalArray {
public static void main(String[] args) {
// create a 2d array
int[][] a = {
{1, 2, 3},
{4, 5, 6, 9}, Length of row 1: 3
{7}, Length of row 2: 4
}; Length of row 3: 1
// calculate the length of each row
System.out.println("Length of row 1: " + a[0].length);
System.out.println("Length of row 2: " + a[1].length);
System.out.println("Length of row 3: " + a[2].length);
}}

COPY RIGHT: ASST. PROF SYED FAISAL ALI Tuesday, March 29, 2022 27
PRINT ALL ELEMENTS OF 2D ARRAY USING LOOP
class MultidimensionalArray {
public static void main(String[] args) {
1
int[][] a = { -2
{1, -2, 3}, 3
{-4, -5, 6, 9}, -4
{7}, -5
6
};
9
7
for (int i = 0; i < a.length; ++i) {
for(int j = 0; j < a[i].length; ++j) {
System.out.println(a[i][j]);
} } } }

COPY RIGHT: ASST. PROF SYED FAISAL ALI Tuesday, March 29, 2022 28
WRITE A JAVA PROGRAM TO FIND THE INDEX OF AN ARRAY
ELEMENT

public class Practice_Prog 3 {


public static int findIndex (int[] my_array, int t) {
if (my_array == null) return -1;
int len = my_array.length;
int i = 0;
return -1;
while (i < len) {
}
if (my_array[i] == t) return i; public static void main(String[] args) {
else i=i+1; int[] my_array = {25, 14, 56, 15, 36, 56, 77, 18, 29, 49};
System.out.println("Index position of 25 is: " + findIndex(my_array, 25));
} System.out.println("Index position of 77 is: " + findIndex(my_array, 77));
}
COPY RIGHT: ASST. PROF SYED FAISAL ALI } Tuesday, March 29, 2022 29
WRITE A PROGRAM TO GENERATE A TABLE

 You can use creative steps to show how your table can work.

COPY RIGHT: ASST. PROF SYED FAISAL ALI Tuesday, March 29, 2022 30
ASSIGNMENT #2

 Read chapter 2 and the video will be uploaded to you.


 You need to make notes as previous and do practice for the questions that will be uploaded to you on MS teams.

COPY RIGHT: ASST. PROF SYED FAISAL ALI Tuesday, March 29, 2022 31
END OF LECTURE

COPY RIGHT: ASST. PROF SYED FAISAL ALI Tuesday, March 29, 2022 32

You might also like