DSA Lec 2 Spring 2022
DSA Lec 2 Spring 2022
DSA Lec 2 Spring 2022
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
•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
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
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};
// create an array
int[] age = {12, 4, 5};
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;
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,
COPY RIGHT: ASST. PROF SYED FAISAL ALI Tuesday, March 29, 2022 24
3D ARRAY
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
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
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