SlideShare a Scribd company logo
19Z305OBJECT
ORIENTED
PROGRAMMING
Unit 2
Overview
PART
-
1
• Characteristics
of Java
• Data types,
Variables,
Operators
• Control
Statements,
Arrays
PART
-
2
• Classes
• Methods,
Constructors
• Inheritance
• Abstract class
12/1/2020 Vani Kandhasamy,PSG Tech 2
Characteristics
of Java
 Simple
 Secure
 Portable
 Object-oriented
 Robust
 Multithreaded
 Architecture-neutral
 Interpreted & High performance
 Distributed
 Dynamic
12/1/2020 Vani Kandhasamy,PSG Tech 3
12/1/2020 Vani Kandhasamy,PSG Tech 4
Datatypes,Variables,
Operators
Part 1 – Chapter 3 & 4
12/1/2020 Vani Kandhasamy,PSG Tech 5
DataTypes
 Kinds of values that can be stored and manipulated.
 Java - StronglyTyped Language
 Automatic Widening casting
Boolean: boolean (true or false).
Integers: byte, short, int, long (0, 1, -47)
Floating point numbers: float, double (3.14, 1.0, -2.1)
Characters: char (‘X’, 65)
String: String (“hello”, “example”).
12/1/2020 Vani Kandhasamy,PSG Tech 6
12/1/2020 Vani Kandhasamy,PSG Tech 7
public class Example1 {
public static void main(String[] args) {
int myInt = 9;
double myDouble = myInt; // Automatic casting: int to double
System.out.println(myInt); // Outputs 9
System.out.println(myDouble); // Outputs 9.0
}
}
Variables
 Named location that stores a value of one particular type.
 Syntax: type variable ; (0r) type variable = value;
 Example: String rollNo;
rollNo = “19Z30X”;
(or)
String rollNo = “19Z30X”;
12/1/2020 Vani Kandhasamy,PSG Tech 8
12/1/2020 Vani Kandhasamy,PSG Tech 9
class Example2 {
public static void printSquare(int x){
System.out.println("printSquare x = " + x);
x = x * x;
System.out.println("printSquare x = " + x);
}
public static void main(String[] arguments){
int x = 5;
System.out.println("main x = " + x);
printSquare(x);
System.out.println("main x = " + x);
}
}
Practice1
Open repl.it
Debug me!!!
12/1/2020 Vani Kandhasamy,PSG Tech 10
Operators
 Symbols that perform simple computations
 Type Promotion
 Syntax: var = var op expression; (or) var op= expression;
