Saturday, October 10, 2009

An interesting java program that caught my attention is the one that follows!

import java.awt.BorderLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;

public class ScrollSample {
public static void main(String args[]) {
String title = "JScrollPane Sample";
JFrame frame = new JFrame(title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Icon icon = new ImageIcon("dhoni.jpg");
JLabel label1 = new JLabel(icon);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(
label1);
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 200);
frame.setVisible(true);
}
}

Following is a program that was given to my juniors in the C-Debugging finals. It was quite surprising that no one solved it, in an efficient manner!

Only Vivek and Sathish from third year did some justice to the program!

#include<stdio.h>
void main()
{
int x;
char c,h;
clrscr();
printf("\n Enter a character:");
scanf("%c", &c);
printf(" The first character entered was:%c",c);
printf("\n Enter another character:");
scanf("%c",&h);
printf("\n The second character entered was:%c",h);
getch();
}

Solution:

#include<stdio.h>
void main()
{
int x;
char c,h;
clrscr();
printf("\n Enter a character:");
scanf("%c", &c);
fflush(stdin);
printf(" The first character entered was:%c",c);
printf("\n Enter another character:");
scanf("%c",&h);
printf("\n The second character entered was:%c",h);
getch();
}

Explanation:

stdin, gets the character and the enter as another character and it is set to h.
So h is not input at all!

fflush is an interesting function which flushes the unwanted content from the stream!

In java too we face this problem, I will get back to that in my tutorial!

Saturday, October 3, 2009

Hope you meditated enough.

It's been quite some time since my last post.

I had been really busy organizing the Intra Virtuoso events for my college juniors. Anyway getting back to the post, we shall continue from where we left...

No preprocessor directives in Java

Which means that, Java uses something else to makeup!

It uses a technique which we would be calling as...

Importing of Classes

Consider the following code in C

# include <stdio.h>
void main()
{
printf("Hi");
}

It actually has only four lines of code.

But at compile time it shows that there are 312 lines of code!

The remaining 308 lines of code is from the stdio.h header file. Its added into the program.

In C, its just the standard library functions from the header files.
Java has a different hierarchy for programming!

Java Library------->Packages------->Classes & Interfaces------->Methods

So, if a programmer wants to use a class then only that class imported.

Difference between #include and import

#include directive makes the compiler to go to the c/c++ standard library and then copy from the header files into the program.

import statement makes the JVM go to the java standard library, execute the code there and then substitute the result in the program.

Here no code is copied and hence there is no waste of memory or the process time.

Eg:

import java.lang.system;
import java.lang.string;

Note:

java.lang.*;

is automatically imported to all java programs!

So the code import java.lang.*;
is never needed!

Now the comparison between C and Java is over!
We shall try to get along with Java basics from now on.

Try to recollect the Hello world program in Java.
I would try to get back to you with my next post.

Monday, September 21, 2009

A nice Story



Once upon a time there was a shepherd looking after his sheep on the side of a deserted road. Suddenly a brand new Porsche screeches to a halt.

The driver, a man dressed in an Armani suit, Cerutti shoes, Ray-Ban sunglasses, TAG-Heuer wrist-watch, and a Pierre Cardin tie, gets out and asks the Shepherd: If I can tell you how many sheep you have, will you give me one of them?"

The shepherd looks at the young man, and then looks at the large flock of
grazing sheep and replies: "Okay."

The young man parks the car, connects his laptop to the mobile-fax, enters a NASA Webster, scans the ground using his GPS, opens a database and 60 Excel tables filled with logarithms and pivot tables, then prints out a 150 page report on his high-tech mini-printer.

He turns to the shepherd and says, "You have exactly 1,586 sheep here."

The shepherd cheers,"that's correct, you can have your sheep."

The young man makes his pick and puts it in the back of his Porsche.

The shepherd looks at him and asks: "If I guess your profession, will you return my animal to me?"

The young man answers, "Yes, why not".

The shepherd says, "You are an IT consultant ".

"How did you know?" asks the young man.

"Very simple," answers the shepherd. "First, you came here without being called. Second, you charged me a fee to tell me something I already knew, and third, you don't understand anything about my business...
Now can I have my DOG back?"

I got this story as a forwarded mail, few days back. It was like damaging myself because I aspire to become an IT consultant! Anyway, its how the customer always thinks of us! So it was pretty cool to read. Hope you like it too!

Sunday, September 13, 2009

1. Coca-Cola was originally green.

2. The most common name in the world is Mohammed.

3.The name of all the continents end with the same letter that they start with.

4. The strongest muscle in the body is the tongue.

5. There are two credit cards for every person in the United States.

6. TYPEWRITER is the longest word that can be made using the letters only on one row of the keyboard.

7. Women blink nearly twice as much as men!

8. You can't kill yourself by holding your breath.

9. It is impossible to lick your elbow.

10. People say "Bless you" when you sneeze because when you sneeze,your heart stops for a millisecond.

11. It is physically impossible for pigs to look up into the sky.

12. The "sixth sick sheik's sixth sheep's sick" is said to be the toughest tongue twister in the English language.

13. If you sneeze too hard, you can fracture a rib. If you try to suppress a sneeze, you can rupture a blood vessel in your head or neck and die.

14. Each king in a deck of playing cards represents great king from history.
Spades - King David
Clubs - Alexander the Great,
Hearts - Charlemagne
Diamonds - Julius Caesar.

15. 111,111,111 x 111,111,111 = 12,345,678,987,654,321

16. If a statue of a person in the park on a horse has both front legs in the air, the person died in battle.

If the horse has one front leg in the air,the person died as a result of wounds received in battle.

