01 March 2014

Java - Introduction

Good day to every reader,

Today I am going to introduce you to a new programming language namely "Java". It was first developed by Sun Microsystems, which was acquired by Oracle in 2010. Java is vastly used all around the world by most programmers, especially by those who seek portability and security in programming.

JAVA AND OOP

Java is an OOP (Object Oriented Programming) language, meaning it concerns more on objects and their attributes as well their methods than on the procedure of how the program runs — like in the C Programming Language which is also known as the procedural programming language.

JAVA vs. C

Despite the differences between the two languages, knowledge in C Programming is preferably required because the syntax of a Java code is pretty much similar to that of a C code. Some new keywords I will explain today are: Class, Object, Attribute, and Method.


DEFINITIONS

Classes and Objects

I will explain these two terms with an illustration:
Let's say you have a Printer. We all know that there are many manufacturers of printers. There are Canon printers, HP printers, Xerox printers, and so on.
When you talk about a Printer, you are actually talking about printers in general, which is abstract.
But when you talk about a Canon Laserjet Printer, you talk about a real thing, something that exists.

In this case, Printer is a Class and a Canon printer is an Object created based on the Printer Class.

 To sum up, a Class is a blueprint of an Object, and an Object is a physical representation of a Class.

Attributes

An Object (or a Class, which is actually the same, because an Object is created based on a Class) has certain attributes.

Examples:
A box has width, height, depth, surface area, and volume.
A phone has price, manufacturer, type, dimensions, weight, etc.
A car has maximum speed, price, manufacturer, type, color, number of doors, number of tires, etc.

Basically, Attributes are what make up an Object (or a Class). You can think of them as the ingredients of a recipe — The recipe itself is a Class, while the food cooked based on it is an Object.

Note:
The Attributes of a Class (or an Object) must be declared as private, so it cannot be accessed other than through its Methods. This will be explained in the next post.

 

Methods

Methods are (or should be) the only way you access the Attribute of a Class (or an Object).
If not, you can imagine if someone has access to your program, he can easily set the attributes' values to those he wants.

Let's assume that we have an Object, an ATM.
We all know we can withdraw money using the ATM, provided that there is enough money in our account.
There will be Methods, supposedly withdrawMoney(), setUser(), and getBalance().

First, an ATM user must insert the card, containing their user info into the ATM.
As it is inserted, the ATM calls its Method, setUser(), to set the current user to the card holder.

Next, it will call the next Method which is to getBalance() of the account number which is already provided by the card (obtained using the setUser() Method).

Finally, the user will try to withdraw the money by inputting the amount of money he wants to withdraw. The withdrawMoney() Method will then be called to check whether the amount requested is available in the account. If such amount (or more) is available, the ATM will withdraw the requested amount of money, end the transaction, and eject the card.

As you can see above, there is no way for the user to set the balance of the account because there is no such Method provided.


As this is an introductory post, I will stop right here and continue to expose more about Java and the OOP concept in the next posts.


Happy Coding,
Cyber Frost

20 November 2013

C Functions

Today, I am going to discuss about "Functions".

Functions are a group of statements that is created to perform a special task and returns a value as its result.
Example: The main() function.


From the above definition, you can get:

-Function is a group of statements.

-Function performs special tasks.
 Example: The factorial function is used to calculate the factorial of a given number.

-Function returns a value.
 Please note this: Functions return one value only no matter how many arguments it has as its input(s).


So, how do we create a function?

Use this syntax:
return_type function_name (data_type var1, data_type var2, ....){
 //statement1;
 //statement2;
 //...
return data
}

Let's go straight to creating our own function.
I would like to show you how to create a factorial function by using iteration rather than recursion.

First of all, I need to explain what factorial is and how it works.

n factorial, denoted by n!, is the factorial of n which equals to n * (n-1) * (n-2) * ... * 3 * 2 * 1.