Arithmetic operators: (+, -, *, /, …)
Assignment operators: (=, +=, -=,…)
Relational operators: (==, !=, >, <, …)
Logical operators: (&&, ||, !)
Bitwise operators: (&, |, ~, …)
12/1/2020 Vani Kandhasamy,PSG Tech 11
12/1/2020 Vani Kandhasamy,PSG Tech 12
class Example3 {
public static void main(String args[]) {
byte b = 42;
char c = 'a';
short s = 1024;
int i = 50000;
float f = 5.67f;
double d = .1234;
double result = (f * b) + (i / c) - (d * s);
System.out.println((f * b) + " + " + (i / c) + " - " + (d * s));
System.out.println("result = " + result);
}
}
Output:
238.14 + 515 - 126.3616
result = 626.7784146484375
Practice2
Open repl.it
Compute distance light travels
12/1/2020 Vani Kandhasamy,PSG Tech 13
ControlStatements,
Arrays
Part 1 – Chapter 3 & 5
12/1/2020 Vani Kandhasamy,PSG Tech 14
12/1/2020 Vani Kandhasamy,PSG Tech 15
Conditional Statements
if (CONDITION) {
STATEMENTS
}
if (CONDITION) {
STATEMENTS
} else {
STATEMENTS
}
if (CONDITION) {
STATEMENTS
} else if (CONDITION) {
STATEMENTS
} else {
STATEMENTS
}
12/1/2020 Vani Kandhasamy,PSG Tech 16
public class Example5 {
public static void main(String args[]) {
int month = 4; // April
String season;
if(month == 12 || month == 1 || month == 2)
season = "Winter";
else if(month == 3 || month == 4 || month == 5)
season = "Spring";
else if(month == 6 || month == 7 || month == 8)
season = "Summer";
else if(month == 9 || month == 10 || month == 11)
season = "Autumn";
else
season = "Bogus Month";
System.out.println("April is in the " + season + ".");
}
}
Selection
Statement
switch (expression) {
case value1:
// statement sequence
break;
case value2:
// statement sequence
break;
...
case valueN :
// statement sequence
break;
default:
// default statement sequence
}
12/1/2020 Vani Kandhasamy,PSG Tech 17
12/1/2020 Vani Kandhasamy,PSG Tech 18
public class Example6 {
public static void main(String args[]) {
String str = "two";
switch(str) {
case "one":
System.out.println("one");
break;
case "two":
System.out.println("two");
break;
case "three":
System.out.println("three");
break;
default:
System.out.println("no match");
break;
}
}
}
Overview
12/1/2020 Vani Kandhasamy,PSG Tech 19
Practice3
Open repl.it
Identify season using switch
12/1/2020 Vani Kandhasamy,PSG Tech 20
2
min
12/1/2020 Vani Kandhasamy,PSG Tech 21
Stretch
Break
12/1/2020 Vani Kandhasamy,PSG Tech 22
What if you want to do it for 200 Rules?
12/1/2020 Vani Kandhasamy,PSG Tech 23
Iterative Statements
while (CONDITION) {
STATEMENTS
}
do {
STATEMENTS
} while (CONDITION);
for(initialization;CONDITION;update){
STATEMENTS
}
while
12/1/2020 Vani Kandhasamy,PSG Tech 24
for
12/1/2020 Vani Kandhasamy,PSG Tech 25
12/1/2020 Vani Kandhasamy,PSG Tech 26
S
12/1/2020 Vani Kandhasamy,PSG Tech 27
r
12/1/2020 Vani Kandhasamy,PSG Tech 28
public class Example8 {
public static void main(String args[]) {
int a, b;
for(a=1, b=4; a<b; a++, b--) {
System.out.println("a = " + a);
System.out.println("b = " + b);
}
}
}
Practice4
Open repl.it
Find whether a number is Prime or
not
12/1/2020 Vani Kandhasamy,PSG Tech 29
Overview
12/1/2020 Vani Kandhasamy,PSG Tech 30
Arrays
Syntax: type var_name[ ] = new type [size];
type[ ] var_name;
Example:
double values[ ] = new double [10]
The index starts at zero and ends at length-1
12/1/2020 Vani Kandhasamy,PSG Tech 31
12/1/2020 Vani Kandhasamy,PSG Tech 32
Declaration &
Instantiation
Initialization
Declaration, Instantiation &
Initialization
12/1/2020 Vani Kandhasamy,PSG Tech 36
public class Example7 {
public static void main(String args[]) {
int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int sum = 0;
for(int x =0; x<nums.length; x++){
System.out.println("Value is: " + x);
sum += nums[x];
}
System.out.println("Summation: " + sum);
}
}
12/1/2020 Vani Kandhasamy,PSG Tech 37
public class Example7 {
public static void main(String args[]) {
int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int sum = 0;
// use for-each style for to display and sum the values
for(int x : nums) {
System.out.println("Value is: " + x);
sum += x;
}
System.out.println("Summation: " + sum);
}
}
Practice5
Open repl.it
Search an array using for-each style
12/1/2020 Vani Kandhasamy,PSG Tech 38
Multi-
dimensional
Array
12/1/2020 Vani Kandhasamy,PSG Tech 39
int twoD[][] = new int [4][5]
int[][] twoD= new int [4][5]
int []twoD[] = new int [4][5]
12/1/2020 Vani Kandhasamy,PSG Tech 40
public class Example9 {
public static void main(String args[]) {
int twoD[][]= new int[4][5];
int i, j, k = 0;
for(i=0; i<4; i++)
for(j=0; j<5; j++) {
twoD[i][j] = k;
k++;
}
for(i=0; i<4; i++) {
for(j=0; j<5; j++)
System.out.print(twoD[i][j] + " ");
System.out.println();
}
}
}
Practice6
Open repl.it
Sum of principle diagonal elements
of a matrix
12/1/2020 Vani Kandhasamy,PSG Tech 41
References
 "Java:The Complete Reference” by Schildt H - Part 1 - Chapter 3 , 4 & 5
 https://www.w3schools.com/java/
