introduction to computer programming

11
Ihsan Ayyub Qazi ([email protected]) Introduction to Computer Programming Recitation 2 Ihsan Ayyub Qazi

Upload: tale

Post on 08-Jan-2016

30 views

Category:

Documents


0 download

DESCRIPTION

Introduction to Computer Programming. Recitation 2 Ihsan Ayyub Qazi. Agenda. Practice Practice Practice. What is the output of the program?. public class Test { public static void main (String args []) { int x = 10, y = 3; double t = 3.0, z = 0.0; z = x/3; - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Introduction to Computer Programming

Ihsan Ayyub Qazi ([email protected])

Introduction to Computer Programming

Recitation 2Ihsan Ayyub Qazi

Page 2: Introduction to Computer Programming

Ihsan Ayyub Qazi ([email protected])

Agenda

Practice Practice Practice

Page 3: Introduction to Computer Programming

Ihsan Ayyub Qazi ([email protected])

What is the output of the program?

public class Test{ public static void main (String args []) { int x = 10, y = 3; double t = 3.0, z = 0.0; z = x/3; System.out.println(z); z = x/(double)3; System.out.println(z); z = (double)(x/3); System.out.println(z); z = x/t; System.out.println(z); }}

Page 4: Introduction to Computer Programming

Ihsan Ayyub Qazi ([email protected])

Output?public class Test{ public static void main(String args []) { char letter = 'A'; int val = 0; double d = 0.0; byte b = 0; val = letter; System.out.println(val);

d = letter; System.out.println(d); b = letter; System.out.println(b); }}

Recall “narrowing conversion”To avoid a compilaton errorexplicit cast must be done !!

Why?The compiler wants to makesure that the programmer

knows what he/she is doingand didn’t make a mistake

Page 5: Introduction to Computer Programming

Ihsan Ayyub Qazi ([email protected])

Output?public class Test{ public static void main(String args []) { char letter_1 = 65, letter_2 = 66; char temp; temp = letter_1; letter_1 = letter_2; letter_2 = temp; System.out.println(letter_1); System.out.println(letter_1); System.out.println((int) letter_1); System.out.println((int) letter_1); }}

Page 6: Introduction to Computer Programming

Ihsan Ayyub Qazi ([email protected])

Output?

public class Test{ public static void main(String args []) { int age = 0, height = 0, sum = 0; 15 = age; 20 = height; sum = age + height; System.out.println(sum); }}

The operand to the left of the assignment “=“ operator

always has to have a variable !Why?

Page 7: Introduction to Computer Programming

Ihsan Ayyub Qazi ([email protected])

Output?

public class Test{ public static void main(String args []) { int x = 0, 34 = 0;

34 = 13; System.out.println(34); }}

How about x=34?

Page 8: Introduction to Computer Programming

Ihsan Ayyub Qazi ([email protected])

Output?

public class Test{ public static void main(String args []) { int age = 21; short height = 78, sum = 0; sum = age + height; System.out.println(sum); }}

Page 9: Introduction to Computer Programming

Ihsan Ayyub Qazi ([email protected])

Output?public class Test{ public static void main(String args []) { String str = "This is a class on computer programming"; String abc = "ABC"; char c, d; int val; System.out.println( str.toUpperCase() ); val = abc.charAt(0) + 1; c = (char) val; System.out.println(c); }}

Page 10: Introduction to Computer Programming

Ihsan Ayyub Qazi ([email protected])

Output? (Challenge Question) public class Test{ public static void main(String args []) { int x = 10, y = 3; double t = 3.0, z = 0.0; z = (double)(x/3); System.out.println(z); z = (char)x/3; System.out.println(z); }}

Page 11: Introduction to Computer Programming

Ihsan Ayyub Qazi ([email protected])

THANKS !!