Friday, 29 October 2010

Iteration or Java Loop

From the very beginning of computer or its predecessor that is the automated calculating machines, the primary goal of the scientists was to create a machine which can do the repetitive tasks accurately. For example I a human being is asked to manually add 1000 numbers every day then you can be sure that most humans will start making errors because we are not good at doing repetitive tasks.
Therefore every computer is good at this ie repetitive task. If you ask it to do the same task 4000 times you can be rest assured that each time it will do that task consistently and accurately without any chance of variation or error.In computer terminology repetition is also known as iteration or simply put it is called a loop. Say for example we had these lines

START
DISPLAY “Hello World”
STOP

If I had to print “Hello World” 10 times then I have two options either to write the statement 10 times or create a loop. The first option is not feasible because it beats the purpose of using a computer.
Therefore the only option is to create a loop. A loop by definition is set of instruction that forces the computer to do one or more lines of instructions repeatedly for n number of times. Please remember most often loops have a starting and ending point just like when we count anything. Let us look at the Java’s implementation of a loop or iteration



The lines inside the inner purple box are called a loop construct. If we break it down it is quite simple to understand

Part 1
int i=1 means that this is our starting point a variable has been given a value of one to signify that we will count from 1
Part 2
i<=10 means that the value of i will change until it is less than or equal to 10 in other words we will count from 1 to 10. This is called the condition or the limit. If this condition becomes false then the loop will terminate.
Part 3
i++ means that each time we have to increment the value of i we will increment it by one just like normal counting 1,2,3,4…10
These three parts instruct the CPU to repeat these lines until i become greater than 10 and each time increment i’s value by one. The really interesting part is that each time it increments the value it will repeat the lines written inside the body of this statement (the opening and closing braces). Thus step one will be when i gets the value of 1 it will print Hello World then it will increment the value of i by one to make it 2,  check the condition if i is less than or equal to 10 if it is true it will print Hello World, this is step2 again it will increment the value by one, check  the condition and if true it will print Hello World. These steps will be repeated until the value of I becomes greater than 10 which will make the condition false and the loop will terminate. To illustrate this point let me repeat the above program with the value of i with the message



The output of this program will clearly illustrate my point





Monday, 14 December 2009

Java programs without BlueJ

It is quite simple to make Java program without BlueJ

Download Java


Install the package

Set up the environment first

Go to Control Panel and open System then choose the Advanced System Settings

It is quite simple to make Java program without BlueJ but you must set up the environment firstGo to Control Panel and open System then choose the Advanced System Settings In the new window choose Environment Variable



Check if CLASSPATH has been set and the path variable has the path to your JDK folder. You must remember if you set it up yourself then put a dot as the first item in the CLASSPATH and then the path where your jar files are stored(search for them)

Make sure you put the path where your javac.exe file is stored most probably it will be inside on this path C:\Sun\SDK\jdk\bin

Once this is done you are ready to go

Fire up NotePad and type the following snippet

[caption id="attachment_28" align="aligncenter" width="512" caption="First Java program"]

Now you must remember 3 things

1) Where you are saving this code

2) Name of the file should be entered in the file save box as "ABC.java" Nothe double quotes, it is important you do this or else in most systems notepad will save it as ABC.java.txt.

3) The name of the file must be same as the class name.

[caption id="attachment_29" align="aligncenter" width="650" caption="Mind the doule quotes and name of the file"]

Compile and Run

Open the DOS prompt, if you cannot find it on vista just type cmd on the start menu and the icon shown will be DOS Prompt.

I hope you know where you have stored your file ABC.java, navigate to that place. I will not cover DOS commands, if you need help leave a comment

When you have reached the place where you have stored the file just type the following command

javac ABC.java

if there is any error it will show up other wise it will show nothing just the prompt will come back

then type java ABC

and you should get a message Hello World just below the prompt, congratulation you have your first Java program

If you have any queries please feel free to leave comments for me

Friday, 10 April 2009

Learn Programming Easily Step 4

In the last post I had discussed the String or Alpha-Numeric Constant. In this post I shall endeavor to discuss Numeric Constants. They are the most important constants in programming. And you must learn to use them properly if you have to learn programming easily. We must be able to differentiate between alpha-numeric and numeric constant. If a student is unable to this basic task then the whole point of learning programming is defeated.


First question might be what are numeric constants? To put it simply any number without an enclosing quotation mark is a numeric constant. For example

1

23.45

45678

326478


These are all valid numeric constants. Now if we have to see the difference between valid and invalid numeric constants.


Constant
Valid/Invalid
5.345
Valid
"5.345"
Invalid
Sudeep45
Invalid
245
Valid

I hope by now you understand the difference between a valid and invalid constant. A numeric constant may be preceded by a sign +.-. For Example

-34
+678
In the previous examples these signs were missing. When it is a valid numeric constant and there is no sign then please assume them to be positive because the computer does the same thing. These are some small things you must remember if you want to learn programming easily.Let us go back to the core concept of constant and see if the following instructions are correct.

34.56=67
"Sudeep"=45
34="Sandeep"
47=67=32

Let us analyze these examples and see if they are valid. First line essentially says 34.56 equals to 67 and please note it is not a question but an assertion. Thus computer is trying to put the value of 67 in 34.56. Now that is quite stupid. Both the numbers are different with different values we cannot replace one value with another that is not possible is it?. Second example shows a Alpha-Numeric Constant being given a value of 45. So what it is trying to do is say the word or the name Sudeep means 45. Again it is not question but an assertion. Lets say your name is John and I say John means 23. Does it make any sense. No, it has no logic and as I had mentioned programming is all to do with logic. One more important point to note is that the name Sudeep is enclosed in double quotes thus making it is an alpha-numeric constant. Therefore by definition a constants value cannot be changed. 5litre does not mean 3 tommorow why because 5 has a fixed value or a constant value which cannot be changed. Same way anything inside a double quote has a fixed value and its value cannot be changed.

