Session - 1
Introduction to
Introduction
Why do we need to use Java ?
Why do we need to use Java?
Java Program written in OS
Why do we need to use Java?
Online editors
Class_name:
They are case-sensitive, so name them carefully. For better practice start it with capital letter and you can use other symbols and numbers. It can be of any length.
And most important make sure the name you are giving to your class is not a keyword in java.
public static void main(String[] args):
Main function declaration, the code inside main function is where the execution of the program starts.
Your first Java code
Click on Execute to run the Code
Character | Action |
\n | New line |
\t | Tab(horizontal) |
\\ | insert a backslash character |
\’ | Single quote |
\” | Double quote |
\0 | Null |
Escape characters Java
What are Data Types :
13
(c) Rajkumar
Java Variables
local, instance and static.
14
(c) Rajkumar
NOTE : When we write a statement with a data type and a variable
it reserves space in memory to store the value.
This is the declaration of the variable.
int i=10;
10
Type :int / integer
Name of variable : i
15
(c) Rajkumar
16
(c) Rajkumar
Operators in Java
This presentation uses a free template provided by FPPT.com
www.free-power-point-templates.com
Types of operators in java
This presentation uses a free template provided by FPPT.com
www.free-power-point-templates.com
Arithmetic Operators
Operator | Name | Description |
+ | Addition | Add two values |
- | Subtraction | Subtracts two values |
* | Multiplication | Multiplies two values |
/ | Division | Divides one values by another |
% | Modulus | Returns the division remainder |
This presentation uses a free template provided by FPPT.com
www.free-power-point-templates.com
Code for Arithmetic operator
This presentation uses a free template provided by FPPT.com
www.free-power-point-templates.com
Comparison operator
operator | Name | Description |
== | Equal to | Compares both the values are equal or not. |
!= | Not equal | Compares both the values are not equal. |
> | Greater than | Compare weather the first value is Greater than the second one. |
< | Less than | Compare weather the first value is Less than the second one. |
>= | Greater than or equal to | Compare weather the first value is Greater than or equal to second one. |
<= | Less than or equal to | Compare weather the first value is Less than or equal to second one. |
This presentation uses a free template provided by FPPT.com
www.free-power-point-templates.com
Code for comparison operator
Comparison operator always returns a Boolean value
This presentation uses a free template provided by FPPT.com
www.free-power-point-templates.com
Logical Operators
Operator | Name | Description |
&& | Logical And | Returns true if both statements are true |
|| | Logical OR | Returns true if one of the statements is true |
! | Logical Not | Reverse the result, returns false if the result is true |
This presentation uses a free template provided by FPPT.com
www.free-power-point-templates.com
Code for Logical operator
This presentation uses a free template provided by FPPT.com
www.free-power-point-templates.com
�Ternary operators�
Ternary operator works just like if else statements if the first condition is true it prints the statements else it goes for else part.
Syntax: variable=Expression1?Expression2:Expression3
If the Expression 1 is true it will assign the Expression 2 to the variable or else it assigns Expression 3 to the variable.
This presentation uses a free template provided by FPPT.com
www.free-power-point-templates.com
Code for Ternary operator
This presentation uses a free template provided by FPPT.com
www.free-power-point-templates.com
Increment operator
Increment operator is used to increase the value by one
Symbol:++
We can use increment operator(++) as prefix and postfix.
This presentation uses a free template provided by FPPT.com
www.free-power-point-templates.com
Code for Increment operator
This presentation uses a free template provided by FPPT.com
www.free-power-point-templates.com
Decrement operator
Decrement operator is used to increase the value by one
Symbol: --
We can use increment operator(--) as prefix and postfix.
This presentation uses a free template provided by FPPT.com
www.free-power-point-templates.com
Code for Decrement operator
After post fix what happens to the value b?
This presentation uses a free template provided by FPPT.com
www.free-power-point-templates.com
Java Control Statements
Java Session 4
IF – then Statement
if(condition)
{
Statements;
}
Flowchart for IF-then
Condition
True
False
True Statements
Continue with the rest of the code
Code for If -then
IF ELSE Statement
After you write your exams your are awarded grades for that. Grades are awarded according to the marks scored in this way.
Marks | Grades |
>=90 | A |
>=80 | B |
>=70 | C |
>=60 | D |
Flowchart for if else loop
Marks
is equal to condition
True
False
True statements
False statements
Code for if–else loop
IF-ELSE-IF LOOP
After you write your exams your are awarded grades for that. Grades are awarded according to the marks scored in this way.
Marks | Grades |
>=90 | A |
>=80 | B |
>=70 | C |
>=60 | D |
Syntax for IF-ELSE-IF loop
If(condition)
{
statement;
}
else if(condition)
{
statement;
}
….
….
….
else
{
statement;
}
Flow chart for if -else-if loop
condition
True
false
Condition
True
false
True statements
True statements
False statements
Code for IF-ELSE-IF LOOPS
Switch Case
Switch case is similar to the if else loop:
Syntax for Switch case
switch(expression/ value)
{
Case x : code block;
Break;
Case y : code block;
Break;
……
……
Default: code block;
Break;
}
Note:
Break and Default are keywords in switch - case.
Break : when a match is found, execution will stop and program breaks out from the switch case loop.
Default : It is executed only if there is no case match.
Flowchart for Switch case
Code for switch case
Loops using GUI
�FOR Loops In Java using GUI�
This presentation uses a free template provided by FPPT.com
www.free-power-point-templates.com
What is Loop?
Example: Monkey jumping 3 times in a Loop
After jumping 3 times it will exit the loop
This presentation uses a free template provided by FPPT.com
www.free-power-point-templates.com
For loop
Flow chart for FOR loop
condition
Statements
Increment/decrement
Stop
false
true
Design a square using FOR loop
Activate the pen with size 10
Set the loop to repeat for 4 times
Move the pen forward 100 units and turn 90 degrees
Fill the shape with yellow color
This presentation uses a free template provided by FPPT.com
www.free-power-point-templates.com
Design a flower using for loop
Activate the pen with size 3
Set the loop to repeat for 10 times
Make an Arc with radius 100
Turn Left to draw another Arc
This presentation uses a free template provided by FPPT.com
www.free-power-point-templates.com
Design a Square Spiral using FOR Loop
Activate the pen with size 3
Set the loop to repeat for 40 times
Move the pen forward
x * 5 units and turn 90 degrees
This presentation uses a free template provided by FPPT.com
www.free-power-point-templates.com
What is Nested for loops
Design the shape using Nested for loops
output
What is IF loop
Loop to draw different polygons using if statements
What is if else loop
If(condition)
{
statements;
}
else
{
statements;
}
Program for if else
What is nested if else if loop
If(condition)
{
statements;
}
else if(condition)
{
statements;
}
else if (condition)
{
statements;
}
Program for nested if
Output
Loops in java
While loop
Syntax for while loop:
While (condition)
{
Statements to be executed;
}
Flow chart for While Loop
start
Statements
Exit Loop
stop
condition
true
false
How while Loop works
int i=0;
While(i<3)
{
System.out.println(“Hi”);
i++;
}
0
true
Storage container
int
i
Output
condition result
condition
increment
Hi
Hi
Hi
1
true
2
3
false
Code for while loop
For loop
Syntax for FOR Loop
for(initialization; condition; increment / decrement)
{
code inside the loop;
…….;
…….;
…….;
}
Flow chart for FOR loop
condition
Statements
Increment/decrement
Stop
false
true
How for Loop works
for(int i=0;i<3;i++)
{
System.out.println(“Hi”);
}
0
true
Storage container
int
i
Output
condition result
condition
increment
Hi
Hi
Hi
1
true
2
3
false
Code for For loop
��Java program to print the sum of first 20 natural numbers�
Program for factorial
Do while
Syntax for do while
do
{
loop statements;
loop statements;
…;
…;
}while(condition)
Flow chart for do while loop
statements
condition
stop
false
True
How do while works
int i=0;
do{
System.out.println(“Hi”);
i++;
}while(i<3)
0
true
Storage container
int
i
Output
condition result
condition
increment
Hi
Hi
Hi
1
true
2
3
false
How do while works
int i=4;
do{
System.out.println(“Hi”);
i++;
}while(i<3)
4
Storage container
int
i
Output
condition result
condition
increment
Hi
5
false
Code for do while loop
Functions in Java
Functions
Function is a self contained block of statements which performs a particular task. In java functions are also called as methods.
when we are doing some actions we can take few actions as procedure and give it a name.
Syntax for declaring functions
Access specifier Access modifier void methodName()
{
Code to be executed //method implementation
……..
}
Access specifier= public or private
Access modifier =static or non static
Sample program
Function returns a value
Syntax:
Access specifier access modifier returntype method/function name()
{
----------------
----------------
.
.
.
----------------
}
Implementation
Sample program
Function with Parameters
Sample program
In the above program
X and Y are called as Arguments.
a and b are called as parameters.
Scanner
Methods available in scanner class
Method | Description |
nextByte() | Reads a byte value from the user |
nextShort() | Reads a byte value from the user |
nextInt() | Reads a byte value from the user |
nextFloat() | Reads a byte value from the user |
Nextdouble() | Reads a byte value from the user |
nextBoolean() | Reads a byte value from the user |
nextLong() | Reads a byte value from the user |
nextLine() | Reads a byte value from the user |
Sample Program
Sample Program 2
Calculator Project
Calculator Project
Input & Output Operations
To use Mathematical Operators
To take input from users
Define your main method inside the Calculator class
Calculator Project
Declaring to variables to store number
This command helps us for user input
Take Operation Input
Calculator Project
Here we used switch-case
Based on the operator selected the operation is carried out on the numbers
Calculator Project
Calculator Project Output