Showing posts with label fast. Show all posts
Showing posts with label fast. Show all posts

Sunday, May 8, 2011

8. Flow of Control

This is the part where you learn to design the 'Artificial Intelligence' of the program. ALL the logic of the program depends on how the flow of control is maintained by it.

Let us first see the logical operators available to us in Java:
  1. == : This is the 'equals' operator. You can use this to check if two numbers or characters are equal.
  2. >= : This is the 'greater-than/equals' operator.
  3. <= : This is the 'less-than/equals' operator.
  4. < : This is the 'less than' operator.
  5. > : This is the 'greater than' operator.
  6. ! : This is the 'not' operator. It is used in conjunction with the other operators. eg: '!=' stands for 'does not equal'.
Now let us take a scenario:
Let us make a grade-calculator. It will take the marks(out of ten) as input, and print the
following results:
  1. 9 or 10: A+
  2. 7 or 8: A
  3. 6: B
  4. 5: C
  5. 4: D
  6. <4: F
This is how you can do it:
__________________________________________________________________

Now that's pretty simple to understand, isn't it? Leave me a comment if there is absolutely ANYTHING that you would like to ask.

Saturday, May 7, 2011

7. More About Methods

Let us first understand what 'methods' are:
Def: 'Methods' are pieces of code that take some input, and may return an output.

Remember how in the Hello World program in chapter-3 we declared the 'main' method?

Let us break it down. The general syntax to declare a method is as follows:

access-modifier static return-type name(input)

1. In the previous chapter, we have already seen what access-modifiers are for.
2. 'static' is something that we will discuss later on.
3. return-type: This is the datatype of the output that the function returns.
4. input: what? you don't know what input means?

So now let us see an example:
This is a simple method that takes two integers as input, and displays the sum as output.

To run this piece of code, right-click on the compiled class, and click on 'void sum(...)'.

Now, this method does DISPLAY an output, but it does not 'return' it. This is why it's return-type has been given as 'void'.
In order to RETURN an output from this method, replace the print statement with the following code:
return c;
Now can you figure out what the return-type of the method would be?
That's right. It would be 'int', as the variable that we're returning(c) is of the datatype 'int'.
So change the return-type of the method to 'int', and try running it again.

6. Access Modifiers

One of the basic features of an Object Oriented Language is that properties of one class can be used by another.
This gives rise to a problem: Data Theft.
Because the Data and Methods of one program can be accessed by another, there are chances that others might steal your data, or your code.

To overcome this problem, we have been provided with what are called 'Access Modifiers'. Using Access Modifiers, you can specify what parts of your programs can be accessed by other programs.

The 3 most-used modifiers are 'public', 'private', and no-modifier.
1. 'public' members(data or methods) can be accessed from ANYWHERE by ANYONE.
2. 'private' members can be accessed ONLY by other members of the SAME class.
3. no modifier: If you do not specify a modifier, it is taken to be 'friendly' by default, ie, it can be accessed by all classes in the same package.

See this link to get a complete understanding.

5. Variable... or Not

As we saw in the previous chapter, Java stores all the data in different 'datatypes'. Now let's see how this is done.

Any kind of data can be stored within a program by using a 'variable' of the appropriate type, just like in Algebra.
For example, if you have 3 coconuts, you can use the following variable declaration:
int coconuts = 3;
That line should be self-explanatory, I think. We have declared a variable called 'coconuts' that holds the value 3. The same statement could be broken down into two lines in the following manner:
int coconuts;
coconuts = 3;
Let us do something similar with the other datatypes:
double money = 3.5;
char temporary = 'A';
boolean happy = true;
String str = "Hello!";
...and so on.

Now, all the above declarations are VARIABLE declarations, i.e., their values can VARY.
For example, if I eat one of the coconuts, i would now write:
coconuts = 2;
The value stored in 'coconuts' has now been OVERWRITTEN, and is now 2.

Java also supports CONSTANTS. Just like variables, constants can store values, but unlike them, these values CANNOT be changed. Example:
final int cookies = 3;
See the difference? The declaration is the same as that of a variable, except for the keyword 'final' having been added before it.
Now what if i try to change it's value?
cookies = 2;
This would give an error during compilation, and won't work.
You can use this property to store mathematical constants, such as:
final double pi = 3.1415;

This is all there is to this chapter. You can post any queries in the 'comments' section below...

4. Datatypes

As we've discussed before, every program is made up of two parts:
1. Data
2. Methods
In this chapter, we'll see how data is stored in a Java program.

Data may be of various 'types'. It may be numerical(integral or real), textual(single character or string of characters), boolean(true or false), etc.

The following table shows the various kinds of primitive datatypes available in Java:
(Taken from HERE)
Along with these primitive datatypes, there is one more datatype that is commonly used in Java:

The 'String' datatype. Unlike the 'char' datatype that can store only a single character(alphanumeric values and special symbols), the String datatype can store a string of characters, such as a sentence.

Friday, May 6, 2011

3. Hello, world!



It's an old programming tradition to start learning a new language with a simple program that prints out 'Hello, world!' to the screen. So let us keep the tradition alive, and get down to the dirty work!
_____________________________________________________________________
NOTE: Before we start, I would like to make one thing clear: Java is a hard-core Object Oriented Programming language, and may seem a little daunting at first. But trust me, once you get the hang of it, it is VERY simple to do stuff.
_____________________________________________________________________


