SlideShare a Scribd company logo
School of Computing
COMP 155: Object Oriented Programming
Dr Ziaul Hossain
Lec 2: JAVA constructs
Date School of Computing Slide 2
Input From Keyboard
• We use the Scanner class for that purpose
• Have to import java.util.Scanner
• nextInt() function for Integer type
• nextDouble() for Double type
• nextLine() for a Line (a string before you press Enter)
Date School of Computing Slide 3
Sample code for Integer Input
1 import java.util.Scanner;
2
3 public class InputTest {
4 public static void main(String[] args) {
5 Scanner input = new Scanner(System.in);
6
7 System.out.println("Please enter an integer");
8 int userGivenInteger = input.nextInt();
9
10 System.out.println("You entered: " +
userGivenInteger);
11 }
12 }
Date School of Computing Slide 4
Input from Keyboard
• Explained more in the Lab 2 manual
– Please refer to it
Date School of Computing Slide 5
Type Conversion
• Java does automatic type conversion
– Types are compatible
– Destination type is larger size than source type
– byte -> short -> char -> int -> long -> float -> double
– Also known as Widening Casting
int i = 20; char ch = ‘Q’;
double d = i; long l = ch;
Date School of Computing Slide 6
Explicit Casting
• Manual or explicit casting
– Converts larger sized data types into smaller ones
– May loose value or precision
– Also known as Narrowing Casting
Output:
Date School of Computing Slide 7
Auto type-conversion in expression
Arithmetic operators
convert byte or char or
short into integers
solution
Can not assign an int to byte
Date School of Computing Slide 8
Type promotion
• int & char -> int
• int & float -> float
… …
• Example of multiple types together
(int + byte) * (float – double)
int * double
double
Date School of Computing Slide 9
Demonstration of println()
• println() or print() function are capable of printing
multiple variables together
• Some examples you should try to find the output
and understand the reasoning behind:
int a=20, b=30;
System.out.println(“a”);
…..println(a);
…..println(a+b);
……println(“a”+b);
……println(“Sum=” + a+b);
……
Date School of Computing Slide 10
Arithmetic Operators
Date School of Computing Slide 11
Relational Operator
Date School of Computing Slide 12
Boolean Logical Operators
Date School of Computing Slide 13
Operator Precedence
Date School of Computing Slide 14
If-else example
int i=10, j=15, k=70;
int a=0, b=30, c=40, d=20;
if(i == d/2) {
if(j < 20) a = b;
if(k > 100) c = d;
else a = c;
}
else a = d;
What is the value of a?
Date School of Computing Slide 15
While Loop
int i=100, j=200;
while ( ++i < --j) ;
System.out.println(
“Midpoint: ” + i);
Another Example
Date School of Computing Slide 16
An example of for loop and if condition
Date School of Computing Slide 17
Another for Loop Example - condition
boolean done = false;
int i = 0;
for( ; !done; ) {
System.out.println("i is " + i);
if(i == 10) done = true;
i++;
}
Date School of Computing Slide 18
break with Label
Date School of Computing Slide 19
Ternary Operator - ? :
if ( a > b )
max = a;
else
max = b;
max = (a>b)? a : b;
max= (a>b)? ( (a>c)?a:c ) : ( (b>c)?b:c ) ;
Date School of Computing Slide 20
Arrays
• In C, just declaration was enough
int arr [10];
• In Java, declaration and allocation with new
keyword required
int months[];
months = new int [12]; int months[] = new int [12];
Date School of Computing Slide 21
Array Example
Trying to access an array with negative index
or index value more than the size, a Java run-
time error will occur
Date School of Computing Slide 22
2-D arrays
All rows contain same columns Each row contain different columns
Date School of Computing Slide 23
Different array declaration syntax
Date School of Computing Slide 24
A new format in for loop
int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int sum = 0;
for(int i=0; i < 10; i++) sum += nums[i];
int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int sum = 0;
for(int x: nums) sum += x;
New Format allowed in Java
* You can use the break statement if want to finish the loop early
Date School of Computing Slide 25
Explanation of for loop new format
public class JavaApplication3 {
public static void main(String[] args) {
int nums[] = { 1, 2, 3, 4, 5, 6,
7, 8, 9, 10 };
for(int x : nums) {
System.out.print(x + " ");
x = x * 10; // no effect on nums
System.out.println(x);
}
}
}
1 10
2 20
3 30
4 40
5 50
6 60
7 70
8 80
9 90
10 100
Output:
Date School of Computing Slide 26
For-each loop in 2D array
public class JavaApplication3 {
public static void main(String[] args) {
int sum = 0;
int nums[][] = new int[3][5];
for(int i = 0; i < 3; i++)
for(int j=0; j < 5; j++)
nums[i][j] = (i+1)*(j+1);
for(int x[] : nums) {
for(int y : x) {
System.out.println("Value is: " + y);
sum += y;
}
}
System.out.println("Summation: " + sum);
}
}
Date School of Computing Slide 27
• End of Lecture 2

