Showing posts with label world. Show all posts
Showing posts with label world. 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.