Java Notes Jul 2024 MBatch
Java Notes Jul 2024 MBatch
11-Jul-2024
************************************************************
Manual Testing :
--------------
Testing :
----------
checking /Verifying sthng on the appl whether functionality is working properly
(or) not as Per FRS or BRS.
Manual testing :
----------------
checking the functionality with help of manual efforts
Steps:
-----------
Open chrome browser, with gmail.com,
Enter user,
Enter pwd
Click Login btn
Verify home page is displayed ---Compose mail button
Automation : 20 to 30 min
- Test resources will be tired .. we will not execute Tc, missing to execute
TC's
Automations tools -- never -
- Costly -- more no of Resources - 8
Automation- initially - we need more resources- -- 2 to maintian
automation code
--------------------
Automation Testing :
---------------------
checking the apppl functionality with help of some automation tool and some
Programming languages.
ex:
Steps:
-----------
Open chrome browser --> cmd -- line of code
with gmail.com, ---> cmd - code
Enter user, --> code
pwd, --> code
Click Login btn --> code
Verify home page is displayed ---Compose mail button --> code
ex:
----------------------------------
QTP by Mercury Interacive -> UFT (Unified Functioanl Testing ) by HP > UFT
acquired by MicrFocus
Test Complete -
KatalonStudio -
NeoLoad
Locust
Gatling
Jmeter -Open source tool or Free tool
------------------------
************************************************************
12-Jul-2024
************************************************************
Selenium Introduction:
----------------------
Selenium : is Open source automation tool.
source code of the project is open to all people -- Free --
Web appl :
----------
the appl's which are opened by browser -- chrome, firefox , Edge...etc
Desktop appl :
--------------
the appl which are installed in our desk top machine -
FAQ What is the diff b/w Web based appl and Desktop appl?
--------------------------------------------------------
1.Def:
2.ex:
3.how many users can access?
UFT - paid tool -- buy licence -- > supports only one language i.e VBscript
demand --
UFT -- paid tool ---> free tools / open source -- > use in project
UFT code --- > migrate to Selenium automation code project
Selenium Advantages:
-------------------
tell same above points
---------------------------
Comparison with commercial tools :
------------------------------------------
Selenium UFT
(Open source Tool) (Commercial /paid tool)
-------------------------------------------------------------------
1. Open source - freely
- buy license in order to use tool
------------------------------------------------------
FAQ: Disadvantages of Selenium:
-------------------------------
------------------------------------
Download Install JDK 8 or 11 Version
Download Eclipse IDE
***********************************************************************************
*********************
13-jul-2024
***********************************************************************************
*********************
Java Introduction:
-----------------
Java is open source Programming language.
ex: addition 10 + 20 = 30
substraction 20 - 10 = 10
OOPS features :
----------------
if any Programing lang supports above OOPs features -- Object oriented Programming
language.
• Platform independence:
-------------------------------
-Java is platform indepenent lang
ex: C- Language ,
Java applications:
-----------------
1. develop Desk top appl's
4. in ERP applications
ex: Saleforce -
Note : after clicking download link > Select checkbox 'I reviewed and accept the
Oracle Technology Network License Agreement for Oracle Java SE
' --> Click Download btn > create Oracle account with ur gmail and password >
login with ur gmail > download
cmd :
-----
open cmd Prompt window : Pressing 'Windows' btn + R --> It opens 'Run' window >
Type 'cmd' > click 'Ok' or Press enter button > opens 'Cmd prompt window' in
black screen
java -version
ex:
C:\Users\Lenovo>java -version
Download "Eclipse" :
-----------------
Open Eclipse :
Create Workspace :
------------------
Create Project :
---------------
steps:
--------
File >New > Other > Search for Java Project > Select 'Java Project > Click "Next"
> ' Give some "Project name" i.e "Jul2024JavaProject" > finish or Next> Click
Finish button > click 'Dont create ' button > it creates new Project i.e
'Jul2024JavaProject" in 'package explorer' window in left side
1. src
2.JRE System Libraries
HW Create 2 Java Projects in Eclipse and find the location of the project in ur
system ?
***********************************************************************************
*********************
15-Jul-2024
***********************************************************************************
*********************
Keywords in Java :
-----------------
to learn any lang --> alphabets A, B,C
make words Apple, Bat...etc
make stmt - > I like an apple - --> have to follow some grammar rules -->
Syntax rules
Apple like I ---invalid stmt
I apple like
Sys will not understand our lang english --> java lang stmts---> Machine
Level Lang (001010101) -- (Sys or Processor understands)
Keyword:
---------
Def:
----
are pre defined words in Java lang.
Reserved words: which has special meaning in Java lang.
which are reserved for Special purpose.
ex:
int --> is keyword, can be used to store integer numbers 10,20, -15
Syntax:
class someClassName
{
Note:
-----
without writing/defining any class, we cannot write any java Program.
- if we want to write any java program, we must write/define class
interface- k./w
syntax :
interface I1
{
public
static
void ---
...etc
---------------------
Create Package and Class:
-------------------------
Package :
---------
collection of classes and Interfaces
class1 + Interface1
class2 + Interface2
class3 + Interface3
create Package :
------------
Steps:
--------
select 'src' folder> Rt click > New > Package > enter Name: package1 > clk
'Finish' button > it creates new package i.e Package1 in src folder
Create class :
----------------
Select Package i.e Package1 , Rt click > new > class >Enter some class name :
class1 class1 > click Finish
> it create new class i.e class1 in the selected package
Create Interface :
------------------
HW : write Naviagation steps how to create interface (same as above)
• Types of comments:
--------------------------
2 types
package Package1;
// stmt1
// stm2
// stmt3
// note2 :
//select multiple lines and Press ctrl + / -- it comments selected lines
// select multiple lines and Press ctrl + / -- it uncomments selected lines
// stmt1
// stm2
// stmt3
package SamplePackage1;
/* Author : Brahma
Program name : Writing comments
Devleoped Date: 15-Jul-2024
*/
/* stmt-1
stm-2
stm-3
*/
/*
* Author : Brahma Program name : Writing comments Devleoped Date: 6-Apr-2023
*
* stnt-1 stm2 stm-2
*/
/* stmt-1
stm-2
stm-3
*/
}
run java :
----------
right click inside main method > run as > Java applictaion
o/p:
-----
Hi Java
How are you
------------------------------------
ex2:
package Package1;
// Short cut :
// to write println () --
//type syso + Press -(ctrl + space) and press enter- it displays
println() stmt fully
System.out.println("stmt-4");
System.out.println("stmt-5");
}
//o/p:
stmt-1
stmt-2
stmt-3
stmt-4
stmt-5
If we dont have laptop, We can run java program in online java -compiler
Refer: https://www.programiz.com/java-programming/online-compiler/
------------------------------------
**************************************
16-Jul-2024
**************************************
println("Hi Java"); : can be used to print the given msg and cursor goes to next
line
package Introduction;
/*
* o/p:
* Hi JavaHow are youI am fine
*
*/
o/p:
Hi Ram
Learn Java
sure
-------------------------
package Introduction;
* I am fine
* What about you.
*
*
*/
System.out.println("Hi Java");
System.out.print("How are you");
System.out.print("I am fine");
System.out.println("What about you.");
package Package1;
o/p:
Hi Java
How are you
I am fine
What about you.
-------------------------------------
HW in the above program replace \n by \t write the o/p?
\t -represents tab (Enter 5 spaces)
.
A B C D
5 spaces
Different ways to run Java Program:
------------------------------------
1. from Eclipse
2. through command prompt window
1. Eclipse:
----------------
1. RT click inside main () --> Run as --> Java appl
2. In Menu Items (under 'navigate' or 'Project' menu) >click 'play button
(or) Run button' > it runs java program
cmds :
-------------------
1. compile java program:
syntax :
javac javafilename.java
javac SampleJava.java
C:\brahma\Practise\SelniumPractiseNew\JavaPrograms>javac SampleJava.java
SampleJava.java:7: error: unclosed string literal
System.out.println("Hi Java')
^
1 error
System.out.println("Hi Java")
C:\brahma\Practise\SelniumPractiseNew\JavaPrograms>javac SampleJava.java
SampleJava.java:7: error: ';' expected
System.out.println("Hi Java")
^
1 error
Compilation Process:
--------------------------
Compiler : will check all errors in programs. if at all we have errors, it
displays all error details
. if at all there are no errors >
> to low level lang (or) Machine level lang ( 01010) --> Processor (or) system >
executes the instruction
> display the o/p on console window
once we compile java progam- it creates class file with same name as classname >>
byte code instructions
> JVM -> Machine lang (or) low level language > machine (or) system or
processor understands only machine lang only > Executes program and displays the
o/p
FAQ JVM ?
JVM convert byte code instructions to machine lang and it will be given to
processsor . executes the instruction
> display the o/p on console window
C:\brahma\Practise\SelniumPractiseNew\JavaPrograms>java SampleJava
Hi Java
C:\brahma\Practise\SelniumPractiseNew\JavaPrograms>java SampleJava
Hi Java
not displaying Hi Ram .. again we must compile java if we modify any line in java
program.
C:\brahma\Practise\SelniumPractiseNew\JavaPrograms>javac SampleJava.java
C:\brahma\Practise\SelniumPractiseNew\JavaPrograms>java SampleJava
Hi Ram
C:\brahma\Practise\SelniumPractiseNew\JavaPrograms>
Note : every java program, first we must compile and then Run Java program.
if you modify some code/ add 2 (or) 3 lines of code, first we must compile again
, then run java program.
C:\brahma\Practise\SelniumPractiseNew\JavaPrograms>java SimpleJava.class
Error: Could not find or load main class SimpleJava.class
Caused by: java.lang.ClassNotFoundException: SimpleJava.class
C:\brahma\Practise\SelniumPractiseNew\JavaPrograms>java SimpleJava.java
error: class found on application class path: SimpleJava
----------------------------
Datatypes in Java:
--------------------
what type of data we are storing / using
2 types:
-------
1.Primitive data types:
Numbers:
-------
double :
----------
is predefined in java .
- can be used to store decimal nos
-allocate 8B of memory
- can store 15 values after decimal point
character:
-----------
to store single character -- char
char - is predefined k/w in java
invalid : 'ABC'
- it must be enclosed in single quotes
-- allocated 2Bytes of memory
----------------------
boolean :
------------
is predefined k/w in java lang.
----------------------------------------------------------
Revision:
-------
************************************************************************
17-Jul-2024
************************************************************************
String :
-----------
"Rama", "Sita"
-is not primitive data type - is predefined class in java. but it is often used
in the form of data types.
***********************************************************************************
*********************
FAQ Diff b/w primitive data type and reference data type ?
Def
ex: byte,short,int, long
float , double ,
char, boolean
-----------------------
Variable:
------------------
In Mathemtics,
x = 10
y = 20
z = x + y , what is the value of z?
Z =
//
// z= 30
ex2:
a = 3
b =2
c = a*b, c= ?
c =
c =
Declare var (esal) and store decimal no's --> float esal;
// esal is var of type float data type- so esal var we can store only decimal
no's
a = 10;
1. varname= val;
20 val..... b contains 20
2. varName = var;
int a;
a =10;
int b;
b = a;// value of a i.e 10 is assigned to left side var i.e b so b contains
10 val.
// 10
b = 10
3. varName = Expression;
c = a +b;
c= 10 + 20
30
c = 30 rt side expression is evaluated/calculated first and result will
be stored in left side var i.e c
so C contains 30 val.
Note:
---------
1. Left side, always we must use varname only
invalid :
10 = a ; Left side, we have to use varname only
10.657 = b; // invalid stmt
----------------------------------
Int basic program:
-----------------
package SamplePackage1;
System.out.println("hi Java");
System.out.println(a);
// 10
System.out.println("a");// a
System.out.println(b);
// 20
System.out.println(c);
// 30
o/p:
---
hi Java
10
a
20
30
---------------------
HW Declare variables for "byte" data type and store some values and display
o/p ?
HW Declare variables for "short" data type and store some values and display
o/p ?
HW Declare variables for "long" data type and store some values and display
o/p ?
package SamplePackage1;
// Note :
//Can We store char value into int variable ?
a = 'B';
// 66
// value of ASCii no A -65 , B -66, C-67.....
// 66
// a= 66
// ascci
System.out.println(a);
// 66 . Note: it wont display 'a'
---------------------------------------
Float basics:
--------------
package SamplePackage1;
// A - 65
// 65
// f = 65
// 65 ---> 65.0
// f = 65.0
// display f var value
System.out.println(f);
// 65.0 but not 'A' and
not 65
o/p:
------------
2.3
4.0
65.0
--------------------------
double basics:
-------------
package SamplePackage1;
double x;
x = 2.34d;
// d- double. even if we dont write 'd' at end - it will be considered
as double value
//x = 3.45;// d is optional
// display x
System.out.println(x);
// 2.34
// display x var
System.out.println(x);
// 4.534
// note :
//Select all lines > ctrl + I - Indentation (or) allignment of
lines proper
********************************************
18-Jul-2024
********************************************
char basic program:
---------------------
package SamplePackage1;
// display var ch
System.out.println(ch);// A
// A
o/p:
----
A
B
---------------------------------------
// display var a
System.out.println(a);
// true
// display var a
System.out.println(a);
// false
// Error : boolean data type , we can store only true / false values
but not int no
String :
---------
is collection of characters (or) group of chars's
- must be enclosed dbl quotes
package SamplePackage1;
Revision :
---------
// byte, short,int, long --> to store integer values 10,20,-10
// float, double --> decimal no 1.34f, 1.45d
// char --> 'A'
// boolean --> true / false
// String --> "ram"
-----------------------------
Concatenation Operator (+):
---------------------
package SamplePackage1;
// 30
// c = 30
// c = right side expression is evaluated /
calculated and result will be stored in left side var
// i.e C so C contains 30 val.
// display var c
System.out.println(c);
// 30
// + concatenation Operator:
// performs concatenation (or) joining values
// Note: if any one value is String, it performs
concatenation or joining values
// "ram" + 20 --> ram20
System.out.println(1 +3);//--> 4
System.out.println("1" + 3); // --> joining 13
System.out.println(1+ "3");// joining 13
System.out.println("1"+ "3");//joining 13
o/p:
30
ram20
10sita
ramasita
ramasitalakshman
4
13
13
13
1Sita
1Sita2
s
Rama
String s value is =Rama
30Ram
Ram1020
-------------------------------
• Declare Multiple Variables and assign values:
-----------------------------------------------
declare single var :
------------------
datatype varName;
int a;
a = 20;
or
int a =20;
--------------------
declare variables a,b,c of type = int in and store 10,20,30
int a;
int b;
int c;
a =10;
b=20;
c=30;
Syntax :
--------
datatype VarName1 , VarName2, VarName3.... etc ;
---------------
package SamplePackage1;
// store 10 into a
a = 10;
// print a
System.out.println("a="+ a);
// a = 10
//store 20 into b
b =20;
System.out.println("b="+ b);
//store 30 into c
c = 30;
System.out.println("c="+ c);
// Display a1
System.out.println("a1=" + a1);
// 1
// Display b1
System.out.println("b1=" + b1);
// 2
// Display c1
System.out.println("c1=" + c1);
// 3
// Can we declare one int , one float var in single line
// int a5, float f;
// declaring single var a2 and and assign the 20 value in the same
line
int a6 =20;
// -------------------
Note:
package SamplePackage1;
------------------------
package SamplePackage1;
int a, b ,c; // local var : the variables , which are declared inside
method
a= 10; // all local variables must be initilased with some value else
it throws error
b=20;
c=30;
// a, b ,c = 10;// invalid
a = b = c = 20;
// c= 20
// b =20
// a =20
//
//
// display a,,b,c
System.out.println("a="+a);
// "a=" + 20
// a=20
System.out.println("b="+b);
// "b=" + 20
// b = 20
System.out.println("c="+c);
// c =20
**********************************
19-Jul-2024
**********************************
//ex invalid
// int 1ename;// invalid. var name should not
start with digit
// int 25;// var name must start with alphabet or
Underscore
// _ is special char --
String emp_name;// valid
// Note : always try to give very short name and meaningful name
// String empname;
// String empname;// Duplicate local variable empname
-------------------------------------------------------
Operators:
-----------
1. Arithmetic operators: + , - ,*, / , %
3.Compound operators
1.Arithmetic operators:
-----------------------
+ - * / %
used perfom arithematic opertions like addition, subtsraction , Multiplication,
division
+
-
*
/ - Quotient val
% - Remainder operator
2 + 3
3 - 2
3 * 2
3 / 2 = Division operator - gives Quotient val
3 % 2 = Remainder opertator --gives Remainder val
package OperatorsBasics;
public class ArithematicOperators {
// "div=" + 1
// div=1
// 3 /2 - Quotient val
// 2) 3 ( 1 -(Quotient val)
// 2
// ---------------
// 1 (Remainder)
System.out.println("div= " + div);
// % - Remainder operator
int rem = a % b;
// rem = 3 % 2 ;
// rem= 1
System.out.println("rem ="+ rem);
// rem val=1
// 5/ 2 = quotient val
// 5 % 2 = Remainder val
// 2 ) 5 (2 Quotient val)
// 4
// ------------------------
// 1(remainder val)
System.out.println(5/2);// 2
System.out.println(5%2);// 1
System.out.println(9/2);//4 -quotient
System.out.println(9%2);// 1 rem val
}
}
// HW Perform add, sub, mul, div (quo, Remainder) of 2 nos 6 ,2 and display the
o/p?
// HW Perform add, sub, mul, div (quo, Remainder) of 2 nos 7,2 and display the
o/p?
------------------------
o Relation operators /Comparison operators:
------------------------------------------------
can be used to compare 2 nos and gives the o/p in the form of true/ false
<
>
<=
>=
==
!= not equal
6 < 5 =
3 < 5 =
6 > 5 =
3 > 5 =
6 <= 5 -->
6 <= 6 -->
3 <= 6 -->
6 >= 5 -->
6 >= 6 -->
3 >= 6 -->
6 != 3 -->
6 != 6 -->
package OperatorsBasics;
System.out.println(6>5);// true
System.out.println(3>5);// false
System.out.println(6<=5);// false
System.out.println(6<=6);// true
System.out.println(3<= 6);// true
System.out.println(6>=5);// true
System.out.println(6>=6);// true
System.out.println(3>=6);// false
// HW do below work
// 6 == 6 gives o./p : -->
// 6 == 3 -->
// 6 < 5 ;
// res = false = is a boolean val
// display result
System.out.println("res= "+ res);
// res=false
}
2 == 2
2 == 3
3 > 2
2 > 3
6 < 3
6 < 8
3 >= 2
3 >= 3
3 >= 10
3 <= 2
3<=3
3<=10
3 !=3
3! =10
Note :
----
3 > 3 --> False
3 < 3 --> False
System.out.println(3>3);// false
System.out.println(3<3);// false
------------------------
Compound operators:
--------------------------
mixed operators =
+=
-=
*=
/=
%=
package OperatorsBasics;
int a = 2;
a += 5;
// a = a +5;
// 2 + 5
// 7
// a = 7
System.out.println("a="+ a);// 7 but not 2
a -= 5;
// a= a-5;
// 7-5
// 2
// dont substitute a= 2 , substitute a=7
//
// a = 2
System.out.println("a="+ a);//2
a *= 5;
// a = a*5 ;
// 2* 5
// 10
// a = 10
System.out.println("a="+ a);// 10
a/=5;
// a = a / 5;
// 10 / 5 == Quotient
//
// a = 2
System.out.println("a="+ a);// 2
a%=5;
// a = a %5;
// 2 % 5;
// a= 2 rem val
// 5) 2 ( 0 - not 0.4
// 0
// ---------
//// 2 -- rem
System.out.println("a="+ a);// 2
a= 3;
b =2;
a+=b;
// a = a + b;
//
//
a-=b;
// a = a-b;
//
//
HW
a*=b;
a/=b;
a%=b;
2) 5 ( -Quotient val
-------------
-rem val
0 - false
1 - true (or any no 235)
a b a && b (Multiplication)
-------------------------------
0 0 0
0 1 0
1 0 0
1 1 1
--------------------
a b a && b (Multiplication)
---------------------------------------
false false false
false true false
true false false
true true true
package OperatorsBasics;
// result = false
System.out.println("Result a &&b ="+ result); // false
a = true;
b = true;
}
-------------------------
|| - Logical OR operator :
-------------------------
Pipe symbol
- can be used to perform addition (+) operation on boolean values
if any one i/p is true - Logical OR gives o/p: true else false
if both i/p's are false - false
a b a || b (Addition +)
------------------
0 0 0
0 1 1
1 0 1
1 1 1
--------------------
a b a || b (addition+)
--------------------------------
false false false
false true true
true false true
true true true
-------------------------
package OperatorsBasics;
// addition
//
// Display Result
//
// perform logical OR operation and store the o/p into result var
//
// Display Result
// perform logical OR operation and store the o/p into result var
//
// Display Result
// perform logical OR operation and store the o/p into result var
//
// Display Result
****************************
20-Jul-2024
****************************
!true = false
!false = true
package OperatorsBasics;
a = false;
res = !a;
// ! false
// true
// res = false;
System.out.println("res ="+ res);// false
}
AND, OR , Not
&& || !
* + Negation
Ex1:
boolean result = true || false && true;
// 1st calculate / evaluate && only
// true || false
// true
System.out.println(result);// true
Ex2:
boolean result = (5 > 3) && (2 < 4);
// true && true
// true
System.out.println(result);// true
Ex3:
int x = 5;
boolean result = (x >= 5) || (x < 2);
// 5>=5 5<2
// true || false
// true
System.out.println(result);
HW Ex4:
boolean result = (7 < 5) && (8 > 3) || (9 != 9);
// //
// //
// //
System.out.println(result);
HW Ex5:
int x = 4;
int y = 6;
boolean result = (x == 4) && (y != 6) || (x < y);
// //
// //
// //
System.out.println(result);
HW Ex6:
int a = 8;
int b = 3;
boolean result = (a % 2 == 0) || (b % 2 != 0);
//
//
//
System.out.println(result);
HW Ex7:
boolean result = (4 >= 4) && (2 != 2) || (7 <= 7);
//
//
//
System.out.println(result);
HW Ex8:
boolean a = true;
boolean b = false;
boolean result = (a && b) || (!b);
//
//
System.out.println(result);
HW Ex9:
int x = 10;
boolean result = (x < 5) && (x == 10) || (x > 5);
//
//
//
System.out.println(result);
-----------------------------
Increment operators :
-------------------------------
can be used to increment val of var by 1
-------------------
package OperatorsBasics;
int a = 2;
++ a;// pre Increment
// a = a +1;
// 2 +1
// a = 3
System.out.println("a="+ a);// 3
a= 3;
a ++;// Post Increment
// a = a+1;
// 3 +1
// a= 4
System.out.println("a="+ a);// 4
---------------------
package OperatorsBasics;
a= 7;
System.out.println(a++);
// 1st display var value + incremnting
// 7 7+1 =8
Post decrement :
var --;
package OperatorsBasics;
int a = 3;
--a;
// a= a -1
// 3-1
// a=2
System.out.println("a="+a);// 2
a= 4;
a --;//
// a = a-1;
// 4-1
// a= 3
System.out.println("a="+a);// 3
------------------
package OperatorsBasics;
System.out.println(--a);//
// 1st perform decrement + display var
// a-1
// 3-1
// a=2 , display a=2
a= 5;
System.out.println(--a);//
// 1st it performs decrementing + display var value
// a-1
// 5-1
// a= 4,, display a =4
a= 6;
System.out.println(a--);//
// display var , + perform Decrement
// print a =6 , a-1
// 6-1
// a = 5
System.out.println("a="+a);// 5
a =7;
System.out.println(a--);// 7
// display var value + decrementing
// a-1
// print a = 7 7-1
// A= 6
}
}
--------------------
package OperatorsBasics;
System.out.println(++x);//
// 1st increment + display var later
// x +1
// 6+1
// x=7, print 7
System.out.println(x--);//
// display var + decrementing later
// print 7 first , x-1
// 7-1
// x= 6
System.out.println(--x); //
// 1st decrementing + display var later
// x-1
// 6-1
// x= 5 diaplay x =5
package OperatorsBasics;
System.out.println(x++);//
System.out.println(++x);//
System.out.println(++x); //
}
}
package OperatorsBasics;
System.out.println(x--);//
System.out.println(--x);//
System.out.println(--x); //
1.Conditional stmts:
--------------------
ex:
2. Loop stmts:
------------
For
while loop
do while loop
1.Conditional stmts:
----------------------
if() cond stmt
syntax:
if (cond) note : there is no ; at the end of if stmt
{ // if block / beginning of if block
stmt2; // gets executed if cond is true only
stm3;
} // end of if block
stmt4;
ex:
{ // if block / beginning of 'if' block
stmt2;
stm3;
} // end of if block
package ConditionalStatements;
System.out.println("after if block");
o/p:
----
Given number is > 10
stmt-2
stmt-3
after if block
-ve :
package ConditionalStatements;
int num = 3;
System.out.println("after if block");
o/p:
after if block
package ConditionalStatements;
if(num<10)
{// 5 <10 true,enters if block
System.out.println("Given number is <10");//
}
o/p:
Given number is <10
after if block
-ve:
package ConditionalStatements;
if(num<10)
{// 30 <10 false, does not enter if block, ctrl goes after if block
and executes all stmts
System.out.println("Given number is <10");//
}
}
o/p:
after if block
System.out.println("stmt-1");
if(true)
{
System.out.println("stmt-2");
System.out.println("stmt-3");
}
System.out.println("stmt-4");
}
o/p:
stmt-1
stmt-2
stmt-3
stmt-4
System.out.println("stmt-1");
if(false)
{
System.out.println("stmt-2");
System.out.println("stmt-3");
}
System.out.println("stmt-4");
}
o/p:
stmt-1
stmt-4
-----------------------
2. if else stmt
stmt1;
if(cond)
{
stmt -2; // gets executed if cond is true only
stmt -3;
}
else // else - k/w in java - can be used to define else block
{
stmt -4;
stmt -5;
} // end of if block
package IFCondStmts;
System.out.println("stmt-1");
if(true)
{
System.out.println("stmt-2");// true
System.out.println("stmt-3");
}
else // if con- is false - else block gets executed
{
System.out.println("stmt-4");
System.out.println("stmt-5");
}
System.out.println("stmt-6");
o/p:
--------
stmt-1
stmt-2
stmt-3
stmt-6
package IFCondStmts;
System.out.println("stmt-1");
if(false)
{
System.out.println("stmt-2");// true
System.out.println("stmt-3");
}
else // if con- is false - else block gets executed
{
System.out.println("stmt-4");
System.out.println("stmt-5");
}
System.out.println("stmt-6");
o/p:
-------
stmt-1
stmt-4
stmt-5
stmt-6
if ()
if else
ex: write a program to check the given Age is greater than 18.
If age is > 18, display msg “age is >18”
Else display msg “age is not >18 ”
package ConditionalStatements;
************************
21-Jul-2024
************************
package ConditionalStatements;
int num = 4;
// int num = 5;
if(num%2 == 0)
{// 4 %2
// 0 == 0 true, it enters if block
System.out.println("It is Even no");
}
else
{
System.out.println("It is not Even no");
}
}
-ve :
ex2:
package IFCondStmts;
if(num%2 == 0)
{// 3%2
// 1 == 0 false, it goes to else block
System.out.println("It is Even no");
}
else
{
System.out.println("It is not Even no");
}
o/p"
It is not Even no
--------------------------------
if(num%2 == 1)
{// 3%2
// 1 == 1 true, enters if block
System.out.println("It is Odd no");//
}
else
{
System.out.println("It is not Oddd no");
}
}
o/p:
It is Odd no
package ConditionalStatements;
if(num%2 == 1)
{// 4%2
// 0 == 1, false, does not enter if block, ctrl goes to else part
and executes stmts inside the else block
System.out.println("It is Odd no");//
}
else
{
System.out.println("It is not Oddd no");
}
o/p:
----
It is not Oddd no
HW WAP to check given salary is > =50000 -- display msg "Salary is >=50000"
else display msg "Salary is not > = 50,000
HW WAP to check Esal <= 50,000, display msg "Salary <= 50000"
else Salary is not <= 50000
package ConditionalStatements;
if(eid == 10)
{// 10 == 10, true, enters if block
empsal = empsal + incrementSalary;
// 30000 + 10000
// 40000
//empsal = 40000
-ve:
package ConditionalStatements;
if(eid == 10)
{// 25 == 10, false,does not enters if block, ctrl goes to else block
empsal = empsal + incrementSalary;
// 30000 + 10000
// 40000
//empsal = 40000
o/p:
No Increment
-ve:
package ConditionalStatements;
//HW WAP to check eid =100 or esal =50 000, display proper msg eid = 100 or
salary is 50000
else display msg "either eid is not = 100 or salary is not = 50000"
//note : use logical OR symbol
package ConditionalStatements;
-ve:
package ConditionalStatements;
package ConditionalStatements;
if(num >0)// 1
{// 10 >0, true,enters if blck
// -2 > 0, false,
System.out.println("Given no is +Ve no");
}
else
{
System.out.println("Given no is not a +Ve no");
}
---------------------------
3. if else if else if else if () -Multiple else if cond
-----------------
Syntax:
stmt-1;
if(cond)
{
stmts-2;
stmt-3;
else if(cond-2)
{
stmt-4;
stmt-5;
}
else if(cond-3)
{
stmt-6;
stmt-7;
}
stmt-8;
package ConditionalStatements;
System.out.println("stmt-1");
if(true)
{
System.out.println("stmt-2");// gtes executed if cond - true
System.out.println("stmt-3");
}
else if(true) // if con- is false - else block gets executed
{
System.out.println("stmt-4");
System.out.println("stmt-5");
}
else if(true)
{
System.out.println("stmt-6");
System.out.println("stmt-7");
}
System.out.println("stmt-8");
}
}
o/p:
-----
stmt-1
stmt-2
stmt-3
stmt-8
---------------------
o/p for below program?
package IFCondStmts;
public class ifbasics1 {
System.out.println("stmt-1");
if(false)
{
System.out.println("stmt-2");// gtes executed if cond - true
System.out.println("stmt-3");
}
else if(false) // if con- is false - else block gets executed
{
System.out.println("stmt-4");
System.out.println("stmt-5");
}
else if(false)
{
System.out.println("stmt-6");
System.out.println("stmt-7");
}
System.out.println("stmt-8");
o/p:
---------------
stmt-1
stmt-8
WAP
// marks > 85 and marks<=100 --> 1st class
// marks > 60 and marks <=85 --> 2nd class
// marks >=35 and marks <=60 --> 3rd class
// else --> Fail
package ConditionalStatements;
------------------------------
Nested if :
------------
writing if cond stmt with in another if cond stmt- nested if stmt.
syntax:
--------------
if(cond-1)
{ //
if(cond-2)
{ // cond -1 is true and cond-2 is also true
}
else
{
if(cond-3) // cond-1 is false, cond-3 is true
{
}
}
ex1: program - to check the age is >=18, if age >=18, check weight > 50, display
msg "You are eligible to donate blood"
package ConditionalStatements;
public class NestedIfBasics1 {
if(age >=18)
{// 25 >=18, true
System.out.println("age is >=18");
****************************
22-Jul-2024
****************************
-------------------------
Switch -Conditional stmts
-------------------------
syntax:
----------
switch(int val/var (or) char val /var (or) String val/var (or) expresion)
{ // // begining of switch block
case val1 :
stmt1;
break;
case val2:
stmt2;
break;
case val3:
stmt3;
break;
default :
stmt4;
stmt5;
..............
where there are multiple cases / options are available - we will go for Switch
statement
package ConditionalStatements;
int dayNo = 3;
// int dayNo = 5;
switch(dayNo)
{ // 1 // begining of switch
case 1:
System.out.println("1-is Sunday");
break;// come out of switch block and ctrl goes after switch
block
case 2:
System.out.println("2-is Monday");
break;
case 3:
System.out.println("3-is Tuesday");
break;
default:
System.out.println("No case is matched");
}// ending of switch
package ConditionalStatements;
// int dayNo = 3;
int dayNo = 5;
switch(dayNo)
{ // 1 // begining of switch
case 1:
System.out.println("1-is Sunday");
break;// come out of switch block and ctrl goes after switch
block
case 2:
System.out.println("2-is Monday");
break;
case 3:
System.out.println("3-is Tuesday");
break;
default:
System.out.println("No case is matched");
}// ending of switch
o/p:
---
No case is matched
after switch block
syntax:
--------
break;
// can be used to come out of switch block../ for loop, while loop... etc
package IFCondStmts;
int dayNo = 1;
switch(dayNo)
{ // beginning of switch block
case 1:
System.out.println("Sunday");
// break;// if we dont write break,
case 2:
System.out.println("Monday");
break;
case 3:
System.out.println("Tuesday");
break;
// default:
// System.out.println("No case is matched");
} // end of swicth block
o/p:
----
Sunday
Monday
after swicth block
------------------
Note:
------
Duplicate case numbers are not allowed in Switch block
ex:
case 1:
System.out.println("Sunday");
break;
case 1: // Duplicate case are not allowed
System.out.println("Sunday");
break;
case 2:
case 3:
case 2:
package IFCondStmts;
int dayNo = 1;
switch(dayNo)
{ // begining of switch block
case 1:
System.out.println("Sunday");
break;
// Duplicate case are not allowed. if we write duplicate cases
case 1:
System.out.println("Monday");
break;
case 3:
System.out.println("Tuesday");
break;
// default:
// System.out.println("No case is matched");
} // end of swicth block
switch(int val/var (or) char val /var (or) String val/var (or) expresion)
switch('A') -->
switch(char var) -->
switch("RAM") -->
switch(String var) -->
switch(2.34f) -->
switch(float var) -->
switch(2.34d) -->
switch(double var) -->
switch(true) -->
switch(false) -->
switch(boolean var) -->
package ConditionalStatements;
// switch(f) // error
// switch(d)
switch(ch)
// Cannot switch on a value of type float. .
//Only convertible int values, strings or enum variables
are permitted
{
case 1.0f:
System.out.println("stmt-1");
break;
case 2.0f:
System.out.println("stmt-2");
break;
default:
System.out.println("default ");
}
}
package ConditionalStatements;
char ch ='B';
switch(ch)
// 'B'
{
case 'A': // not macthing
System.out.println("Apple -stmt-1");
break;
default:
System.out.println("default ");
// boolean b = true;
String s = "Ram";
// switch(b) // we cannot use boolean var val in switch ()
switch(s)
// Cannot switch on a value of type boolean. .
//Only convertible int values, strings or enum variables are permitted
{
case "Ram": // macthing
System.out.println("Ram - is king");
break;
case "Amar": // matching
System.out.println("-Amar -is normal person");// yes
break;// can be used to come out of swicth cond
default:
System.out.println("default ");
--------------
Switch (String val/ var):
HW WAP "Mon" --> Monday
Tue ---> Tuesday
Note:
-----
Is is better to write break stmt in side each case block. else it executes next
case block also even if case val is not matched.
-----------------------
Revision:
-------
Note :
Switch() - accepts int/char/string val/var
but not float,double, bolean val/var
- break;
- default : block gets executed , if no case val is matched --(or) none of case is
matched
---------------------------
Loop statements:
----------------
execute group of stmts repeatedly
// System.out.println("stmt-1");
// System.out.println("stmt-2");
// System.out.println("stmt-3");
1.For Loop
2.While Loop
3. Do while
1.for Loop:
------------
syntax:
if cond (step-2) is true,again it goes inside the loop, executes stmts inside
loop (step-3)
and goes to step-4 performs increment/decrement ---- again it goes to step-2
if cond (step-2) is true,again it goes inside the loop, executes stmts inside
loop (step-3)
and goes to step-4 performs increment/decrement ---- again it goes to step-2
if cond is false, control comes out of loop and goes after out of loop,
executes smts after loop
ex:
package LoopBasics;
// System.out.println("stmt-1");
// System.out.println("stmt-2");
//
//
// System.out.println("stmt-1");
// System.out.println("stmt-2");
//
//
// System.out.println("stmt-1");
// System.out.println("stmt-2");
//o/p:
//stmt-1
//stm-2
//stmt-1
//stm-2
//stmt-1
//stm-2
//out of for loop
o/p:
----
package LoopBasics;
System.out.println(i);
// 1
// 2
// 3
}//
}
o/p:
----
i=1
i=2
i=3
out of for loop
-----------------
display numbers 1 to 5?
package LoopBasics;
for(int i=1;i<=5;i++)// i=1, 1+1 =2, 2+1= 3, 3+1 =4, 4+1 =5, 5+1 =6
{// 1<=5 true, enters for loop,incremeting
// 2<=5 , true,enters for loop,
// 3<=5, true, enters for loop,
// 4<=5 , true, enters for loop,
// 5<=5 true
// 6<=5 false, wont enter for loop, ctrl goes fater for loop
System.out.println(i);
// 1
// 2
// 3
// 4
// 5
}// end of for loop
HW display numbers 1 to 10
HW display numbers 1 to 15
**********************************************
23-Jul-2024
**********************************************
package LoopBasics;
// 4
// 5
o/p:
----
i=2
i=3
i=4
i=5
out of for loop
package LoopBasics;
}
}
o/p:
out of for loop
------
o/p for below program:
----------------------
package LoopBasics;
for(int i=3;i>=1;i++) // i =
{//
//
//
// // cond is always true - will never become false --
loop - it goes to infinite loop
System.out.println(i); //
}
o/p:
----
3
4
5
6
7
....inifnite looop - as condi is always true
ex:
package LoopBasics;
}
o/p:
---
3
2
1
out of for loop
Note :
------
package LoopBasics;
for(int i=5;i>=1;i--)// i=5 , 5-1 =4, 4-1 =3, 3-1 =2, 2-1 =1, 1-1 = 0
{// 5>=1 true, for loop
// 4>=1 true , for loop
// 3>=1 true, forl oop
// 2>=1 true
// 1>=1 true , for loop
// 0>=1, False , ctrl goes after for loop
System.out.println(i);
// 5
// 4
// 3
// 2
// 1
HW display numbers 5 to 2
HW display numbers 10 to 5
Note:
We can write i--, --i or i=i-1 in for loop
and also we can write i = i+2
or i= i+3;..etc
o/p:
-----
1
3
out of for loop
------------------------------
while loop:
----------
Syntax:
---------
// initialisation;
// while(condition)
// {
// stmt1;
// stm-2;
// Incre/Decre;
// }
package LoopBasics;
int i=1;
while(i<=3)
{ // 1<=3, true, enters while loop
// 2<=3 , true, enters while loop
// 3<=3 , true , enters while loop
// 4<=3 ,false , ctrl goes after while loop
System.out.println("stmt-1");
//stmt-1
// stmt-1
//stmt-1
i++;// i = i+1= 1+1=2 , 2+1 =3, 3+1 =4
}
o/p:
stmt-1
stmt-1
stmt-1
after while loop
ex:
package LoopBasics;
int i=1;
while(i<=3)
{ // 1<=3, true, enters while loop
// 2<= 3 , true , enters while loop
// 3<=3 true , enters while loop
// 4<=3 , false, ctrl goes after while loop
System.out.println(i);
// 1
// 2
// 3
o/p:
----
1
2
3
after while loop
int i=2;
while(i<=5)
{
System.out.println(i);
i++;
}
}
}
----------
HW Write o/p for below program?
package LoopBasics;
int i=3;
while(i<=5)
{ //
//
//
//
System.out.println(i);//
i++;// i =
}
o/p:
---
3
4
5
after while loop
package LoopBasics;
int i=3;
while(i>=5)
{ //
System.out.println(i);//
}
}
o/p:
----
after while loop
***********************************************************************************
*********************
***********************************************************************************
*********************
package LoopBasics;
// display 3 to 1
int i=3; // while reversing, we have to use decrement
while(i>=1)
{ // 3>=1 true, enters while loop
// 3>=1 true,enters while loop
// 3>=1 , true, enters while loop
// ...
//....
//Note :
--------
//while writing loop stmts, write (be careful about) proper increment/ decrement
operators- else it goes to infinite loop
package LoopBasics;
// Note :
// while writing loop stmts, write (be careful about) proper
increment/ decrement operators- else it goes to infinite loop
o/p:
-----
i =3
i =4
....
....
infinte loop
-----------------
package LoopBasics;
// Note :
// while writing loop stmts, write (be careful about) proper
increment/ decrement operators- else it goes to infinite loop
}
o/p:
i=3
i=2
i=1
after while loop
-------------------------
package LoopBasics;
// display 3 to 1
int i= -1 ; // while reversing, we have to use decrement
System.out.println(-1>=1);// false
while(i>=1) //
{ // -
System.out.println("i="+i); //
i--;// i=
}
// Note :
// while writing loop stmts, write (be careful about) proper
increment/ decrement operators- else it goes to infinite loop
o/p:
----
false
after while loop
-----------------
---------------------------------
package LoopBasics;
int i=1;
while(i<=3)
{ //
//
//
System.out.println(i); //
i = i+2;// i=
}
o/p:
-----
1
3
after while loop
---------------------------
Do while loop:
------------
// do while
// do is predefined word (or) k/w in java language
// can be used to write do while loop stmt
// syntax:
-------------------------
// do
// {
// stmt-1;
// increment / Decrement operator;
// }
// while (condi); // Note : dont forget to write ; in do while loop
stmt
ex:
package LoopBasics;
int i=1;
do
{
System.out.println("stmt1-");// these stmts gte executed
at least once
// stmt1-
// stmt1-
// stmt1-
i++;//i+1 =1+1 =2, 2+1 =3, 3+1 =4
}
while(i<=3);// at end we are checking condi
// 2<=3, true , enters do block
// 3<=3 ,true , enters do block
// 4<=3 false, ctrl goes after do while loop
o/p:
----
stmt1-
stmt1-
stmt1-
after do while loop
package LoopBasics;
do
{
System.out.println("i="+ i);// i= 1
// do block smts gets executed at least once even if cond is
false
i++;// i =
}
while(i>=3);
// 2>=3 False - it wont go do block
// - does not enter do block and goes after do while loop
System.out.println("after do while loop");
o/p:
-------
i=1
after do while loop
********************************
24-Jul-2024
********************************
Break, continue stmt:
-----------------------
in Switch con
switch()
{
case a:
stmt1;
break; // can be used to come out of switch block
}
------------------
package LoopBasics;
o/p:
----------
i=1
after break
i=2
after for loop
-------------------
write o/p for below program?
package LoopBasics;
}
o/p:
i=1
after for loop
-------------------
break in while loop:
---------------------
package LoopBasics;
public class breakInsideWhileLoop {
o/p:
----------
Enters loop
Enters loop
after while loop
----------------
HW what is the o/p for below program?
int i=1;
while(i<=5)
{
if(i==3)
{
System.out.println("i="+ i);
break;
}
System.out.println("after if i="+i);
i++;
} // end of while loop
o/p:
----------------------------------------------------
HW Write 'break' stmt inside "do while" loop?
Note:
------
break can be used in switch, for , while , do while loop
Switch()
{
case val:
stmt1;
break;
case 2 :
stmt2;
break;
for()
{
break;
while()
{
break;
do
{
break;
}
while(cond);
continue:
--------
package LoopBasics;
System.out.println(i); //1 3
} // end of for loop
System.out.println("after loop");
o.p:
1
3
after loop
--------
Write o/p for the below program ?
package LoopBasics;
}
o/p:
----
i=1
i=2
after break
i=3
after break
after for loop
---------------------
o Nested If, nested for loop
package LoopBasics;
o/p:
-----------
a> 15
b>10
package LoopBasics;
public class NestedForLoop {
for(int i=1;i<=3;i++) // outer for loop i=1 , 1+1 =2, 2+1= 3, 3+1 =4
{ // 1<=3, true, enters outer for loop
// 2<=3 , true enters outer for loop
// 3<=3 , true, enters outer for loop
// 4<=3 , false, ctrl goes after "outer for" loop
System.out.println("inside 1st for loop");// inside 1st for loop
for(int j=1;j<=2;j++) // inner for loop j=1,1+1 =2 , 2+1 =3
{ // 1<=2 , true, enters "inner for" loop
// 2<=2, true ,enters "inner for" loop
// 3 <=2, false, ctrl after inner for loop
System.out.println("i="+ i + ",j="+j);
// i = 1 , j= 1
// i= 1 , j =2
// i =2 . j=1
// i = 2 j =2
// i = 3 j =1
// i =3 j =2
}// end of inner loop
----------------
inside 1st for loop
i=1,j=1
i=1,j=2
after inner loop
inside 1st for loop
i=2,j=1
i=2,j=2
after inner loop
inside 1st for loop
i=3,j=1
i=3,j=2
after inner loop
after outer loop
-----------------
package LoopBasics;
// 3 1
// 3 2
// 3 3
// 4 1
// 4 2
// 1 3
o/p:
---
i=2,j=1
i=2,j=2
i=2,j=3
i=3,j=1
i=3,j=2
i=3,j=3
i=4,j=1
i=4,j=2
i=4,j=3
after outer loop
------------------------
-----------------
HW write o/p for below program:
o/p:
*************************************************
Revision:
---------
cond stmts- if(), if else, if .. else if() else if() , Switch
------------------------
Array:
------
// 10, 20, 30
// 'A','B', 'C'
// "Ram", "Sita","Raju"
int a ;
float f;
// declare int array with name i.e iarr and size of array = 3
int iarr [] = new int [3];
// declare float array with name i.e farr and size of array = 4
float farr [] = new float [4];
// declare char array with name i.e charr and size of array = 3
char charr [] = new char [3];
// declare "String" array with name i.e farr and size of array = 3
String sarr [] = new String[3];
package ArraysBasics;
o/p:
----
array value at index no = 0-->10
array value at index no = 1-->20
array value at index no = 1-->30
*********************************
25-Jul-2024
*********************************
package ArraysBasics;
o/p:
float values in array at index no =0 i.e 1.2
float values in array at index no =1 i.e 2.2
float values in array at index no =2 i.e 3.2
Note:
---------------------
iarr[0] = 3.2f;// Type mismatch: cannot convert from float to int
// int array - can be used store int values
// floar array - can be used store decimal numbers else it throws
error
2. In one array - We cannot store 1 float val + 1 double val + 1 char val + 1
boolean value
"" "" diff data
iarr[0] = 1.23f;
iarr[0] = 3.2;
iarr[0] = 'C';
iarr[0] = true;
iarr[0] = "Ram";
package ArraysBasics;
public class intArrayBasics2 {
---------------------------------
Array length:
---------------
package ArraysBasics;
------------------------------
Display values using for loop in float array:
-------------------------------------------
package ArraysBasics;
// display all vals from float arr using "for loop" and use length of
array
// length of arr
int lengthofArray = farr.length;
// 4
System.out.println("lengthofArray="+lengthofArray);
// 4
// for(int i=0;i<=3;i++)
for(int i=0;i<=lengthofArray-1;i++)
{// 4-1=3
System.out.println("vals from float array using for loop =" +
farr[i]);
}
o/p:
lengthofArray=4
vals from float array using for loop =1.2
vals from float array using for loop =2.2
vals from float array using for loop =3.2
vals from float array using for loop =4.2
------------------------------------
Note:
In array, we can store same data type of values
homogeneous values
In General array, we can not store 1 int val + 1 float + 1 double + 1 char
+ 1 boolean + 1 String
Note:
------
in float array, we can store int number and char value, as it converts int no to
float no and char value --> decimal no
fArr[1] = 'A';//ok no
// 65 --> 65.0
// fArr [2] = true;// Type mismatch: cannot convert from boolean to float
fArr [2] = "Ramu";// Type mismatch: cannot convert from String to float
HW define long array and store some values and display all values from arr using
"for" loop
HW define double array and store some values and display all values from arr
using "for" loop
HW define boolean array and store some values and display all values from arr
using "for" loop
HW define char array and store some values and display all values from arr using
"for" loop
--------------------------------
Default values in int array:
------------------------------
// if We dont store any vals in array, it will store 0 val based on array
type ie. int
package ArraysBasics;
System.out.println("ends here");
------------------
Default values in "float" array : --> 0.0
----------------------------
package ArraysBasics;
Way2 : Intialise values in integer array (or) Store Values in integer Array:
----------------------------------------------------------------------------
package ArraysBasics;
// vals at index 0 1 2
// display all vals from array using for loop
for(int i=0;i<=iarr.length-1;i++)
{
System.out.println("vals from array using for loop="+
iarr[i]);
}
}
--------------------
Way2 : Intialise values in float array (or) Store Values in float Array:
------------------------------------------------------------------------
package ArraysBasics;
HW define "long array" and store vals using 2nd way {120,12313,24214} and
display vals from arr
HW define "double" array and store vals using 2nd way and display vals from
arr
HW define "boolean" array and store vals using 2nd way and display vals from
arr
HW define "char" array and store vals using 2nd way and display vals from arr
Way2 : Intialise values in String array (or) Store Values in String Array:
---------------------------------------------------
package ArraysBasics;
// 0 1 2
2. syntax :
}
- it goes to infinite loop
----------------------------
package ArraysBasics;
o/p:
stmt-1
stmt-2
stmt-1
stmt-2
stmt-1
stmt-2
....
..... infinite loop
--------------------------
for Each loop:
-----------------
can be used to get vals from array / collection obj
syntax :
--------
for(datatype eachVal : arrName)
{
-------------------
Display Values from int Array using for each loop:
=================================================
package ArraysBasics;
---------------
Display Values from float Array using for each loop:?
package ArraysBasics;
o/p:
-----
HW store some vals in "long" array and gets all vals using "for each" loop?
HW store some vals in "double" array and gets all vals using "for each" loop?
HW store some vals in "char" array and gets all vals using "for each" loop?
HW store some vals in "boolean" array and gets all vals using "for each" loop?
HW Display Values from "String" Array using "for each" loop:
*****************************************
26-Jul-2024
*****************************************
Note:
in 'for each' loop,
// we dont need to write any cond-- no cond
// to get vals from array using for each -loop - we dont need to use--
index no (no index no)
- always get vals in forward direction but not in reverse direction
--------------------
for loop with index :
--------------------
- We have to write cond in for loop
- to get values from array, we have to use index no ex: sarr[0], sarr[1]
- we can get vals in forward and also in reverse direction in for loop
package ArraysBasics;
2 types of arrays:
----------------
1. single dimensional array :
2 . Multi Dim array
2. 2 Dim array:
-----------
----------------------
package ArraysBasics;
// index no -3
}
o/p:
val at index 00 =10
val at index 01 =11
val at index 11 =14
val at index 12 =15
HW define 2 dim float array- with 2 rows and 3 columns and store some float values
and display all values
HW define 2 dim char array- with 3 rows and 2 columns and store some char values
and display all values
------------------------
// get all values from 2 dimensional array
// 00 01 02
// 10 11 12
package ArraysBasics;
// 10 1 1 , 12
arr [1] [0] = 10;
arr [1] [1] = 11;
arr [1] [2] = 12;
// arr[1]
[0]
// arr[1]
[1]
// arr[1]
[2]
o/p:
-----
vals from arry using for loop=0
vals from arry using for loop=1
vals from arry using for loop=2
vals from arry using for loop=10
vals from arry using for loop=11
vals from arry using for loop=12
---------------------
// HW Define 2 Dim float array of size 1 , 2 and store some values and display
all vals using for loop
// HW Define 2 Dim String aray of size 3 , 4 and store some values and display
all vals using for loop
package ArraysBasics;
public class getAllValuesFromTwoDimArr {
HW use rows cnt and columns cnt in for loop to display all values from 2 dim
array ?
------------------------------
FAQ SearchForGivenNoInArray :
------------------------
package ArraysBasics;
if(valFound == false)
{// false == false
System.out.println("Value ="+ searchNo + " is not found in
array");
}
-------------
package ArraysBasics;
for(int i=0;i<=charr.length-1;i++)
{
// System.out.println(charr[i]);
if(charr[i] == ch) // if val is found in array , increase the cnt
by 1
{// 'r'
cnt = cnt +1;
}
-----------------------------
package ArraysBasics;
System.out.println("vowelsCnt ="+vowelsCnt);
----------------
HW FAQ WAP to get consonants count from char array and display all consonants ?
r a m a
consonants cnt = 2
-----------------------------
TypeCasting:
-----------
def:
----
// 2 types:
// Widening Casting (automatically) - converting a smaller type to a
larger type size
// byte -> short -> char -> int -> long -> float -> double
int i1 =10;
long l1 = i1;
// int
// 8B 4B -- automatically conversion done by compiler
// 10 --> long 10
}
}
o/p:
---
Before conversion, int value 10
After conversion, long value 10
After conversion, float value 50.0
package TypeCastingBasics;
---------------------
// byte -> short -> char -> int -> long -> float -> double
package TypeCastingBasics;
byte b = 65;
// char ch = b; //Type mismatch: cannot convert from byte to char
//
char ch = (char) b;
// 65
// ch= A 65 --> A
System.out.println("ch="+ ch);
// A
}
// double -> float -> long -> int -> char -> short -> byte
// convert char to int
package TypeCastingBasics;
-----------------------
// convert int val to boolean ---not valid
package TypeCastingBasics;
int i1 =10;
// boolean b1 = i1;
// 10
// true/ false only we cannnot store any other values 10, 3.45f
*************************
Method/Function:
-----------------
Method :
---------
2 Types of methods:
-----------------
1. user defined method
2. Predefined method
} // end of method
}
write add method and return type = void?
// Method name : add
public static void add()// Add method with out args
{
stmt-1;
stmt-2;
// sub
// Method name : substraction
public static void substraction()// Method heading
{
Method call:
------------
if we want to execute stmts inside the Method- we must call method by it's
name.
MethodName();
o/p:
in main method
after add -
Calling add():
------------------
-------------
package MethodBasics;
o/p:
in main method
inside add- method -stmt-1
stmt-2
after add -
package MethodBasics;
// Define Method name M1 () and return type = void and call below stmts
public static void M1()
{
System.out.println("inside M1- method .stmt-1");
System.out.println("stmt-2");
}
//Define Method - M2 and return type = void and call below stmts
public static void M2()
{
System.out.println("inside M2-method.stmt-3");
System.out.println("stmt-4");
}
// call M1
M1();
// Call M2
M2();
o/p:
------
inside M1- method .stmt-1
stmt-2
inside M2-method.stmt-3
stmt-4
-We can define/ write any number of methods inside the class
M1(),M2(), M3()...
//ex: Define add () and return type -void ,perform addition and call it:
// Calling add()
add();
--------------------
ex3: Define Substraction() and call it?
------------------------------------------
package MethodBasics;
//ex: Define substraction () and return type -void ,perform substraction and
call it:
// call substraction()
substraction();
o/p:
inside main()
Result fo substrcation =10
after sub ()
----------------------------------------
********************************
28-Jul-2024
********************************
Note:
-----
We can define every method inside class only.
We can define multiple methods inside class
package MethodBasics;
//ex: Define add () and return type -void ,perform addition and call it:
// Calling add()
add();
// call substraction()
substraction();
o/p:
inside main()
result=30
Result fo substrcation =5
after add ()
ex:Define Method -M1 with 1 int arg, 1 float arg, 1 char arg
public static void M1(int a, float f, char ch) // M1 with 3 args
{
------------------------------
method Call :--> M1(10); we must pass int value in method call
M1(); --invalid method call
ex2:
M2 (int a , int b ) // 2 args
{
M2(10);// invalid as we passed only one value but in method heading 2 args
if 2 args in method heading, we must pass 2 values in method call also.
ex3:
M3 (int a, float f, char ch) // 3 args
{
values in method call and arguments in method definition must be same type.
ex:
Define Method Input arguments and call Method :
----------------------------------------------
package MethodBasics;
public class CallingM1With1Args {
}
}
o/p:
in M2
a= 10
in M2
a= 20
***********************************************************************************
*********************
***********************************************************************************
*********************
Method call:
---------------------
M1(2.45f);//Error: invalid we should not pass float value / Decimal no in method
call as we have M1 (int a)
--> The method M2(int) in the type CallingM1With1Args is not applicable for the
arguments (float)
ex:
package MethodBasics;
// Call M1
M1(10,20);
System.out.println("after M1 ()");
o/p:
in main()
M1 With 2 args
a=10
b=20
after M1 ()
ex: Define 'add' method with 2 i/p arguments and call add()?
----------------------------------
package MethodBasics;
o/p:
----------------
called add()
Addition result =30
System.out.println("called Substarction()");
int result = a-b;
// 20 -10
// 10
// result = 10
System.out.println("Substraction results="+result);
// 10
}
// Call substraction
// substraction();// error we must pass 2 values
o/p:
------
//called Substarction()
//Substraction results=10
*****************************************************
// add(3.5f,4);// Error : The method add(int, int) in the type
MethodWith2Args is not applicable for the
//arguments (float, int)
-----------------------------------------------
syntax:
--------------
returntype M1()
{
return val/var/expre;
}
when we want to get some o/p from the method, then we have to write respective data
type in return type and write return stmt also.
return " ""
ex : if we want to get int o/p from the method, then we have to write int data
type in return type and also write return stmt (return 10;)
ex : if we want to get float o/p from the method, then we have to write float data
type in return type and also write return stmt (return 10.85f;)
// ex : return 10;-->
// ex: return a; -->
// ex3: return a+b; -->
int M1()
{
return 10;
}
float M1()
{
return 10.45f;
}
char M1()
{
return 'B';
}
String M1()
{
return "Ram";
}
-----------------------------
// Define add() - pass 2 int -args -perform addition and return result ?
package MethodBasics;
// Define add() - pass 2 int -args, return type - void -perform addition and
return result ?
// public static void add(int a, int b)// void method cannot return any value
// int -- return int value
// float - return float
public static int add(int a, int b)
{// 2 3 so a =2, b=3
System.out.println("Called add()");
int result = a +b;
// 2 + 3
// 5
// result = 5
System.out.println("result = "+result);
// result = 5
return result;
// 5;
// return ;
}
// call add by passing 2,3 and store result into var i.e x
add(2,3);
// 5
int x = add(2,3);
// 5
// x= 5
o/p:
Called add()
result = 5
Called add()
result = 5
after add. x= 5
-------------
Return val :
-------------
package MethodBasics;
// Define add() - pass 2 int -args -perform addition and return result ?
public static int add(int a, int b)// void method cannot return any value
{
// 10 20 so a= 10 b= 20
System.out.println("Called add()");
int result = a +b;
// 10 + 20
// 30
// result = 30
System.out.println("result = "+ result);
// result = 30
// return result;
// 30
return 25 ;// returns o/p : 25
// dont write return stmt inside void method.
}
// call add
int x = add(10,20);
// x= 25
System.out.println("x= "+ x);// 25
o/p:
Called add()
result = 30
x= 70
after add
--------------------
return val; --> return 10;
return var; --> return b;
// 20; --> returns 20 value
return expression;
-------------------
Method ReturnType Float :
---------------------
package MethodBasics;
// Note : if Method return type is float, we must return float val/ float
var
// return type must be written just before the method name
// ex" void, int, float, char, boolean, String also
package MethodBasics;
System.out.println("result="+ result);
// 4.0
return result ;
// return 4.0;
}
// call add () - pass 2.5f and 1.5f and store into int var x
// int x = add(2.5f,1.5f);
}
}
o/p:
result=4.0
result=4.0
x=4.0
-----------------------------
Method Return Type Char :
-----------------------
package MethodBasics;
// display result
System.out.println("ch ="+ ch);
o/p:
in side add
in side add
ch=A
------------------
public static char add() // Method with return type char - so method return
char val o/p
{
System.out.println("in side add");
return val/var/expression:
return 10;
int b=20;
return b; return val of variable b
//
return a+b-2; // evaluates expression and result is returned
HW WAP to define method to perform subtsraction and return the o/p and and display
result, perofrm result + 100 in main()
HW WAP to define method to perform division and return the o/p and display result
in main()
HW WAP to define method to get Remainder val and return the o/p and display
result in main()
WAP to define method i.e checkEvenNo and pass argument as int no,
if it is even no, return boolean value i.e true,
else return false value and display the result in main()?
}
package MethodBasics;
// call checkebenno () pass -2 and store result into some var result
boolean result = checkEvenNo(2);
// // result = true
// // this method gives o/p/ return o/p boolean value i.e
true
// display result
System.out.println("result="+result);
/// true
// call checkebenno () pass -3 and store result into some var result
result = checkEvenNo(3);
// result = false
// this method gives o/p/ return o/p boolean value i.e false
// return value i.e false will be stored in left side varible i.e
result .
// So Result var contains false value
// display the result
System.out.println("result="+result);
// false
// Note : if we dont know what method return value. move the mouse over
// on method call i.e checkOddNumber , dispays return type i.e char
// so left side , we must declare char var only but not int,
float,boolean
HW WAP to define method to check given no is odd no and return some 'char' o/p
if it is odd no -- return char 'Y'
else 'N'
---------------------------------------
Return type String :
-------------------
HW WAP to define method i.e checkEvenNo and pass argument as int no,
//if it is even no, return "Yes" value
// else return "No" value and display the result in main()?
-------------------------
******************************************************
29-Jul-2023
******************************************************
void : nothing
void method -- > return nothing
no value
if method's return type is void, that method returns nothing i.e it does not
return any value.
void - predefined k/w in java
can be used before method name
when to use: when we dont want to get any o/p from method -- we will go for void
return any
package MethodBasics;
// call add() ,pass 2,3 and store result into some var i.e result
// int result = add(2,3);// CE
// void it does not return any value
//error : this method does not return any o/p. so dont store result
into left side var
// if method return type is void, dont store o/p into some var /
dont declare any var left side
// Type mismatch: cannot convert from void to int
System.out.println("ends");
-----------------------
o/p:
res=5
ends
Types of methods:
---------------------------
1. user defined method
2. Predefined method
2.Predefined methods:
---------------------
method is already defined by programmer in java library
}
---------------
// Math -is predefined class in java
// which has some methods - to perform mathemtical operations
// sqrt , max , min
package PreDefinedMethods;
// Note : open max() code -Press ctrl + move mouse over method name
i.e max() . + click open Delcaration
// it goes to max() - we can see method body
}
o/p:
result max=5
min no =7
sqrt =2.0
-----------------------
WAP to square given no
squareNo(2); o/p ://4
2*2 = 4
package PreDefinedMethods;
return result;
// 4 ---> returns 4 value
}
System.out.println("in main");
SquareNo(2);
// 4
or
return n*n; // this also fine
We can write in this way also.
public static int SquareNo(int n)
{// 2 so n =2
return n*n;
// 2*2
// 4 ---> returns 4 value
}
---------------------------
****************************************
****************************************
package PreDefinedMethods;
System.out.println(add(2,3));
// 5.0 --> print 5.0
}
----------------------------
-------------------------
package PreDefinedMethods;
System.out.println("val ="+val);
// val =5
}
-----------------------
-------------------------
return 10;
return 3.45f;
package PreDefinedMethods;
// return arrayName;
return iarr; // 10 20 30
// Type mismatch: cannot convert from int[] to int
// [10,20,30]
}
-------------------------
// Ex
// public static float[] display()
// char []
// boolean []
{
// floar farr[]
// char charr[]
// boolean barr
}
}
o/p:
HW WAP to Define method and return float array and display array values in
main()
HW WAP to Define method and return char array and display array values in
main()
HW WAP to Define method and return String array and display array values in
main()
-----------------------------------------
30-Jul-2024
-----------------------------------------
// String :
------------
is collection of charcters
group of characters which must be enclosed in dbl quotes ""
//ex:
"abv123@#$", "123", "abc123"
// Declare String:
-------------------
Declare int var i.e a --> int a;
Declare float var i.e f --> float f
Declare String var i.e s --> String s;
package StringBasics;
//Note:
// 123 - int no
// "123" -string value
// Decmial nos
// s = 1.45f;// CE: Type mismatch: cannot convert from
float to String
o/p:
s=rama
String val =123
-------------------
//HW Can we store double value into 's' String variable ?
-------------------------
Important String class Methods:
-------------------------------
class String
{
int Length()
{
toUpperCase()
{
package StringBasics;
// l= 4
// display length
System.out.println("length ="+ l);
// 4
// HW s = "Ram" ; length= 3 ?
//String toUppercase():
// convert the given String to Upper case
and return String value
// "RaMa" --> RAMA
// RAMA --> RAMA
String sUppercase = s.toUpperCase();
// "RaMa".--> RAMA
// sUppercase = RAMA"
System.out.println("upper case String="+ sUppercase);
//ucaseString == "RAMA"
// "RAMA"
String s2 = "RAMA";
String supperCase2 =s2.toUpperCase();
// "RAMA" --> RAMA
System.out.println("supperCase2="+supperCase2);
// RAMA
// HW s = "sita" convert into upper case?
// s.toLowerCase()
s= "RaMa";
String sLowerCase = s.toLowerCase();
// "RaMa" --> rama
// sLowerCase = rama
System.out.println("sLowerCase="+sLowerCase);
// rama
// "RaMa" // covert the given String to
lower case
}
}
o/p:
length =4
upper case String=RAMA
supperCase2=RAMA
sLowerCase=rama
----------------------------------
charAt(int indexno) :
-----------------------
char charAt(int indexno)
{
package StringBasics;
// display char
// System.out.println("ch3="+ ch3);
// get each Char from String and display using 'for' loop
System.out.println("***************");
// for(int i=0;i<=2;i++) // using hard code value
for(int i=0;i<=s.length()-1;i++)
{// 3-1 =2
char ch = s.charAt(i);
// 0 1 2
System.out.println("get each char from String using
for loop="+ ch);
}
-----------------------------------
nested if - possible
nested for --possible
nested method - not possible --
package StringBasics;
2. Duplicate method with same data type args not allowed in java
Same method signature is not allowed .
public static void main(String[] arr) { // always execution start from main ()
which has signature
i.e Main() + String array...
package StringBasics;
-----------------
indexOf():
---------------------
String s = "RAMA";
// 0123
package StringBasics;
// 1
// -ve if given String val is not there i.e "P" in main string, this
method return -1
int indexOfP = s.indexOf("P");
// -1
// indexOfP = -1
System.out.println("indexOfP="+indexOfP);
// -1
// Note : if duplicates characters are there in String i.e "A ", it always
gets the first matched index no
// indexOfA1 == 1
// 1
LastIndexOf():
---------------
gets last index no of given string
String s = "rama";
// 0123
// s.indexOf("a");
s.lastIndexOf("a");
package StringBasics;
//
---------------------------------------------
// substr() :
------------
can be used to get sub part or some part of string from given index no to till
last index no
String s = "RAMA";
// 0123
String s0 = s.substring(0);
// get string values from index no 0 to all
package StringBasics;
String s = "RAMA";
// 0123
// substr(int index) : can be used to get sub part or some part of
string from given index no to till last index no
// get string values from index no 0 to all
------------------
Substring with 1 arg:
-----------------------------
substring(int startIndexNo)
substring(int startIndexNo , int endingindexNo)
------------------------------------------------
String s = "RAMA";
// 0123
package StringBasics;
---------------
package StringBasics;
// A
System.out.println("s12="+s12);
// AM
System.out.println("s13="+s13);
//
String s11= s.substring(1, 1);// get string vals from indexno =1 to 1-
1 = 0 i.e 1,0 wont display AR
System.out.println("s11-= "+ s11);
// empty val ***
o/p:
----
----------------------
HW Write o/p for below program String s = "RAMA Rao"; display ?
s.substring(2, 3);
s.substring(2, 4);
s.substring(2, 5);
s.substring(3, 6);
s.substring(3, 5);
s.substring(3, 4);