If the horse has a all four legs on the ground, the person died of natural causes.

17. What do bullet proof vests, fire escapes, windshield wipers and laser printers all have in common?

Ans. - All invented by women.

18. Question - This is the only food that doesn't spoil. What is this?
Ans. - Honey

19. A crocodile cannot stick its tongue out.

20. A snail can sleep for three years.

21.. All polar bears are left handed.

22. American Airlines saved $40,000 in 1987 by eliminating one olive from each salad served in first-class.

23. Butterflies taste with their feet.

24. Elephants are the only animals that can't jump.

25. In the last 4000 years, no new animals have been domesticated.

26. On average, people fear spiders more than they do death.

27. Shakespeare invented the word 'assassination' and 'bump'.

28. Stewardesses is the longest word typed with only the left hand.

29. The ant always falls over on its right side when intoxicated..

30. The electric chair was invented by a dentist.

31. The human heart creates enough pressure when it pumps out to the body to squirt blood 30 feet.

32. Rats multiply so quickly that in 18 months, two rats could have over million descendants.

33. Wearing headphones for just an hour will increase the bacteria in your ear by 700 times.

34. The cigarette lighter was invented before the match.

35. Most lipstick contains fish scales.

36. Like fingerprints, everyone's tongue print is different

Tuesday, September 8, 2009

Recently, I had to send a mail to all my friends.
So I was searching for a method to do it.
That's when I found this pretty useful video in youtube!

Monday, September 7, 2009

Today, I was speaking with my Internet Programming lecturer, Mr. Gupta. He was sharing his experience on teaching C to the first year mechanical class. He said, he couldn't explain the logic of adding sum of n digits to a student. He employed the usual strategy of leaving out the nines in the number. (The student didn't know that simple logic it seems!)

That's when this idea struck me. If you have a number, say x.
It shall be represented as (9*n)+y.

If we identify y then that's the sum of digits.

Say the number is 103

It's simple 99+4, (9*11)+4 hence 4.

y is nothing but (9+9+9+9....11 times) +4
So we skip all those nines and take the 4 alone.

In simpler words if x is the number then (x modulus 9) is the sum of the digits.
So in a programmer's perspective its just x%9.

Sunday, September 6, 2009

What Java does not have?

No functions in Java (Only Methods)
Function vs. Method
1. C++ functions could be present inside and outside the class.
2. But java methods could be written inside classes only.

No pointers in Java
1. Pointers lead to confusion.
2. Pointers crash program easily. (Addition of 2 pointers, Runtime memory leakage-dangling pointers)
3. Pointers break security.

No operator overloading in Java
1. Operators are immutable in java.
2. Operator’s meaning cannot be changed in java.

No preprocessor directives in Java

No multiple inheritance in Java

No static memory in Java

No destructors in Java

No goto statement in Java

Well, there seems to be more features available in C and C++ than in java. Why learn Java then? Doesn’t your brain ask this question? My brain did ask.
And I began searching for the answer.
First we have to compare C and Java to understand how the languages work.

Here is a small comparison between C and Java
C vs. Java





C





Java

So Java is powerful mainly because, of the byte code. It could be interpreted by the Java Virtual Machine. So based on the system, different JVMs are used. But the class file remains the same unlike C's exe file. It’s quite an interesting fact that JVM was written using C.

There is a big table with all the differences between C and Java mentioned in it. I would suggest you to skip it if you feel it to be too big.

Click here to view that table.

C uses a compiler or an interpreter (only one at a time).
Java uses both compiler and interpreter together.

In addition to this it uses a pretty interesting JIT. Now what does this JIT do?
Wikipedia gives the following definition:

In computing, just-in-time compilation (JIT), also known as dynamic translation, is a technique for improving the runtime performance of a computer program. JIT builds upon two earlier ideas in run-time environments: byte code compilation and dynamic compilation. It converts code at runtime prior to executing it natively, for example byte code into native machine code. The performance improvement over interpreters originates from caching the results of translating blocks of code, and not simply reevaluating each line or operand each time it is met. It also has advantages over statically compiling the code at development time, as it can recompile the code if this is found to be advantageous, and may be able to enforce security guarantees. Thus JIT can combine some of the advantages of interpretation and static (ahead-of-time) compilation.

The paragraph does not make complete sense to you unless you already know about JIT ;-).

Now let me try to put it simple.

Consider the following code:

for(i=1;i<=10;i++)
print i

It is a program to print from one to ten.

Normally, a compiler will convert the print i code ten times (say it takes ten
nanoseconds).

JIT Compiler will load print i at a place in the memory with an increment in it
and call it ten times (it takes just 2 nanoseconds).

Convert print I --------> 1 nanosecond.
Load it in memory -----> 1 nanosecond.

Coming back to our comparison, C vs. Java, I think I have told you enough for today. And as for C++ vs. Java, that’s covered in what Java does not have itself.

I have told you what Java does not have?
Why Java does not have those features is the question you have to think upon. Keep meditating till the next post.



I had been spending my last fortnight with Java. As I was studying, I understood its power and the flexibility it gives to a programmer. Though I have learnt Java in bits and pieces, I have not learnt it from the basic (ie., from the origin, history, architecture,..). In my college, we students have not been attending the classes for the past one week due to the fear of getting infected from swine flu in the campus! This has been like a blessing in disguise, I was able to peacefully explore Java. As I was exploring Java, I got an idea of making a small tutorial for people like me who have learnt Java in bits and pieces.

I will be posting my tutorial here in my blog once in a while! It is a tedious process so there is no fixed timeline on my tutorial posts. Continue your support.