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

Java Basics - Part1

  • 1.
  • 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
  • 4.
  • 5.
    Datatypes,Variables, Operators Part 1 –Chapter 3 & 4 12/1/2020 Vani Kandhasamy,PSG Tech 5
  • 6.
    DataTypes  Kinds ofvalues 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,PSGTech 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 locationthat 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,PSGTech 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.
  • 11.
    Operators  Symbols thatperform 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,PSGTech 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 distancelight 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,PSGTech 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,PSGTech 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) { casevalue1: // 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,PSGTech 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; } } }
  • 19.
  • 20.
    Practice3 Open repl.it Identify seasonusing switch 12/1/2020 Vani Kandhasamy,PSG Tech 20
  • 21.
  • 22.
    12/1/2020 Vani Kandhasamy,PSGTech 22 What if you want to do it for 200 Rules?
  • 23.
    12/1/2020 Vani Kandhasamy,PSGTech 23 Iterative Statements while (CONDITION) { STATEMENTS } do { STATEMENTS } while (CONDITION); for(initialization;CONDITION;update){ STATEMENTS }
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
    12/1/2020 Vani Kandhasamy,PSGTech 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 whethera number is Prime or not 12/1/2020 Vani Kandhasamy,PSG Tech 29
  • 30.
  • 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,PSGTech 32 Declaration & Instantiation Initialization Declaration, Instantiation & Initialization
  • 33.
    12/1/2020 Vani Kandhasamy,PSGTech 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,PSGTech 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 anarray using for-each style 12/1/2020 Vani Kandhasamy,PSG Tech 38
  • 36.
    Multi- dimensional Array 12/1/2020 Vani Kandhasamy,PSGTech 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,PSGTech 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 ofprinciple diagonal elements of a matrix 12/1/2020 Vani Kandhasamy,PSG Tech 41
  • 39.
    References  "Java:The CompleteReference” by Schildt H - Part 1 - Chapter 3 , 4 & 5  https://www.w3schools.com/java/ 12/1/2020 Vani Kandhasamy,PSG Tech 42