12/1/2020 Vani Kandhasamy,PSG Tech 42

More Related Content

PDF
Java Basics - Part2
Vani Kandhasamy
 
PPTX
13. Java text processing
Intro C# Book
 
PPTX
07. Java Array, Set and Maps
Intro C# Book
 
PPTX
16. Java stacks and queues
Intro C# Book
 
PDF
Python Puzzlers
Tendayi Mawushe
 
PDF
Python Cheat Sheet
Muthu Vinayagam
 
PPTX
19. Java data structures algorithms and complexity
Intro C# Book
 
PPTX
20.1 Java working with abstraction
Intro C# Book
 
Java Basics - Part2
Vani Kandhasamy
 
13. Java text processing
Intro C# Book
 
07. Java Array, Set and Maps
Intro C# Book
 
16. Java stacks and queues
Intro C# Book
 
Python Puzzlers
Tendayi Mawushe
 
Python Cheat Sheet
Muthu Vinayagam
 
19. Java data structures algorithms and complexity
Intro C# Book
 
20.1 Java working with abstraction
Intro C# Book
 

What's hot (20)

PPTX
Chapter 22. Lambda Expressions and LINQ
Intro C# Book
 
PPTX
05. Java Loops Methods and Classes
Intro C# Book
 
PPTX
13 Strings and Text Processing
Intro C# Book
 
PDF
Python 2.5 reference card (2009)
gekiaruj
 
PPT
PDBC
Sunil OS
 
PPTX
09. Java Methods
Intro C# Book
 
PPTX
15. Streams Files and Directories
Intro C# Book
 
PPTX
Introduction to julia
岳華 杜
 
PDF
Coding Guidelines - Crafting Clean Code
Ganesh Samarthyam
 
PPTX
20.3 Java encapsulation
Intro C# Book
 
PPTX
03 and 04 .Operators, Expressions, working with the console and conditional s...
Intro C# Book
 
PPTX
07. Arrays
Intro C# Book
 
PPTX
09. Methods
Intro C# Book
 
PDF
Beginners python cheat sheet - Basic knowledge
O T
 
PPT
Chapter 2 Java Methods
Khirulnizam Abd Rahman
 
PDF
.NET 2015: Будущее рядом
Andrey Akinshin
 
PPT
Parameters
James Brotsos
 
PPTX
16. Arrays Lists Stacks Queues
Intro C# Book
 
PPTX
Metaprogramming in julia
岳華 杜
 
PDF
Object Orientation vs Functional Programming in Python
Tendayi Mawushe
 
Chapter 22. Lambda Expressions and LINQ
Intro C# Book
 
05. Java Loops Methods and Classes
Intro C# Book
 
13 Strings and Text Processing
Intro C# Book
 
Python 2.5 reference card (2009)
gekiaruj
 
PDBC
Sunil OS
 
09. Java Methods
Intro C# Book
 
15. Streams Files and Directories
Intro C# Book
 
Introduction to julia
岳華 杜
 
Coding Guidelines - Crafting Clean Code
Ganesh Samarthyam
 
20.3 Java encapsulation
Intro C# Book
 
03 and 04 .Operators, Expressions, working with the console and conditional s...
Intro C# Book
 
07. Arrays
Intro C# Book
 
09. Methods
Intro C# Book
 
Beginners python cheat sheet - Basic knowledge
O T
 
Chapter 2 Java Methods
Khirulnizam Abd Rahman
 
.NET 2015: Будущее рядом
Andrey Akinshin
 
