• No results found

“Introduction to Programming With Java”

N/A
N/A
Protected

Academic year: 2023

Share "“Introduction to Programming With Java”"

Copied!
17
0
0

Loading.... (view fulltext now)

Full text

(1)

“Introduction to Programming With Java”

nlp-ai@cse.iitb

Lecture - 6

U

MESH

P

ATIL (umesh@cse.iitb.ac.in)

(2)

nlp-ai@cse.iitb

• ‘switch’ construct

• Introduction to Arrays

Contents for Today’s Lecture

(3)

nlp-ai@cse.iitb

class print_num {

public static void main(String args[]) { int a_number;

a_number = 0;

if(a_number = = 1)

System.out.println("One");

else {

if(a_number = = 2)

System.out.println("Two");

else {

if(a_number = = 3)

System.out.println("Three");

else

System.out.println("No. is out of the range");

} } }

}

‘if-else’ Program

(4)

nlp-ai@cse.iitb

class print_num_switch {

public static void main(String args[]) { int a_number;

a_number = 3;

switch(a_number){

case 1:

System.out.println("One");

break;

case 2:

System.out.println("Two");

break;

case 3:

System.out.println("Three");

break;

default:

System.out.println("No. is out of the range");

} } }

‘switch’ Program

(5)

nlp-ai@cse.iitb switch(expression){

case value1:

statements;

break;

case value2:

statements;

break;

. .

case valueN:

statements;

break;

default:

statements;

}

Syntax

(6)

nlp-ai@cse.iitb

• The case values can be compared only for equality with the switch expression

• The expression must be of type int, char, short, byte

Each case value must be a constant, not a variable

• Duplicate case values are not allowed

• The default part is optional

Note

(7)

nlp-ai@cse.iitb Definition:

An array is a group/collection of variables of the same type that are referred to by a common name and an index

Examples:

Collection of numbers

• Collection of names

• Collection of suffixes

Arrays

(8)

nlp-ai@cse.iitb Array of numbers:

Array of names:

Array of suffixes:

Examples

10 23 863 8 229

ment tion ness ves

Sholay Shaan Shakti

(9)

nlp-ai@cse.iitb

Array is like a pen box with fixed no. of slots of same size.

Analogy

(10)

nlp-ai@cse.iitb Declaration of array variable:

data-type variable-name[];

eg. int marks[];

This will declare an array named ‘marks’ of type ‘int’. But no memory is allocated to the array.

Allocation of memory:

variable-name = new data-type[size];

eg. marks = new int[5];

This will allocate memory of 5 integers to the array ‘marks’

and it can store upto 5 integers in it. ‘new’ is a special operator that allocates memory.

Syntax

(11)

nlp-ai@cse.iitb Accessing elements in the array:

Specific element in the array is accessed by specifying name of the array followed the index of the element. All array

indexes in Java start at zero.

variable-name[index] = value;

eg. marks[0] = 10;

This will assign the value 10 to the 1st element in the array.

And

marks[2] = 863;;

This will assign the value 863 to the 3rd element in the array.

Syntax…

(12)

nlp-ai@cse.iitb STEP 1 : (Declaration)

int marks[];

marks  null

STEP 2: (Memory Allocation) marks = new int[5];

marks 

marks[0] marks[1] marks[2] marks[3] marks[4]

STEP 3: (Accessing Elements) marks[0] = 10;

marks 

marks[0] marks[1] marks[2] marks[3] marks[4]

Example

0 0 0 0 0

10 0 0 0 0

(13)

nlp-ai@cse.iitb class try_array {

public static void main(String args[]) { int marks[];

marks = new int[3];

marks[0] = 10;

marks[1] = 35;

marks[2] = 84;

System.out.println(“Marks obtained by 2nd student=” + marks[1]);

} }

Program

(14)

nlp-ai@cse.iitb

• Arrays can store elements of the same data type. Hence an int array CAN NOT store an element which is not an int.

Though an element of a compatible type can be converted to int and stored into the int array.

eg. marks[2] = (int) 22.5;

This will convert ‘22.5’ into the int part ‘22’ and store it into the 3rd place in the int array ‘marks’.

• Array indexes start from zero. Hence ‘marks[index]’ refers to the (index+1)th element in the array and ‘marks[size-1]

refers to last element in the array.

eg. marks[0] refers to 1st element, marks[1] refers to 2nd element… etc. etc.

Note

(15)

nlp-ai@cse.iitb

Combined declaration & memory allocation:

data-type variable-name[] = new data-type[size];

eg. int marks[] = new int[5];

This will declare an int array ‘marks’ and will also allocate memory of 5 integers to it.

Combined declaration, allocation & assignment:

data-type variable-name[] = {comma-separated values};

eg. int marks[] = {10, 35, 84, 23, 5};

This will declare an int array ‘marks’, will allocate memory of 5 integers to it and will also assign the values as-

marks 

marks[0] marks[1] marks[2] marks[3] marks[4]

Alternative Syntax

10 35 84 23 5

(16)

nlp-ai@cse.iitb

1. Write a program that uses two arrays of size 5. One string array for storing names of students and one int array for storing marks obtained by the students. Assign values to the elements in the arrays. Print names and marks of

students who have scored more than 35 marks.

2. Write a program same as the above, but don’t use

numbers (like 0, 1, 2…etc) to access the elements in the array. Declare an int variable ‘i’ and use it as an index into the arrays.

Assignment

(17)

nlp-ai@cse.iitb

Thank you…

End

References

Related documents

Percentage of countries with DRR integrated in climate change adaptation frameworks, mechanisms and processes Disaster risk reduction is an integral objective of

✓ Microprocessor is a Programmable, Clock driven, Register based, Electronic device that reads instruction from a storage device, takes the data from input unit and process the

INDEPENDENT MONITORING BOARD | RECOMMENDED ACTION.. Rationale: Repeatedly, in field surveys, from front-line polio workers, and in meeting after meeting, it has become clear that

3.6., which is a Smith Predictor based NCS (SPNCS). The plant model is considered in the minor feedback loop with a virtual time delay to compensate for networked induced

The garments are presented in the fashion shows by fashion designers in the presence of the media, which plays the role of giving coverage to the styles exhibited, thus

Dennis falls in love with the American girl, Aimee Thanatogenos, who works at 'Whispering Glades' as a cosmetician.. Aimee too, gets attracted

Under Article 253 of the Constitution of India, the Parliament and the Union of India have the power to implement treaties and can even interfere in the powers of the state

II that there is a non-trivial fixed point 共 FP 兲 of the renormalization group 共 RG 兲 in the (a,h) plane; the system is gapless on a quantum critical line of points which flow to