1 of 102

Session - 1

Introduction to

2 of 102

Introduction

  • Humans need languages to communicate with each other,
  • Similarly we need a language to communicate with the computers.
  • Programming languages are used to communicate with the computer
  • Java was introduced in 1991 by Sun Microsystems

3 of 102

  • James Gosling from Sun Microsystems and his team were working on the first version of java.

4 of 102

  • Later it was taken over by ORACLE.
  • First it was named as Oak, then they got to know a programming language called Oak already exists.
  • Later it was named as JAVA.

5 of 102

Why do we need to use Java ?

6 of 102

Why do we need to use Java?

  • Java is a very powerful language.
  • Java is platform independent that means that you can run the same program on Windows, Linux, MAC OS etc.
  • Platform independent means that you write code any one platform but the code can be executed in all the platforms.

Java Program written in OS

7 of 102

Why do we need to use Java?

  • It is an object oriented language with features like inheritance, polymorphism, encapsulation, abstraction.
  • It is secure since it runs programs in a sandbox.
  • You can carry out multiple operations in parallel.
  • You can make your own applications like calculators , games etc, manage data in the database and also contribute to the open source world through Java.

8 of 102

Online editors

9 of 102

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.

10 of 102

Your first Java code

Click on Execute to run the Code

11 of 102

Character

Action

\n

New line

\t

Tab(horizontal)

\\

insert a backslash character

\’

Single quote

\”

Double quote

\0

Null

  • When we type something in word document we have enter key, tab key, space key etc to make proper spacing.
  • When we do the same thing in a java program we need something to play with space.
  • So we have escape sequence.

12 of 102

Escape characters Java

13 of 102

What are Data Types :

  • Data types are keywords which indicate the type of value a variable will hold.
  • It can be string, integer, float ….etc.
  • We can choose different types of variable names to store the values.
  • The names should start with lowercase letters and it can have numbers.

13

(c) Rajkumar

14 of 102

Java Variables

  • A variable is a container which holds the value while the Java program is executed. A variable is assigned with a data type.
  • Variable is a name of memory location. There are three types of variables in java:

local, instance and static.

14

(c) Rajkumar

15 of 102

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 of 102

16

(c) Rajkumar

17 of 102

Operators in Java

This presentation uses a free template provided by FPPT.com

www.free-power-point-templates.com

18 of 102

Types of operators in java

  • Arithmetic operator
  • Comparison operators
  • Logical operators
  • Bitwise operators
  • Ternary operators
  • Increment operator
  • Decrement operator

This presentation uses a free template provided by FPPT.com

www.free-power-point-templates.com

19 of 102

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

20 of 102

Code for Arithmetic operator

This presentation uses a free template provided by FPPT.com

www.free-power-point-templates.com

21 of 102

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

22 of 102

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

23 of 102

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

24 of 102

Code for Logical operator

This presentation uses a free template provided by FPPT.com

www.free-power-point-templates.com

25 of 102

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

26 of 102

Code for Ternary operator

This presentation uses a free template provided by FPPT.com

www.free-power-point-templates.com

27 of 102

Increment operator

Increment operator is used to increase the value by one

Symbol:++

We can use increment operator(++) as prefix and postfix.

  1. If you use the ++ operator as prefix like ++variable. The value of variable is incremented by 1 then, it returns the value.
  2. If you use the ++ operator as postfix like variable++. The original value of variable is returned first then, age is incremented by 1.

This presentation uses a free template provided by FPPT.com

www.free-power-point-templates.com

28 of 102

Code for Increment operator

This presentation uses a free template provided by FPPT.com

www.free-power-point-templates.com

29 of 102

Decrement operator

Decrement operator is used to increase the value by one

Symbol: --

We can use increment operator(--) as prefix and postfix.

  1. If you use the -- operator as prefix like --variable. The value of variable is decremented by 1 then, it returns the value.
  2. If you use the -- operator as postfix like variable--. The original value of variable is returned first then, age is decremented by 1.

This presentation uses a free template provided by FPPT.com

www.free-power-point-templates.com

30 of 102

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

31 of 102

Java Control Statements

Java Session 4

32 of 102

IF – then Statement

  • IF - then Loop is a basic of control flow statements.
  • IF the condition is satisfied then the commands in the brackets is executed.
  • Syntax

if(condition)

{

Statements;

}

33 of 102

Flowchart for IF-then

Condition

True

False

True Statements

Continue with the rest of the code

34 of 102

Code for If -then

35 of 102

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

36 of 102

Flowchart for if else loop

Marks

is equal to condition

True

False

True statements

False statements

37 of 102

Code for if–else loop

38 of 102

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

39 of 102

Syntax for IF-ELSE-IF loop

If(condition)

{

statement;

}

else if(condition)

{

statement;

}

….

….

….

else

{

statement;

}

40 of 102

Flow chart for if -else-if loop

condition

True

false

Condition

True

false

True statements

True statements

False statements

41 of 102

Code for IF-ELSE-IF LOOPS

42 of 102

Switch Case

Switch case is similar to the if else loop:

  1. Switch expression is evaluated once.
  2. The value of expression is compared with the value of each case, if it matches then the associated block of statements is executed.

43 of 102

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.

44 of 102

Flowchart for Switch case

45 of 102

Code for switch case

46 of 102

Loops using GUI

47 of 102

�FOR Loops In Java using GUI