More Related Content

PPTX
20.1 Java working with abstraction
PPTX
Chapter i(introduction to java)
PPTX
Presentation1 computer shaan
PPTX
05. Java Loops Methods and Classes
PDF
LECTURE 3 LOOPS, ARRAYS.pdf
PDF
LECTURE 3 LOOPS, ARRAYS.pdf
PPTX
Java Programs
PPT
Parameters
20.1 Java working with abstraction
Chapter i(introduction to java)
Presentation1 computer shaan
05. Java Loops Methods and Classes
LECTURE 3 LOOPS, ARRAYS.pdf
LECTURE 3 LOOPS, ARRAYS.pdf
Java Programs
Parameters

Similar to Week_02_Lec_ Java Intro continueed..pptx (20)

PDF
Java Lab Manual
PDF
JavaProgrammingManual
PPTX
Java introduction
PPTX
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_17-Feb-2021_L8-...
PPTX
Java Foundations: Data Types and Type Conversion
PPTX
Ch2 Elementry Programmin as per gtu oop.pptx
DOC
Java final lab
DOCX
703497334-ICSE-Class-9-Computer-Applications-Sample-Question-Papers.docx
PDF
Object Oriented Solved Practice Programs C++ Exams
PPTX
07. Arrays
PDF
Numerical tour in the Python eco-system: Python, NumPy, scikit-learn
PPTX
05A_Java_in yemen adenProgramming_IT.pptx
DOCX
JAVAPGMS.docx
PPTX
Java fundamentals
PPT
Whats new in_csharp4
PPT
00_Introduction to Java.ppt
DOCX
Java file
DOCX
Java file
PPT
Introduction to Java Programming Part 2
PDF
Java Question-Bank-Class-8.pdf
Java Lab Manual
JavaProgrammingManual
Java introduction
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_17-Feb-2021_L8-...
Java Foundations: Data Types and Type Conversion
Ch2 Elementry Programmin as per gtu oop.pptx
Java final lab
703497334-ICSE-Class-9-Computer-Applications-Sample-Question-Papers.docx
Object Oriented Solved Practice Programs C++ Exams
07. Arrays
Numerical tour in the Python eco-system: Python, NumPy, scikit-learn
05A_Java_in yemen adenProgramming_IT.pptx
JAVAPGMS.docx
Java fundamentals
Whats new in_csharp4
00_Introduction to Java.ppt
Java file
Java file
Introduction to Java Programming Part 2
Java Question-Bank-Class-8.pdf
Ad

Recently uploaded (20)

PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
Construction Project Organization Group 2.pptx
PPTX
Sustainable Sites - Green Building Construction
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
Well-logging-methods_new................
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPT
Mechanical Engineering MATERIALS Selection
PPTX
additive manufacturing of ss316l using mig welding
DOCX
573137875-Attendance-Management-System-original
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
Geodesy 1.pptx...............................................
bas. eng. economics group 4 presentation 1.pptx
UNIT 4 Total Quality Management .pptx
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Construction Project Organization Group 2.pptx
Sustainable Sites - Green Building Construction
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Well-logging-methods_new................
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Model Code of Practice - Construction Work - 21102022 .pdf
Mechanical Engineering MATERIALS Selection
additive manufacturing of ss316l using mig welding
573137875-Attendance-Management-System-original
Foundation to blockchain - A guide to Blockchain Tech
Geodesy 1.pptx...............................................
Ad

