13
“Introduction to Programming With Java” [email protected] Lecture – 5 UMESH PATIL ([email protected])

“Introduction to Programming With Java” [email protected] Lecture – 5 U MESH P ATIL ([email protected])

Embed Size (px)

Citation preview

Page 1: “Introduction to Programming With Java” nlp-ai@cse.iitb Lecture – 5 U MESH P ATIL (umesh@cse.iitb.ac.in)

“Introduction to Programming With Java”

[email protected]

Lecture – 5

UMESH PATIL ([email protected])

Page 2: “Introduction to Programming With Java” nlp-ai@cse.iitb Lecture – 5 U MESH P ATIL (umesh@cse.iitb.ac.in)

[email protected]

• Quick recap of the syllabus covered so far.

• Solutions to previous assignments.

• Some programing practice.

• Introduction to Arrays.

Contents for Today’s Lecture

Page 3: “Introduction to Programming With Java” nlp-ai@cse.iitb Lecture – 5 U MESH P ATIL (umesh@cse.iitb.ac.in)

[email protected]

Structure of Java Program:

class class-name { public static void main(String args[]) {

statements;

}}

Printing messages to the screen:

System.out.println(“Hello World”);System.out.print(“Hello World”);

Recap

Page 4: “Introduction to Programming With Java” nlp-ai@cse.iitb Lecture – 5 U MESH P ATIL (umesh@cse.iitb.ac.in)

[email protected]

Data Types:Numbers – int (whole no.s), double (no.s with decimal point)

eg. int i = 23; double d = 33.23

Character – chareg. char c = ‘a’;

String – Stringeg. String s = “Hi”;

Boolean – booleaneg. boolean b = false;

Variable declaration:data-type variable-name

eg. int i = 23;

Recap…

Page 5: “Introduction to Programming With Java” nlp-ai@cse.iitb Lecture – 5 U MESH P ATIL (umesh@cse.iitb.ac.in)

[email protected]

Expression: An expression is a combination of literals (like 100), operators (like +), variables and parentheses used to calculate a value. eg. 2 + 6 / (2 – 9)

Operators:Arithmetic – eg. ‘-’, ‘+’, ‘/’, ‘++’ etc.Relational –eg. ‘<’, ‘>=’, ‘!=’ etc.Conditional –eg. ‘&&’ (AND), ‘||’ (OR)

Control constructs like‘if’ and ‘if else’:

Recap…

Page 6: “Introduction to Programming With Java” nlp-ai@cse.iitb Lecture – 5 U MESH P ATIL (umesh@cse.iitb.ac.in)

[email protected]

Write a program that averages the synsets created for three months April, May and June. Declare and initialize variable to the synset entered for each month. Compute the average and write out the results, something like this:Synsets Entered for April: 12 Synsets Entered for May : 14 Synsets Entered for June: 8 Average Synset Entered: 11.333333

Assignment

Page 7: “Introduction to Programming With Java” nlp-ai@cse.iitb Lecture – 5 U MESH P ATIL (umesh@cse.iitb.ac.in)

[email protected]

class synset { public static void main(String args[]) { int april = 12; int may = 14; int june = 8; double avg; avg = ( april + may + june ) / 3.0;

System.out.println("Synsets Entered for April " + april); System.out.println("Synsets Entered for May " + may); System.out.println("Synsets Entered for June " + june); System.out.println("Average Synset Entered " + avg); }}

Solution

Page 8: “Introduction to Programming With Java” nlp-ai@cse.iitb Lecture – 5 U MESH P ATIL (umesh@cse.iitb.ac.in)

[email protected]

int a_number=1; (range is 1 to 5 both including)

Print the value in a_number in word with and without using equality (= =) operator. For example it should print “Four” if a_number contains 4.

Assignment

Page 9: “Introduction to Programming With Java” nlp-ai@cse.iitb Lecture – 5 U MESH P ATIL (umesh@cse.iitb.ac.in)

[email protected]

class assign1 { public static void main(String args[]) { int a_number; a_number = 5;

if(a_number == 1) System.out.println("One");

if(a_number == 2) System.out.println("Two");

if(a_number == 3) System.out.println("Three");

if(a_number == 4) System.out.println("Four");

if(a_number == 5) System.out.println("Five"); }

}

Solution

Page 10: “Introduction to Programming With Java” nlp-ai@cse.iitb Lecture – 5 U MESH P ATIL (umesh@cse.iitb.ac.in)

[email protected]

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

Page 11: “Introduction to Programming With Java” nlp-ai@cse.iitb Lecture – 5 U MESH P ATIL (umesh@cse.iitb.ac.in)

[email protected]

Array of numbers:

Array of names:

Array of suffixes:

Examples

10 23 863 8 229

ment tion ness ves

Sholay Shaan Shakti

Page 12: “Introduction to Programming With Java” nlp-ai@cse.iitb Lecture – 5 U MESH P ATIL (umesh@cse.iitb.ac.in)

[email protected]

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

Analogy

Page 13: “Introduction to Programming With Java” nlp-ai@cse.iitb Lecture – 5 U MESH P ATIL (umesh@cse.iitb.ac.in)

[email protected]

Thank you…

End