Friday, August 14, 2009

Programming Basics

Hello . I have been noticing for quiet a while that books about programming are quiet difficult so i decided to write a tutorial which explains programming in an easiy way .

So here is what you need to do . first of all i have chosen java in this tutorial because it is much more easiar then c language and it has a wider scope as compare to c language .

Well it do have some drawbacks but the best thing about it is that a programm written in java can run on any operating system with out any modification . while this is'nt always possible in c language.

So what is a program? it is basicly just like your servent or your employee . just give it some instructions and your program follows it . each of program can act on these kinds of basic expressions
  • Input and Output Instruction
Well just like your employee or servent your program also needs communication for this input and output instruction comes . well input are those instructions which gives your program information to perform work and output instructions are those instructions which program uses to show what it has done.
  • Mathematical Expression
Well these are just like the real life mathematical expressions . well unlike your servent or an employee your program can only work with mathematical expressions every thing your program does is done using mathematical expresions
  • Memory
Memory is important as your program needs to memorize stuff

  • Condition
Well your program needs to take decisions while processing some stuff so here our condition statements comes.
  • Jump
Well these statements gives your program an ability to jump from one part of it to another.


So here are the basics how a program works . now we are ready to write our first program.


First of what you need to do is to download Latest version of JDK(Java Development Kit) Click here to download the latest version. Ones Installed it is usually installed in c:\Program Files\java\jdk(version)\ where version is the version of your JDK.

Ones installed open command prompt which is located in start -> All Programs -> Accessories . remember that if you are using windows then right click command prompt icon and click run as administrator

then type cd \"Program Files"\Java and press enter in command prompt


then type dir to see which is the directory of jdk and then type cd jdk(version)\bin\ and press enter


now open notepad and type the following program there

import java.io.*;
public class hello{
public static void main(String[] args){
System.out.println("Hello...");
}
}

Save this program in "c:\Program Files\Java\Jdk(version)\bin\" or in my case "c:\Program Files\Java\jdk1.6.0_14\bin" folder as "hello.java"

Once Saved go to command prompt where you have opened the jdk folder and type javac hello.java to compile your program.if it does'nt show any thing then type java hello to run the program you have compiled



If successful you will be able to see "Hello..." in your command prompt.

So now you have compiled and executed your first program. now you want to know what is going on So here is the program which you have compiled in the figure below.click on the figure to inlarge it



Remember that java is a case sensitive language that means that for example in java this Case and this case is different

Now the next part is how these types of instruction which i have explained you above works in java . in most part instructions in most modren languages are combination two or more type of instructions . so first of all is

  • Memory & Mathematical Expression

Well in most programming languages as well as in java variables are used to store data . they act in most part like mathematical variables.

Variables are of Many Types some of these are given below

  • byte can store one byte or 8 bits (0 - 255) or (-127 - 127)

  • char same as byte but stores one character instead of a number

  • int this is a type which most often used in programs . this is a 32 bit variable . it can store from 0 - 1073741823

  • double This varible is 64 bit varible which stores numbers with decimal or floating point numbers . the number stored in this type is like 0.32425 , 34.352463 , etc

  • String This type stores text messages or simply english sentences in it .

Now i will show you an example which will explain these concepts.

import java.io.*;
public class hello{
public static void main(String[] args){

byte b;
char c;
int i;
double d;
String s;

b = 5;
c = 23;
i = 3563;
d = 0.4353;
s = "Hello this is a test";
System.out.println(b);
System.out.println(c);
System.out.println(i);
System.out.println(d);
System.out.println(s);



}
}

its output is this


To understand the program above see the figure below



Now if you understand this concept you can also understand the concept of mathematical expressions

below is an example program.

import java.io.*;
public class hello{
public static void main(String[] args){

int a;
int b;
int c;

a = 5;
b = 23;

c = a + b*a;

System.out.println(c);




}
}

output of this looks like this


