Friday, 29 October 2010
Iteration or Java Loop
Monday, 14 December 2009
Java programs without BlueJ
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
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
+678
| 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
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 "
"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
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.
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.