This presentation uses a free template provided by FPPT.com

www.free-power-point-templates.com

48 of 102

What is Loop?

  • In programming language Loops are used to repeat the statement until a particular condition is satisfied.

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

49 of 102

For loop

  • When we know the exact number of times the Loop has to be executed then we can use a FOR loop.
  • A range of values can be specified in a FOR Loop.

50 of 102

Flow chart for FOR loop

condition

Statements

Increment/decrement

Stop

false

true

51 of 102

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

52 of 102

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

53 of 102

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

54 of 102

What is Nested for loops

  • If a loop exists inside the body of another loop, it’s called a nested loop. Here’s an example of the nested loop.

55 of 102

Design the shape using Nested for loops

output

56 of 102

What is IF loop

  • IF Loop is the basic loop of control flow statements.
  • If the condition is true then block of statements are executed.

57 of 102

Loop to draw different polygons using if statements

58 of 102

What is if else loop

  • If – then – else loop is used to provide the alternate path if the condition is false.
  • Example

If(condition)

{

statements;

}

else

{

statements;

}

59 of 102

Program for if else

60 of 102

What is nested if else if loop

  • If -else- if loop is used to execute when the condition is false check for another condition.

If(condition)

{

statements;

}

else if(condition)

{

statements;

}

else if (condition)

{

statements;

}

61 of 102

Program for nested if

Output

62 of 102

Loops in java

63 of 102

While loop

  • While Loop continually executes the block of statements as long as the condition is true. Once condition is false it will break out of the Loop.

Syntax for while loop:

While (condition)

{

Statements to be executed;

}

64 of 102

Flow chart for While Loop

start

Statements

Exit Loop

stop

condition

true

false

65 of 102

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

66 of 102

Code for while loop

67 of 102

For loop

  • When we know the exact number of times the Loop has to be executed then we can use a FOR loop.
  • A range of values can be specified in a FOR Loop.

68 of 102

Syntax for FOR Loop

for(initialization; condition; increment / decrement)

{

code inside the loop;

…….;

…….;

…….;

}

69 of 102

Flow chart for FOR loop

condition

Statements

Increment/decrement

Stop

false

true

70 of 102

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

71 of 102

Code for For loop

72 of 102

��Java program to print the sum of first 20 natural numbers

73 of 102

Program for factorial

  • The factorial function (symbol: !) says to multiply all whole numbers from our chosen number down to 1. 
  • 7!=7x6x5x4x3x2x1
  • Factorial zero is equal to 1.

74 of 102

Do while

  • Do while is a variant of the while Loop.
  • The condition is given at the end of the loop, so the loop will get executed at least once.
  • Rest of the functionality remains the same as while loop.

75 of 102

Syntax for do while

do

{

loop statements;

loop statements;

…;

…;

}while(condition)

76 of 102

Flow chart for do while loop

statements

condition

stop

false

True

77 of 102

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

78 of 102

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

79 of 102

Code for do while loop

80 of 102

Functions in Java

81 of 102

Functions

  • What is a Function?

Function is a self contained block of statements which performs a particular task. In java functions are also called as methods.

  • How it is useful in real-time?

when we are doing some actions we can take few actions as procedure and give it a name.

82 of 102

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

83 of 102

  • We have to create functions/methods always in inside the class.
  • Function/Methods are created for reusability and to identify errors quickly.
  • We can declare methods, before creating the main method because JVM always executes the main method first.

84 of 102

Sample program

85 of 102

  • In the above sample program execution starts from main method
  • When test1 method is called it goes to the test1 method and executes the test1 implementation and returns to the main method.
  • When test2 method is called it goes to the test2 method and executes the test2 implementation and returns to the main method.
  • And finally executes the final statements in the main method.

86 of 102

Function returns a value

Syntax:

Access specifier access modifier returntype method/function name()

{

----------------

----------------

.

.

.

----------------

}

Implementation

87 of 102

  1. In the above syntax , if the function has to return integer then the return type should be int.
  2. if the function has to return string then the return type should be string.
  3. If the function has to return double then the return type should be double.

88 of 102

Sample program

89 of 102

Function with Parameters

  1. We can pass the data to the functions as parameter. Parameters acts as variables.
  2. Parameters are specified after the method name inside the ()”parentheses”. You can pass as many as you want by separating with comma “ , ” .

90 of 102

Sample program

In the above program

X and Y are called as Arguments.

a and b are called as parameters.

91 of 102

Scanner

92 of 102

  • Scanner class is used to get the input from the user. To work with the scanner class import java.util.Scanner;
  • To use the Scanner class, create an object of the scanner class and use available methods present in the scanner class

93 of 102

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

94 of 102

Sample Program

95 of 102

Sample Program 2

96 of 102

Calculator Project

  • Basic functionality of a calculator is to
    • ADD
    • SUBTRACT
    • MULTIPLY
    • DIVIDE

  • Let us design a Calculator which takes 2 numbers from the user and the operation and performs the calculation.

97 of 102

Calculator Project

Input & Output Operations

To use Mathematical Operators

To take input from users

Define your main method inside the Calculator class

98 of 102

Calculator Project

Declaring to variables to store number

This command helps us for user input

Take Operation Input

99 of 102

Calculator Project

Here we used switch-case

Based on the operator selected the operation is carried out on the numbers

100 of 102

Calculator Project

101 of 102

Calculator Project Output

102 of 102