Parameters
James Brotsos
 
16. Arrays Lists Stacks Queues
Intro C# Book
 
Metaprogramming in julia
岳華 杜
 
Object Orientation vs Functional Programming in Python
Tendayi Mawushe
 
Ad

Similar to Java Basics - Part1 (20)

PDF
Informatics Practices (new) solution CBSE 2021, Compartment, improvement ex...
FarhanAhmade
 
PPTX
Static code analysis: what? how? why?
Andrey Karpov
 
PDF
What We Talk About When We Talk About Unit Testing
Kevlin Henney
 
PPTX
Week_02_Lec_ Java Intro continueed..pptx
ibrahemtariq
 
PPTX
Story of static code analyzer development
Andrey Karpov
 
PDF
Sam wd programs
Soumya Behera
 
PPT
ch04-conditional-execution.ppt
Mahyuddin8
 
PPT
00_Introduction to Java.ppt
HongAnhNguyn285885
 
PDF
Fnt software solutions placement paper
fntsofttech
 
PPT
06slide.ppt
RohitNukte
 
PPTX
How to Adopt Modern C++17 into Your C++ Code
Microsoft Tech Community
 
PPTX
How to Adopt Modern C++17 into Your C++ Code
Microsoft Tech Community
 
PPTX
A miało być tak... bez wycieków
Konrad Kokosa
 
PPTX
Ch07-3-sourceCode.pptxhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
VaibhavSrivastav52
 
PDF
FileName EX06_1java Programmer import ja.pdf
actocomputer
 
PDF
Chapter 1 Basic Concepts
Hareem Aslam
 
PPT
Data Structures and Algorithoms 06slide - Methods.ppt
AliciaLee77
 
PPTX
Lambdas puzzler - Peter Lawrey
JAXLondon_Conference
 
PPTX
EVERYTHING ABOUT STATIC CODE ANALYSIS FOR A JAVA PROGRAMMER
Andrey Karpov
 
PPTX
Applying Compiler Techniques to Iterate At Blazing Speed
Pascal-Louis Perez
 
Informatics Practices (new) solution CBSE 2021, Compartment, improvement ex...
FarhanAhmade
 
Static code analysis: what? how? why?
Andrey Karpov
 
What We Talk About When We Talk About Unit Testing
Kevlin Henney
 
Week_02_Lec_ Java Intro continueed..pptx
ibrahemtariq
 
Story of static code analyzer development
Andrey Karpov
 
Sam wd programs
Soumya Behera
 
ch04-conditional-execution.ppt
Mahyuddin8
 
00_Introduction to Java.ppt
HongAnhNguyn285885
 
Fnt software solutions placement paper
fntsofttech
 
06slide.ppt
RohitNukte
 
How to Adopt Modern C++17 into Your C++ Code
Microsoft Tech Community
 
How to Adopt Modern C++17 into Your C++ Code
Microsoft Tech Community
 
A miało być tak... bez wycieków
Konrad Kokosa
 
Ch07-3-sourceCode.pptxhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
VaibhavSrivastav52
 
FileName EX06_1java Programmer import ja.pdf
actocomputer
 
Chapter 1 Basic Concepts
Hareem Aslam
 
Data Structures and Algorithoms 06slide - Methods.ppt
AliciaLee77
 
Lambdas puzzler - Peter Lawrey
JAXLondon_Conference
 
EVERYTHING ABOUT STATIC CODE ANALYSIS FOR A JAVA PROGRAMMER
Andrey Karpov
 
Applying Compiler Techniques to Iterate At Blazing Speed
Pascal-Louis Perez
 
Ad

More from Vani Kandhasamy (10)

PDF
Introduction to OOP
Vani Kandhasamy
 
PDF
Economic network analysis - Part 2
Vani Kandhasamy
 
PDF
Economic network analysis - Part 1
Vani Kandhasamy
 
PDF
Cascading behavior in the networks
Vani Kandhasamy
 
PDF
Community detection-Part2
Vani Kandhasamy
 
PDF
Community detection-Part1
Vani Kandhasamy
 