So, 5! = 5*4*3*2*1 = 120 and 7! = 7*6*5*4*3*2*1 = 5040.

If you analyze it thoroughly, you will see that 5! can also be 1*2*3*4*5 and it still equals to 120. The same thing works for 7! too.

Therefore, we can conclude that:
n! = 1 * 2 * 3 *.... * (n-2) * (n-1) * n
We have learned about the For Loop before, now we are going to put it into use.


How are we going to calculate the factorial of n?

It's simple, all we have to do is loop from 1 to n, multiply each values respectively, store it in a variable, then return the result.


The code would be like this:
int fact(int x){
 int i=1;
 int result=1;
 for(i=1; i<=x; i++){
   result = result * i;
 }
 return result;
}

Explanation:
Integer x is the input, Integer i is the loop counter, Integer result is the value that will be returned as the result of the operation performed.

Let me simulate the flow of this program.

Let the input (x) be 5. The program will loop for 5 times.

Loop 1 :
i = 1, result = 1*1 = 1

Loop 2 :
i = 2, result = 1*2 = 2

Loop 3 :
i = 3, result = 2*3 = 6

Loop 4 :
i = 4, result = 6*4 = 24

Loop 5 :
i  = 5, result = 24 * 5 = 120 (answer)


Assuming you want to display your result, you would want to create your main function. The whole program will look like this :

#include <stdio.h>
#include <stdlib.h>


int fact(int x){
 int i = 1;
 int result = 1;
 for(i = 1; i <= x; i++){
   result = result * i; 
 }
 return result;
}


int main(){
  printf("%d\n",fact(5)); 
  system("pause"); 
  return 0; 
}

Description of the code:
The main program prints the factorial of 5. If you want it to print the factorial of different number, you can do so by changing the 5 to another number.

Please note that he result variable is an Integer and an Integer has its limit. It will not be able to calculate the factorial of large numbers. If you wish to calculate the factorial of large numbers, you might want to change the return_type of the function.

Anyway, here's a preview of the program we have just created:





Happy Coding,
Cyber Frost

16 November 2013

Binary Conversions

Hello Coders.

Today I would like to discuss about binary digits.
If you don't know what a binary digit is, here is a simple explanation:

A Binary Digit is a digit of number whose value is either 0 or 1.

In computer, the 0s and 1s indicate electrical signals.
1 indicates ON, 0 indicates OFF.

As we have learned in the previous post about data types,  a character has a size of 1 byte.
1 byte equals to 8 bits.

Basically a set of binary digits make up 1 byte, or 8 bits.

Examples: 1111 1111, 0000 1010, 1010 1110, etc.

What can we do with those 1s and 0s?

You can convert them to decimals, octals, and hexadecimals.
Also, you can use the AND, OR, and XOR operators to perform special conversions.

Converting Binary to Decimal 

Each digit of binary represents a decimal value which is 2n, where 0 <= n < 8 .

The leftmost digit in an 8-bit set of binary digits has the value of 27 while the rightmost has the value of 20. The digits in between has the value from 26 to 21 in a left-to-right order.

Let's go right to the examples:
  • 11111111 = 1*2^7 + 1*2^6 + 1*2^5 + 1*2^4 + 1*2^3 + 1*2^2 + 1*2^1 + 1*2^0 = 255
  • 00000001 = 0*2^7 + 0*2^6 + 0*2^5 + 0*2^4 + 0*2^3 + 0*2^2 + 0*2^1 + 1*2^0 = 1
  • 01010101 = 0*2^7 + 1*2^6 + 0*2^5 + 1*2^4 + 0*2^3 + 1*2^2 + 0*2^1 + 1*2^0 = 85
  • 00110010 = 0*2^7 + 0*2^6 + 1*2^5 +1*2^4 + 0*2^3 + 0*2^2 + 1*2^1 + 0*2^0 = 50

