Java Tutorial Part-1

Buffer Method
In this method,the value of the variables are input while the program is executed.
So before learning this,you need to know a few things.
First we need to allow input and output statements.So we will use a line
"import java.io.*;" at the beginning of every program.
You also need to activate buffer by using a pair of lines


Code:
Input Stream Reader read =new Input Stream Reader (System.in);
Buffered Reader in=new buffered Reader (read);
Every program that needs buffer must have these lines to execute properly.
Now since the value is entered during execution,you wanna display something like "Enter the value of a and b"
So inorder to enter,first you need to initialize variables so that values can be entered into it.
Then for bringing a cursor so that values can be entered,You need to study another line :


Code:
a=integer.parseInt(in.readLine()); if its an integer
a=double.parseDouble(in.readLine()); if its double
a=in.read(); if its char
a=in.readLine()); if its String
So here is a quick program to find the sum of 2 numbers using buffer:
Code:
import java.io.*;
class sum
{
public static void main(String args[])
{
Input Stream Reader read =new Input Stream Reader (System.in);
Buffered Reader in=new buffered Reader (read);
int a,b,sum;
System.out.println("Enter two numbers");
a=integer.parseInt(in.readLine());
b=integer.parseInt(in.readLine());
sum=a+b;
System.out.println("The sum of numbers is : "+sum);
}
}
 

 So in this program,the system will ask us to enter two numbers and proceed to do the rest.Bit more slick and awesome

Mathematical Functions
Now we are going deep into program..We will be learning to use some functions.
Some of them are to find square root,rounding,etc
So if you need to find the square root of a number,say x, the syntax is :


Code:
a=math.sqrt(x)
For rounding it is :

Code:
a=math.round(x)
These are just some commonly used commands.You can use math.max , math.min , math.pow (To raise to a power ) etc.


LOOPS
Next we are gonna learn loops
So first is the For Loop
A for loop consists of three parts.
Here is the syntax :


Code:
for(i=1;i<5;i++)
{
Statements
}

So the three parts are The initial value (i=4)
The test Condition (i=5)
The increment or decrement given by ++ or --
So first ,the system checks the initial value of i.In this case,its 1.Now the condition for the loop is checked which is whether i<5.
So if the condition is true,the statements inside the loop is executed.It can be anything ....
And after the loop execute 1 time,the value of i is increased by 1 since i++ is given which will increase i's value by 1.
The loop executes till the condition becomes false.In this case 4 times.
So for loop can be used to execute some commands a fixed number of times without needing to repeatedly type it 4 times