PDF
Link Analysis
Vani Kandhasamy
 
PDF
Network Models
Vani Kandhasamy
 
PDF
Representing & Measuring networks
Vani Kandhasamy
 
PDF
Cache optimization
Vani Kandhasamy
 
Introduction to OOP
Vani Kandhasamy
 
Economic network analysis - Part 2
Vani Kandhasamy
 
Economic network analysis - Part 1
Vani Kandhasamy
 
Cascading behavior in the networks
Vani Kandhasamy
 
Community detection-Part2
Vani Kandhasamy
 
Community detection-Part1
Vani Kandhasamy
 
Link Analysis
Vani Kandhasamy
 
Network Models
Vani Kandhasamy
 
Representing & Measuring networks
Vani Kandhasamy
 
Cache optimization
Vani Kandhasamy
 

Recently uploaded (20)

PPTX
unit 3a.pptx material management. Chapter of operational management
atisht0104
 
PDF
Chad Ayach - A Versatile Aerospace Professional
Chad Ayach
 
PPT
Lecture in network security and mobile computing
AbdullahOmar704132
 
PPTX
AgentX UiPath Community Webinar series - Delhi
RohitRadhakrishnan8
 
PDF
Unit I Part II.pdf : Security Fundamentals
Dr. Madhuri Jawale
 
PPTX
Civil Engineering Practices_BY Sh.JP Mishra 23.09.pptx
bineetmishra1990
 
PPTX
Inventory management chapter in automation and robotics.
atisht0104
 
PPTX
22PCOAM21 Session 1 Data Management.pptx
Guru Nanak Technical Institutions
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PDF
Introduction to Ship Engine Room Systems.pdf
Mahmoud Moghtaderi
 
PDF
오픈소스 LLM, vLLM으로 Production까지 (Instruct.KR Summer Meetup, 2025)
Hyogeun Oh
 
PDF
top-5-use-cases-for-splunk-security-analytics.pdf
yaghutialireza
 
PPTX
IoT_Smart_Agriculture_Presentations.pptx
poojakumari696707
 
PPTX
EE3303-EM-I 25.7.25 electrical machines.pptx
Nagen87
 
PPTX
Information Retrieval and Extraction - Module 7
premSankar19
 
PDF
Activated Carbon for Water and Wastewater Treatment_ Integration of Adsorptio...
EmilianoRodriguezTll
 
DOCX
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
PDF
Principles of Food Science and Nutritions
Dr. Yogesh Kumar Kosariya
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PDF
Top 10 read articles In Managing Information Technology.pdf
IJMIT JOURNAL
 
unit 3a.pptx material management. Chapter of operational management
atisht0104
 
Chad Ayach - A Versatile Aerospace Professional
Chad Ayach
 
Lecture in network security and mobile computing
AbdullahOmar704132
 
AgentX UiPath Community Webinar series - Delhi
RohitRadhakrishnan8
 
Unit I Part II.pdf : Security Fundamentals
Dr. Madhuri Jawale
 
Civil Engineering Practices_BY Sh.JP Mishra 23.09.pptx
bineetmishra1990
 
Inventory management chapter in automation and robotics.
atisht0104
 
22PCOAM21 Session 1 Data Management.pptx
Guru Nanak Technical Institutions
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
Introduction to Ship Engine Room Systems.pdf
Mahmoud Moghtaderi
 
오픈소스 LLM, vLLM으로 Production까지 (Instruct.KR Summer Meetup, 2025)
Hyogeun Oh
 
top-5-use-cases-for-splunk-security-analytics.pdf
yaghutialireza
 
IoT_Smart_Agriculture_Presentations.pptx
poojakumari696707
 
EE3303-EM-I 25.7.25 electrical machines.pptx
Nagen87
 
Information Retrieval and Extraction - Module 7
premSankar19
 
Activated Carbon for Water and Wastewater Treatment_ Integration of Adsorptio...
EmilianoRodriguezTll
 
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
Principles of Food Science and Nutritions
Dr. Yogesh Kumar Kosariya
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
Top 10 read articles In Managing Information Technology.pdf
IJMIT JOURNAL
 

