Sunday, July 22, 2012

Lecture 2:- Our First C++ Program

So lets start our programming journey from this lesson. I'll not go into the history of languages, but if you are interested, you can use google.

Today we'll see our first C++ program to get familiar of basics of compiler and C++ syntax. We'll use an open source C++ compiler "Dev C++". You can download it from , Its completely free. Now first have a look at the following program, then I'll explain it for you.


The first line of our code consists off "#include". This is a preprocessor directive. #include isn't the C++ statement, but its an instruction for the compiler. It asks compiler to include the content of the file mentioned in "<>". So when compiler compiles the code, it also includes the contents of the "iostream.h" file. IOStream.h is a header file saved in installation directory of compiler. It a library file having definition of Input and Output streams. It means that if we write or output something on the computer screen, or take or read input from the keyboard, we'll use this file. Because the functions that write on the screen or read from the keyboard are defined in this file.

The second line is main(). main is special function in C++. C++ program start executing from this point. Its the first step or first stair of our program. {} are called body of the main function. (We'll discuss functions later in detail).

Saturday, July 21, 2012

Lecture 1:- What is Programming

Welcome to Introduction to programming series. In this program I am going to teach you computer programming. We'll talk about the concepts (Basic to advance). We'll use C++ to explain programming concepts and to give examples. But these concepts are generic and can be implemented or used with any language. So should we start now????????

So guys what is the programming. First of all this is very basic question that we will answer. Programming is telling the computer what to do. We perform some task using computer and through programming we ask computer that perform this task for us. We tell computer by using a specific language (Programming Language), which computer understands. We write statements of that language and collection of that statements is called program. But this isn't precise definition of the program.

"Program is a precise sequence of steps to perform a particular task".

So here we will look at few things in little detail. "Precise sequence" means we have to do one task first and then do second task. Their order cannot be interchanged. So all steps must be performed in a sequence. By changing this sequence we cannot achieve the desired output.

Why we learn programming :- Now some people might ask this question. We learn programming to solve real world problems. You can see huge amount of application today that are helping us in almost every field of life. In scientific research, education, office work, medical and many many more field there are computer being used to solve problems and to facilitate us, and inside those computers programs are running. Of course there are pretty nice code generators are available today but they are very generic and cannot provide exactly what e want.

Keep in mind that programming is a creative activity. Its an art, so you must develop your analytic and critical thinking and creativity. When you are writing program, keep following points in mind.

Pay attention to details:- What does this means, you can't be vague. You should be very clear about what you are doing. Keep every aspect in your mind. Don't miss any minute detail or point. You must sure what you are doing. If you are not 100% sure, you can't create.

Computers are stupid:- They do not do anything on their own. They do what we ask them to do. So don't ever suppose "Oh computer will do the rest of things". Computers require exact input every time. They are not like humans where you ask you r friend "give me your pen" or "pen please" or just "pen" and his/her response is always same. This isn't the case with computers.

Re usability:- Its another important aspect in programming. When you program always think about to reduce your effort, time and money. So when write program, write in a way that it can be re used easily (make functions and classes).

Comment the code:- Always comment the code and take notes. Because you will forget your logic after few days or months. In that time you r comments will help you. Otherwise you have understand you code again and this will be time consuming.

ArrayList in Java

Last time we discussed array in java and i told you that its a fixed size data structure. You have to tell the compiler the size of array. But what if you don't know how many data item you will have in future. The arralist is the answer. It is a dynamic data structure. You don't need to tell the size, just declare it and keep adding the item in arraylist. It grows or shrinks as you add or remove item from arraylist.
In java to use arraylist you must import the java.util package. Here is how you declare the arralist.

ArrayList al = new ArrayList();

Arraylist has many methods, Here I am telling few useful ones.

add():- This adds an item to arraylist. In () or as arguments we provide the element to be added. Yon can add as many item as you want.

get():- This method is used to retrieve back items from the arraylist. In () we mention the index of the arraylist (as you studied in array). We tell the locationof element and it sends the item back to us.

remove():- This item is used to delete an arraylist item. In () again we supply the index or location. After removal the arraylist gets shrink.

int size():- This method tells the size or number of arraylist items.

You can add the object of any class in arraylist too. Here i am using Student class which we are using through out our course.

Student S1 = new Student("mike", 20);
Student S2 = new Student("rachel", 22);

al.add(S1);
a2.add(S2);

Its very simple and fair to use arraylist and its methods. Another useful method is to check whether arraylist is empty or not. Ths "isEmpty()" method can be used. This method returns true or false according to arraylist condition.
So with this our lecture ends. Bye

Friday, July 20, 2012

Arrays in Java

Array is a collection of variables or its a bunch of memory locations, you can say in simple word. Array is a very basic data structure, can have multiple locations or memory slots where we can keep our data. For example if create an integer variable, you can keep one number in it. But what if want to store 5 or 20 or 100 different numbers. Answers is the array. Array has a fixed size, it means if you have declared an array of size 100, you can change it to any other size after creation. We use a number called "index of array" to access different elements or members of the array. This index starts from 0, so in we want to use 3rd element of the array, we will use index value 2. The each location is one element of the array. So lets see how to declare an array.
There are few ways to declare an array.

int[] myArray;
int myArray[];
int myArray[] = new int[4];

you can give the size of array in brackets as I did in third example, or leave it open and define the size later like this.

int myArray[];
myArray[] = 12,33,544,71;

This array will now confirmed as 4 location array.

Now if you want to use array elements in your program, you can use following method.

System.out.println(myArray[2]);

This will print the third element of the array on screen. But what if want to print 1000 elements of the array. Would you write 1000 lines of System.out.println. No we will use for loop as follows.

for(int i = 0; i < 1000; i++)
{
System.out.println(myArray[i]);
}

Same technique can be used to put data in array. You must do the exercise yourself for better learning.
Array has a drawback of having fixed size. In next lectures we will discuss some other data structures that can be re sized. So stay with us.

Thursday, July 19, 2012

Garbage Collection in Java

In C++ when we create an object using "new" operator, the memory is allocated for this object and we use it in our program. When we think that we do not need this object anymore, we return this memory to the heap and hence this memory is available for other applications (programs). This operation is done by using "delete" operator in C++. But if we don't delete this object and make it unreferenced, then we will never get this memory back until we restart our computer. Because it is allocated for our program and no other program can use it until we free it. And if this process of allocation and not returning back is in loop, then after sometime our memory will get finished and computer will stop responding. The unavailability of this unreferenced memory is called memory leak. It is programmer's responsibility to free unused memory for other applications.
Java frees the programmer from this responsibility. The JVM (Java Virtual Machine) takes this responsibility. It look after some time for unreferenced memory and if find any memory locations, it sent back them to heap. Now lot of problems are solved with this technique. This process is called garbage collection. The Garbage collector of java runs and checks, if there is any unreferenced memory is there left by any java program and makes it free for other programs.
Garbage collector runs when it feels that most of CPU resources are free. If there are many heavy applications are running there, then garbage collector will not run. Anyway programmer is free now. GC (garbage collector) runs automatically but you can request to run GC to JVM. Your request will not instantly invoke the GC, again it depends on CPU resources and some other factors.
To request for invoking the gc Systemgc() method is used.