Basicly mathematical expressions are just like real live mathematical expression . you can do any mathematical calculation in your program using these mathematical expressions



  • Condition & Jump Statements

In most modern programming languages including java the concept of condition and jump are combined together .by combination of these two statements we get the following instructions

- if and else -

if statement is the basic condition statement in most programming languages . its function is similar to its name . if some condition is true it executes instruction written in its block otherwise it redirects it to else part if else part is mentioned other wise it does not executes its block here is an example program with its description.


Its Output is 6


- for -

Both for and while are basically loops . What a loop does it that it executes statements in its block again and again until they are told to stop.

What for loop does is that you assign it a variable then tell it that that is its initial value and that is its final value . so the for loop will start to execute the block and sets the variable its initial value and continue to execute the block until the variable's final value is reached. here is an example program below and its description.





Its output is





- while -

Now what does while loop do . well what it does is that it checks a condition . on its start . when that condition becomes false the loop ends. here I will show you an example program

import java.io.*;
public class hello{
public static void main(String[] args){

int a;

a = 0;
while(a<10){
System.out.println(a);
a = a + 1;
}

}
}

the output of this program is same as that of for program. here is the explanation of how this program works



- Input and Output Statements -

Now in this last part I will show you how to take simple input from keyboard and show simple output on the screen . below is the program which take simple input from keyboard as a number in an int integer and outputs a simple output on the screen

import java.io.*;
public class hello{
public static void main(String[] args){


int a;
int i;

try{
i = System.in.read();
a = i + 15;
System.out.println(a);

}catch(Exception e){}

}
}

Output of the program is this

Now the explaination of this program is in the figure below


Now you know how to create a simple program now let us go to more concepts.

  • Arrays

Array is described as collection of elements each described by one or more index . in more common words array is type of varible in which you can store more then one value . each value can be accessed by its indicator called index .

here i will only show you some few basic types . once you have learned this you can learn rest from any advance java tutorial or documentation widely available on the internet

Well this is the example program and it description.


This is its output



Remember that number here [number] while reading or writing into your array can be any expression .

an expression is just similar to mathematical expression and can be any thing from a single variable name or single number to more complex mathematical expressions . just the values produced by it must be in limits from 0 to the maximum number of places you have defined in your arrays definition .

  • Function

function is the building block of any structured or object oriented programming language . a function is just like mathematical function . its like a machine you give it input it processes it and gives you output . and the similar machine can be used again and again. here is a figure describing a function.


In java every function is written in between a class never outside it .for simplicity write the function in different class as in your main class . here is an example program

import java.io.*;
public class hello{
public static void main(String[] args){
int a;
int b;
int c;
a = 3;
b = 7;
function t = new function();
c = t.sum(a,b);
System.out.println(c);


}

}



class function{
public int sum(int x,int y){
int z;
z = x + y;
return z;
}


}

its output is 10

so how does this program works . here is the description


remember that a variable defined outside a function can be accessed in a function . while a variable defined inside a function can not be accessed outside its function.

  • Classes

Well i am not going to explain you all the features of a class just its basics. Well a class is a collection of different variables and functions bunched together .

Here is an example of a class.

import java.io.*;
public class hello{
public static void main(String[] args){
int a;
int b;
int c;
a = 3;
b = 7;
function t = new function();
c = t.sum(a,b);
System.out.println(c);
c = t.sub(a,b);
System.out.println(c);


}

}



class function{

function(){
System.out.println("Hello...");
}

private int z;

public int sum(int x,int y){
z = x + y;
return z;
}

public int sub(int x,int y){
z = x - y;
return z;
}
}

its output is

So how does this program works here is its description


Well what public and private is that public are those kinds of functions and variables which are accessed every where in our program and private are those function and variables which can only be accessed in that class only.

So this was the beginners tutorial.I hope you have understand it well if not then you can give me comments on how to improve this tutorial .

the next tutorial on this blog will be about how to read and write files in java and how to create a database using mysql