Java Basics - Part1

  • 2. Overview PART - 1 • Characteristics of Java • Data types, Variables, Operators • Control Statements, Arrays PART - 2 • Classes • Methods, Constructors • Inheritance • Abstract class 12/1/2020 Vani Kandhasamy,PSG Tech 2
  • 3. Characteristics of Java  Simple  Secure  Portable  Object-oriented  Robust  Multithreaded  Architecture-neutral  Interpreted & High performance  Distributed  Dynamic 12/1/2020 Vani Kandhasamy,PSG Tech 3
  • 5. Datatypes,Variables, Operators Part 1 – Chapter 3 & 4 12/1/2020 Vani Kandhasamy,PSG Tech 5
  • 6. DataTypes  Kinds of values that can be stored and manipulated.  Java - StronglyTyped Language  Automatic Widening casting Boolean: boolean (true or false). Integers: byte, short, int, long (0, 1, -47) Floating point numbers: float, double (3.14, 1.0, -2.1) Characters: char (‘X’, 65) String: String (“hello”, “example”). 12/1/2020 Vani Kandhasamy,PSG Tech 6
  • 7. 12/1/2020 Vani Kandhasamy,PSG Tech 7 public class Example1 { public static void main(String[] args) { int myInt = 9; double myDouble = myInt; // Automatic casting: int to double System.out.println(myInt); // Outputs 9 System.out.println(myDouble); // Outputs 9.0 } }
  • 8. Variables  Named location that stores a value of one particular type.  Syntax: type variable ; (0r) type variable = value;  Example: String rollNo; rollNo = “19Z30X”; (or) String rollNo = “19Z30X”; 12/1/2020 Vani Kandhasamy,PSG Tech 8
  • 9. 12/1/2020 Vani Kandhasamy,PSG Tech 9 class Example2 { public static void printSquare(int x){ System.out.println("printSquare x = " + x); x = x * x; System.out.println("printSquare x = " + x); } public static void main(String[] arguments){ int x = 5; System.out.println("main x = " + x); printSquare(x); System.out.println("main x = " + x); } }
  • 10. Practice1 Open repl.it Debug me!!! 12/1/2020 Vani Kandhasamy,PSG Tech 10
  • 11. Operators  Symbols that perform simple computations  Type Promotion  Syntax: var = var op expression; (or) var op= expression; Arithmetic operators: (+, -, *, /, …) Assignment operators: (=, +=, -=,…) Relational operators: (==, !=, >, <, …) Logical operators: (&&, ||, !) Bitwise operators: (&, |, ~, …) 12/1/2020 Vani Kandhasamy,PSG Tech 11
  • 12. 12/1/2020 Vani Kandhasamy,PSG Tech 12 class Example3 { public static void main(String args[]) { byte b = 42; char c = 'a'; short s = 1024; int i = 50000; float f = 5.67f; double d = .1234; double result = (f * b) + (i / c) - (d * s); System.out.println((f * b) + " + " + (i / c) + " - " + (d * s)); System.out.println("result = " + result); } } Output: 238.14 + 515 - 126.3616 result = 626.7784146484375
  • 13. Practice2 Open repl.it Compute distance light travels 12/1/2020 Vani Kandhasamy,PSG Tech 13
  • 14. ControlStatements, Arrays Part 1 – Chapter 3 & 5 12/1/2020 Vani Kandhasamy,PSG Tech 14
  • 15. 12/1/2020 Vani Kandhasamy,PSG Tech 15 Conditional Statements if (CONDITION) { STATEMENTS } if (CONDITION) { STATEMENTS } else { STATEMENTS } if (CONDITION) { STATEMENTS } else if (CONDITION) { STATEMENTS } else { STATEMENTS }
  • 16. 12/1/2020 Vani Kandhasamy,PSG Tech 16 public class Example5 { public static void main(String args[]) { int month = 4; // April String season; if(month == 12 || month == 1 || month == 2) season = "Winter"; else if(month == 3 || month == 4 || month == 5) season = "Spring"; else if(month == 6 || month == 7 || month == 8) season = "Summer"; else if(month == 9 || month == 10 || month == 11) season = "Autumn"; else season = "Bogus Month"; System.out.println("April is in the " + season + "."); } }
  • 17. Selection Statement switch (expression) { case value1: // statement sequence break; case value2: // statement sequence break; ... case valueN : // statement sequence break; default: // default statement sequence } 12/1/2020 Vani Kandhasamy,PSG Tech 17
  • 18. 12/1/2020 Vani Kandhasamy,PSG Tech 18 public class Example6 { public static void main(String args[]) { String str = "two"; switch(str) { case "one": System.out.println("one"); break; case "two": System.out.println("two"); break; case "three": System.out.println("three"); break; default: System.out.println("no match"); break; } } }
  • 20. Practice3 Open repl.it Identify season using switch 12/1/2020 Vani Kandhasamy,PSG Tech 20
  • 21. 2 min 12/1/2020 Vani Kandhasamy,PSG Tech 21 Stretch Break
  • 22. 12/1/2020 Vani Kandhasamy,PSG Tech 22 What if you want to do it for 200 Rules?
  • 23. 12/1/2020 Vani Kandhasamy,PSG Tech 23 Iterative Statements while (CONDITION) { STATEMENTS } do { STATEMENTS } while (CONDITION); for(initialization;CONDITION;update){ STATEMENTS }
  • 28. 12/1/2020 Vani Kandhasamy,PSG Tech 28 public class Example8 { public static void main(String args[]) { int a, b; for(a=1, b=4; a<b; a++, b--) { System.out.println("a = " + a); System.out.println("b = " + b); } } }
  • 29. Practice4 Open repl.it Find whether a number is Prime or not 12/1/2020 Vani Kandhasamy,PSG Tech 29
  • 31. Arrays Syntax: type var_name[ ] = new type [size]; type[ ] var_name; Example: double values[ ] = new double [10] The index starts at zero and ends at length-1 12/1/2020 Vani Kandhasamy,PSG Tech 31
  • 32. 12/1/2020 Vani Kandhasamy,PSG Tech 32 Declaration & Instantiation Initialization Declaration, Instantiation & Initialization
  • 33. 12/1/2020 Vani Kandhasamy,PSG Tech 36 public class Example7 { public static void main(String args[]) { int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int sum = 0; for(int x =0; x<nums.length; x++){ System.out.println("Value is: " + x); sum += nums[x]; } System.out.println("Summation: " + sum); } }
  • 34. 12/1/2020 Vani Kandhasamy,PSG Tech 37 public class Example7 { public static void main(String args[]) { int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int sum = 0; // use for-each style for to display and sum the values for(int x : nums) { System.out.println("Value is: " + x); sum += x; } System.out.println("Summation: " + sum); } }
  • 35. Practice5 Open repl.it Search an array using for-each style 12/1/2020 Vani Kandhasamy,PSG Tech 38
  • 36. Multi- dimensional Array 12/1/2020 Vani Kandhasamy,PSG Tech 39 int twoD[][] = new int [4][5] int[][] twoD= new int [4][5] int []twoD[] = new int [4][5]
  • 37. 12/1/2020 Vani Kandhasamy,PSG Tech 40 public class Example9 { public static void main(String args[]) { int twoD[][]= new int[4][5]; int i, j, k = 0; for(i=0; i<4; i++) for(j=0; j<5; j++) { twoD[i][j] = k; k++; } for(i=0; i<4; i++) { for(j=0; j<5; j++) System.out.print(twoD[i][j] + " "); System.out.println(); } } }
  • 38. Practice6 Open repl.it Sum of principle diagonal elements of a matrix 12/1/2020 Vani Kandhasamy,PSG Tech 41
  • 39. References  "Java:The Complete Reference” by Schildt H - Part 1 - Chapter 3 , 4 & 5  https://www.w3schools.com/java/ 12/1/2020 Vani Kandhasamy,PSG Tech 42