I hope by now you can differentiate between Numeric and Alpha-Numeric constants. In all programming languages there are few more types of constants which sometime are essential for a particular case. If we have learn programming properly we must know each of them correctly and understand where we must use them. Because these post endevour to teach you programming easily therefore I have left those constants for later discussion.

Just as an exercise try to find out which of these constants are valid and what is their type.

"34.56/12"
12.56"wer"
67
"Sudeep + 56"

Thursday, 9 April 2009

Learn Programming Easily Step 3

I must apologize to the people following my site for not publishing an update for the past 2-3 days. I was a bit busy messing about with RSS and was sucked into the use of this format. Any way lets move on with what we were doing and that is learning the basics of programming.
As I showed you in my earlier post let me give you another example of a language neutral pseudocode

START
DISPLAY "2 + 2"
STOP

As you may have guessed this program will display 4 on the computer screen. WELL DONE..!

You are absolutely and utterly wrong. Why 2+2 is 4...? Yes you are correct in most circumstances yet this is the world of programming.
In this case I have used double quotes " this directs the computer to print whatever written inside the quotes as it is without using any intelligence or any logic. You get what you see no computation no meaning no logic nothing. Therefore the computer forgets about mathematics and prints whatever written inside the double quotes as it is. Again to re-emphasize my point. Let me ask you another question if I write

DISPLAY "Sandeep+4" what should it print. Forget about the meaning, don't think anything about the meaning(I accept the phrase is quite nonsensical) So if you were a ROBOT and just printing it as it is then it will print Sandeep+4 on the screen. For now please remember this do not forget it

DOUBLE QUOTE MEANS AS IT IS. IT DOES NOT HAVE ANY MEANING,NO MATHS,NO LOGIC,NO ANSWER, NOTHING. SHOWS ON THE SCREEN AS IT IS

With this wonderful weapon lets boldly go where no man has ever gone.... oops the Star Trek fan is getting a bit excited.
So what is this thing called. Is there a name for such things which are printed as it is and are written in double quotes?. There are plenty of names different people use to denote this kind of data. My favourite is 'Alpha-Numeric' constant. Again it is very important to understand the name. Lets take the word 'Constant' what does it mean? Again it means exactly what it sounds like.
A Constant is something which does not change irrespective of what you do with it. Best example in this category is your name. Lets say my name is Bruce does the meaning of Bruce changes every day or can you change the meaning of Bruce anytime you want. Can I say Bruce means Eddie or Bruce means 5. That is jibberish. You can noot change the value of a constant irrespective of what you do.

Now lets take the first part 'AlphaNumeric' it means the constant may have alphabets as well as numerics. Because we are not concerned with the value of a constant or what it means. To write your name on the screen you shall write DISPLAY "". To write your address DISPLAY "12 chester road london N2 3P". DISPLAY "MY SKYPE ID IS DER£$". This is sometimes also called String but lets keep thing simple for now. Anything enclosed in a double quote is a String/Alpha Numeric Constant

"23+43" prints------------------------->23+43
"Sudeep"prints----------------------->Sudeep
"*3+(78/34)+3" prints--------------->3+(78/34)+3
Until my next post keep repeating anything inside a double quote has no meaning for the computer. It will print it as it is. In my next post I will handle other data types or other constants.

Monday, 6 April 2009

Learn Programming Easily Step 2

If you have understood what I have tried to convey in my previous post then it is time to move forward. As I mentioned that programming is
A set of logical instructions written to perform a specific task or tasks.
In my previous example I took a mundane task of clearing the bins now lets assume these tasks are to be performed by a computer therefore I shall again over simplify the situation and take a simple task of printing some message on the screen or the output device. Lets say the message is "Happy New Year". If you believe the sci-fi movies and think just saying "Computer show me Happy New Year on my screen" and your computer will do so... then all I can say dream on. Most computers cannot do so and even the ones which may be able to understand speech are very limited in their ability. Have you ever tried to dictate on your computer using the popular speech programs?. Then there is a small problem...computer as a machine does not understand English for that matter any human language. It can understand the language of 0's and 1's called binary thus making it understand your language is a bit of a problem. So how do we go about


My wonderful diagram shows you the task which has to be performed. 'INPUT' means the command or the instructions that you gave to the computer.As I said computer cannot understand the language you speak. So how do we convert it into something which it can understand. There in lies the magic of compilers or interpreters.




I Shall not go into the gory details of compilers just as a simple explanation compilers are a piece of code or programs which take instructions in human readable format and convert them in a format which computer can understand. There are lot of steps involved but that is for some other time. As you know English or any language has a grammar or a way of witting and reading in the same way programs are written following a specific grammar which is quite rigid compared to normal human languages. This grammar is called syntax in technical terms. Therefore if you want to become a crack programmer you must learn the language first. There are many languages each with its own strong points and weaknesses. Some of the well known ones are C++/C, Visual Basic, JAVA, C# etc. But take my word for it the number of languages are innumerable.

Getting back to our discussion as how to print "Happy New Year" on the screen. Now I can say

START
DISPLAY "Happy New Year".
STOP

There we have it our own program to print a message on the screen.(May be I can sell it for a few thousand pounds to IBM).

Your next question might be which language have I used? The answer is none this a style of writing program which are very close to the real thing yet they are not bound by any language. This style sometimes is called 'pseudocode'. For most part of the introduction I shall follow this pattern. Once the basics are out of the way I will take up some language to further illustrate my points.