The Hello World Code:
[In BlueJ, click on 'New Class', and name it 'hello'. Double click on this new class, and replace all the bullsh*t with the code written below.]



Follow these steps to run your program:
1. Click on 'Compile'.
2. Close the class, and right-click on it.
3. Click on void main(String args[])

Breaking the Code- Line by Line:

Line 1- import java.io.*;
This line 'imports' properties of all the 'classes' that have been specified. Here, all the classes in the folder Java\io have been imported. '*' stands for 'ALL'.

Line 2- class hello
In Java, every program is stored within a 'class'. A class thus contains two kinds of assets:
1. Data
2. Methods/Functions (to work on the data)
IMPORTANT: The name of the file in which the program is saved should be the same as the name of the class.

Line 4- public static void main(String args[])
This is called a 'method declaration'. As we have discussed above, every program has two components: data, and methods. Here, a method called 'main' has been created.
As every program is split into a number of methods, there should be a specific method from where the program should start running. This is what the 'main' method is for. A program without a 'main' method will NOT work.
We'll come back to this line later.

Line 6- System.out.print("Hello, world!");
This line is self explanatory, i think.
It calls the 'print()' method located in the class: System\out.
The 'print()' method, in turn, prints out whatever you've written inside it to the screen.
____________________________________________________________________

Well, that was certainly a good start! We'll continue in the next post. Feel free to ask any questions you want to!

Thursday, May 5, 2011

2. Setting it Up

Before we start learning how to code in Java, let us set up a complete environment dedicated to that purpose. This is what you should do:

1. Download and install BlueJ from here. BlueJ is a simple and efficient Integrated Development Environment(IDE) for Java.

2. Once you have finished the installation process, run BlueJ, and and create a new Project. A project is nothing but a folder in which all your files will be saved. By default, a new Project will be created in your 'My Documents' folder(if you're a Windows user, that is).

3. That's it. We're done. You can now proceed to the next post- The 'Hello World' program.

1. Introduction to Java


Who made it?
Java was developed by James Gosling for Sun Microsystems(now Oracle).


A BIG problem...
Before getting down to the dirty work, let us understand how 'programming' works-
Step 1: Write a program in a certain 'language'.
Step 2: 'COMPILE' it. During compilation, your program is converted into machine code(Zeros and Ones), as that is the only language that your computer understands.
Step 3: The compilation produces an executable file, for example, an 'exe' file, that you can run.

There is an exception to the second step. Some languages(called 'SCRIPTING' languages) like python, javascript, etc. do not need to be compiled. They can be run directly by the system. The problem with scripting languages is that your code is not safe, and the programs run slower.

Now, just like human cultures, different operating-systems have different languages as well. The machine language for a Mac is not the same as that for a Windows PC. So when you compile a program on a computer, that program can be understood and run by computers with the same hardware-software configuration(called 'PLATFORM') as yours. As a result, Mac-compiled software will run ONLY on another Mac. Same goes for Windows. This is the BIG problem we're talking about.


The (Final) Solution
No, I'm not racist. The heading is a reference to the song by Pere Ubu.
The objective of Java was to solve the problem we just talked about, and to develop a truly' PLATFORM-INDEPENDENT' language... and it did. But how?? Let's see-
Java is divided into two parts:
1. The Java 'language', and (drumrolls, please!)
2. The Java 'PLATFORM'.
Yes, you read that right. Java is, in itself, a seperate platform!

This platform is officially called the 'Java Runtime Environment'(JRE), the main component of which is the 'Java Virtual Machine'(JVM). As such, a computer MUST have a JRE installed in order to run software written in Java, which is not a big deal, as it is mostly open-source, and can be downloaded from the net free of cost. Most computers come pre-installed with Java, anyhow, so that is not a problem.

So, the thing is, when you compile a Java program, it gets converted NOT to the machine language of your platform, but to the 'machine' language of the Java Platform(the JVM). This is called Java Bytecode. Now, this software can run on ANY machine in the world that has Java installed, thus overcoming the handicap COMPLETELY!


Problems and Critisizms
Java has, since it's inception, been criticized for it's slow performance... and to be true, it always has been a little slower than other languages(like C/C++). But that is something that is changing RAPIDLY.


White Light
I.T. companies have constantly been trying to come up with better and faster versions of JVMs. Java 6 has a new class-loading feature that is MUCH faster than the previous versions.
Heard of the game called Quake 2? Obviously, you have! Right? Kill yourself if you haven't. Okay. if you're still alive, here are some fun facts:
=>The original engine for quake 2 had been written in C/C++. In 2006, a group of programming enthusiasts rewrote the game engine using Java.... and guess what? It actually gives BETTER GRAPHICS AND PERFORMANCE than the original!! Woot! The engine is called Jake 2. Here are some screenshots.
=>Quake 2 is an old game, and hence, the sad graphics. Now let's see what it can today, in the time of advanced pixel shaders and ragdoll physics. JMonkeyEngine is a game-engine under development. It is still in it's alpha phase, and is completely Free and Open Source!! Heck, it is PLATFORM-INDEPENDENT!!! Check the video out!
Java is now being considered as a serious language for desktop programming, although it has always been used for other stuff:
Blu-ray discs, and many Set-top-box systems use Java technology. Heard of Android? Even Android is written in Java. In fact, most of the handheld devices like cell-phones use Java.


Conclusion
That's all for today, folks! In the next post, we'll start-off with the actual sh*t. The addictive junk that geeks like you and me call.....CODING.
See ya.

PREVIOUS|NEXT