So let's say you have sets of 3-bit binary digits:
  • 111 = 1*2^2 + 1*2^1 + 1*2^0 = 7
  • 010 = 0*2^2 + 1*2^1 + 0*2^0 = 2
  • 000 = 0*2^2 + 0*2^1 + 0*2^0 = 0
  • 100 = 1*2^2 + 0*2^1 + 0*2^0 = 4 

 Or sets of 4-bit binary digits:
  • 0111 = 0*2^3 + 1*2^2 + 1*2^1 + 1*2^0 = 7
  • 1010 = 1*2^3 + 0*2^2 + 1*2^1 + 0*2^0 = 10
  • 0100 = 0*2^3 + 1*2^2 + 0*2^1 + 0*2^0 = 4
  • 1001 = 1*2^3 + 0*2^2 + 0*2^1 + 1*2^0 = 9
 There you go. You are now capable of converting binary digits to its decimal value.


Converting Binary to Octal

In converting binary(base-2) to octal(base-8), you need to understand that octals are numbers ranging from 0-7 only. This means that 777 exists while 008 doesn't.

To convert an 8-bit binary to its octal value, you need to separate it into 3-bits at a time, starting from the right. Like this:
1111 1111 =  11 | 111 | 111
Three bits at a time? Why is there a 2-bit part then? Simple add 0 in front to make it 3-bits. Notice that 011 == 11 in binary. So, here is the digits after separation:
011 | 111 | 111
Then what?

Calculate each part, convert them to its decimal value.

011 = 2+1 = 3
111 = 4+2+1 = 7
111 = 4+2 +1 = 7

So, 11111111(2) = 377(8)

Another example:
1010 1010 = 010 | 101 | 010 = 252(8)

Converting Binary to Hexadecimal

Hexadecimal numbers range from 0 to F: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F.
In order to convert to Hexadecimals(base-16), you will need to separate the bits four at a time.

Example:
1111 1111(2) = ..... (16)

= 1111 | 1111 
= 1*2^3 + 1*2^2 + 1*2^1 + 1*2^0 | 1*2^3 + 1*2^2 + 1*2^1 + 1*2^0
= 15 | 15
= FF(16)

1001 1100(2) = ..... (16)

= 1001 | 1100
= 1*2^3 + 0*2^2 + 0*2^1 + 1*2^0 | 1*2^3 + 1*2^2 + 0*2^1 + 0*2^0
= 9 | 12
= 9C(16)

Using the AND(&&) Operator

Here is what the AND operator does when two values are passed to it.

p   q   p AND q
   1       1         1
1   0         0
0   1         0
0   0         0

Let's assume we have two sets of binary digits: 11110101 and 00110101. Using the AND operator, we will receive the result:

11110101
00110101 AND
00110101

Another example with 00001110 and 11101100.

00001110
11101100 AND
00001100


Using the OR(||) Operator

The table of OR looks like this:

p   q   p AND q
   1       1         1
1   0         1
0   1         1
0   0         0

So, if your perform an OR on 11110000 and 00001111, you will get:

11110000
00001111 OR
11111111
It is similar to the AND operator, only with different rules.

Using the XOR(^) Operator

The XOR Operator is also called the Exclusive OR. Its table has values like this:

p   q   p AND q
   1       1         0
1   0         1
0   1         1
0   0         0

It can be used to swap digits. Let me show you.


Let A be 00110011 and B be 01011100.
We would like to swap the value of A and B.
We can use the XOR Operator to accomplish the task, like this:

A = A ^ B
B = A ^ B
A = A ^ B

The formula can also be simplified as A^=B^=A^=B

The process looks like this:
A = 00110011
B = 01011100 XOR
A = 01101111

A = 01101111
B = 01011100 XOR
B = 00110011 ------------------- B is now A

A = 01101111
B = 00110011 XOR
A = 01011100 ------------------- A is now B
These concepts can be applied to your programming. For example you can create a swap function using the XOR operator.


Good luck trying, pals.



Regards,
Cyber Frost

Search