Week_02_Lec_ Java Intro continueed..pptx

  • 1. School of Computing COMP 155: Object Oriented Programming Dr Ziaul Hossain Lec 2: JAVA constructs
  • 2. Date School of Computing Slide 2 Input From Keyboard • We use the Scanner class for that purpose • Have to import java.util.Scanner • nextInt() function for Integer type • nextDouble() for Double type • nextLine() for a Line (a string before you press Enter)
  • 3. Date School of Computing Slide 3 Sample code for Integer Input 1 import java.util.Scanner; 2 3 public class InputTest { 4 public static void main(String[] args) { 5 Scanner input = new Scanner(System.in); 6 7 System.out.println("Please enter an integer"); 8 int userGivenInteger = input.nextInt(); 9 10 System.out.println("You entered: " + userGivenInteger); 11 } 12 }
  • 4. Date School of Computing Slide 4 Input from Keyboard • Explained more in the Lab 2 manual – Please refer to it
  • 5. Date School of Computing Slide 5 Type Conversion • Java does automatic type conversion – Types are compatible – Destination type is larger size than source type – byte -> short -> char -> int -> long -> float -> double – Also known as Widening Casting int i = 20; char ch = ‘Q’; double d = i; long l = ch;
  • 6. Date School of Computing Slide 6 Explicit Casting • Manual or explicit casting – Converts larger sized data types into smaller ones – May loose value or precision – Also known as Narrowing Casting Output:
  • 7. Date School of Computing Slide 7 Auto type-conversion in expression Arithmetic operators convert byte or char or short into integers solution Can not assign an int to byte
  • 8. Date School of Computing Slide 8 Type promotion • int & char -> int • int & float -> float … … • Example of multiple types together (int + byte) * (float – double) int * double double
  • 9. Date School of Computing Slide 9 Demonstration of println() • println() or print() function are capable of printing multiple variables together • Some examples you should try to find the output and understand the reasoning behind: int a=20, b=30; System.out.println(“a”); …..println(a); …..println(a+b); ……println(“a”+b); ……println(“Sum=” + a+b); ……
  • 10. Date School of Computing Slide 10 Arithmetic Operators
  • 11. Date School of Computing Slide 11 Relational Operator
  • 12. Date School of Computing Slide 12 Boolean Logical Operators
  • 13. Date School of Computing Slide 13 Operator Precedence
  • 14. Date School of Computing Slide 14 If-else example int i=10, j=15, k=70; int a=0, b=30, c=40, d=20; if(i == d/2) { if(j < 20) a = b; if(k > 100) c = d; else a = c; } else a = d; What is the value of a?
  • 15. Date School of Computing Slide 15 While Loop int i=100, j=200; while ( ++i < --j) ; System.out.println( “Midpoint: ” + i); Another Example
  • 16. Date School of Computing Slide 16 An example of for loop and if condition
  • 17. Date School of Computing Slide 17 Another for Loop Example - condition boolean done = false; int i = 0; for( ; !done; ) { System.out.println("i is " + i); if(i == 10) done = true; i++; }
  • 18. Date School of Computing Slide 18 break with Label
  • 19. Date School of Computing Slide 19 Ternary Operator - ? : if ( a > b ) max = a; else max = b; max = (a>b)? a : b; max= (a>b)? ( (a>c)?a:c ) : ( (b>c)?b:c ) ;
  • 20. Date School of Computing Slide 20 Arrays • In C, just declaration was enough int arr [10]; • In Java, declaration and allocation with new keyword required int months[]; months = new int [12]; int months[] = new int [12];
  • 21. Date School of Computing Slide 21 Array Example Trying to access an array with negative index or index value more than the size, a Java run- time error will occur
  • 22. Date School of Computing Slide 22 2-D arrays All rows contain same columns Each row contain different columns
  • 23. Date School of Computing Slide 23 Different array declaration syntax
  • 24. Date School of Computing Slide 24 A new format in for loop int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int sum = 0; for(int i=0; i < 10; i++) sum += nums[i]; int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int sum = 0; for(int x: nums) sum += x; New Format allowed in Java * You can use the break statement if want to finish the loop early
  • 25. Date School of Computing Slide 25 Explanation of for loop new format public class JavaApplication3 { public static void main(String[] args) { int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; for(int x : nums) { System.out.print(x + " "); x = x * 10; // no effect on nums System.out.println(x); } } } 1 10 2 20 3 30 4 40 5 50 6 60 7 70 8 80 9 90 10 100 Output:
  • 26. Date School of Computing Slide 26 For-each loop in 2D array public class JavaApplication3 { public static void main(String[] args) { int sum = 0; int nums[][] = new int[3][5]; for(int i = 0; i < 3; i++) for(int j=0; j < 5; j++) nums[i][j] = (i+1)*(j+1); for(int x[] : nums) { for(int y : x) { System.out.println("Value is: " + y); sum += y; } } System.out.println("Summation: " + sum); } }
  • 27. Date School of Computing Slide 27 • End of Lecture 2