Posts

OOP Principles: Introduction

OOP…!, When this topic comes while learning any of the programming language which is object oriented, some time may or may not, but we feel little pressure on mind & think about ‘Can I work with oop?’ or ‘Is the oop easy or difficult to understand?’ like so….
And my answer is OOP (Object-Oriented Programing) is really great think in programming to learn and also easy to understand.
When I was started learn programming, I was created my first program in Lingo. Yes absolutely correct – In Macromedia Director 6. And that time I was completely unknown about programming concepts & OOP principles.
After few months I aware about oops little bit with confused mind. but handling time to time various type of programs using oop terms, I got the good experience.
With the OOP terminology you can design the architecture of your applications or programs. It helps you to design your applications programming structure & flow. You can define the programming patterns as a library using OOP and you can use it many applications. You can make your classes reusable. Classes is one of the main thing in OOP.
If you want to learn OOP principles, learn this principles one by one and step by step with small example of code.
Look at the below,
Package – This thing allows you to organize your classes by separate groups.
Classes – Class is the collections of methods(functions) and you can make any number of instance of that. Changing its property(Variables) or passing variables by calling methods you can take different outputs from it.
Composition – Allows you to make the multiple instances of same classes. If class ‘Bike’ has a class ‘Wheel’, then relation between this classes is composition,
Inheritance – Allows you to extend new class from an existing class with all the properties and methods without doing copy paste the entire code. If class ‘Bike’ is a ‘Vehicle’, then relation between this classes is inheritance.
Polymorphism – Using the same method name in different classes and defining multiple states with single property, can allows to apply based on type or specification. You don’t need to change code every time you use different objects or components.
Encapsulation – This thing take the data & code safe or in other words we can say hiding & protecting data of an object or class.

Here I’ve discussed OOP principles in short. If you have any comment on this post. Please welcome.

Creating Number Slide Puzzle Game in Five Simple Steps using ActionScript and AIR

This is very simple number sliding puzzle game I created in Adobe Flash Builder. I would like to share with you, how simply we can create it.

Download Source
Before start project, let’s plan steps how we can create it.



Step 1: Creating ActionScript Project

Open Flash Builder. Go to File>New>ActionScript Project.
Enter project name “SlidingPuzzle” and select Desktop option under Appliction Type. Then click on Finish.
Now open the SlidingPuzzle.as and set its width = 300, height = 325, frameRate=24 and backgroundColor=#FFFFFF.
[as3]
[SWF(width=”300″, height=”325″, frameRate=”24″, backgroundColor=”#FFFFFF”)]
[/as3]

Step 2: Creating number slide class

Right click on src>new>ActionScript class and name it “Slide.as” set superclass “flash.display.Sprite”. Then click Finish.
Now here we will make rounded rectangular slide graphics and placeholder for number.
Slide.as
[as3]
package
{
import flash.display.Shape;
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFormat;

public dynamic class Slide extends Sprite
{
public var txt:TextField = new TextField();
public var format:TextFormat = new TextFormat();

public function Slide()
{
format.size = 36;

var sp:Shape = new Shape();
sp.graphics.beginFill(0x11cc11);
sp.graphics.drawRoundRect(5,5,90,90,10,10);
//sp.graphics.lineStyle(2,0×000000);
sp.graphics.endFill();
this.addChild(sp);

txt.x = 20;
txt.y = 20;
txt.setTextFormat(format);
txt.selectable = false;
txt.mouseEnabled = false;
txt.textColor = 0xffffff;
this.addChild(txt);
}
}
}
[/as3]

Step 3: Setting default position of slides

Now we will define the default positions of slides. The positions we will store in array.
While user will play this game, in that time we will check slide default position with this array on each individual slide movement.
[as3]
public var positionArray:Array = new Array(
{Num:1,xCo:0,yCo:0},
{Num:2,xCo:100,yCo:0},
{Num:3,xCo:200,yCo:0},
{Num:4,xCo:0,yCo:100},
{Num:5,xCo:100,yCo:100},
{Num:6,xCo:200,yCo:100},
{Num:7,xCo:0,yCo:200},
{Num:8,xCo:100,yCo:200}
);
[/as3]

Step 4: Implementing logic to shuffle the slides

First Initialize Random Array with default positions/x y co-ordinates. Then shuffle the co-ordinates and place all the slides according to shuffled co-ordinates.

[as3]
protected function setRandomArray():void{
// Initialize random array with default co-ordinate
randomPosArray = new Array( {xCo:0,yCo:0},
{xCo:100,yCo:0},
{xCo:200,yCo:0},
{xCo:0,yCo:100},
{xCo:100,yCo:100},
{xCo:200,yCo:100},
{xCo:0,yCo:200},
{xCo:100,yCo:200},
{xCo:200,yCo:200});
// Shuffle co-ordinates
randomPosArray = shuffle(randomPosArray);
// Place all the slides with shuffled co-ordinates
replaceSlides(randomPosArray);
}

protected function replaceSlides(arr:Array):void{
for(var i:int=0; i 0) {
arr2.push(arr.splice(Math.round(Math.random() * (arr.length – 1)), 1)[0]);
}

return arr2;
}
[/as3]

Step 5: Setting up user interaction and Game over check

[as3]
protected function handleSlideClick(e:MouseEvent):void{
// Find the distance between empty position and user clicked slide
var dx:Number=e.target.x-lastPos.x;
var dy:Number=e.target.y-lastPos.y;
var dist:Number = Math.sqrt(dx*dx+dy*dy);

// If it is 100 the place the slide to empty postion and set co-ordinates as last position
if(dist == 100){
var tempPos:Point = new Point(e.target.x,e.target.y);
e.target.x = lastPos.x;
e.target.y = lastPos.y;
lastPos = tempPos;
}else{
return;
}

// If slide is on it’s defualt position the set it as True
if(e.target.x == e.target.xCo && e.target.y == e.target.yCo){
e.target.pos = true;
}else{
e.target.pos = false;
}

// Check all the slides are place on position
// If all the slides are on place set Game finished.
var bool:Boolean = false;

for(var i:int=0; i 0) {
arr2.push(arr.splice(Math.round(Math.random() * (arr.length – 1)), 1)[0]);
}

return arr2;
}

}
}
[/as3]

How to create Android Contact Book App : Part 2 : Creating SQLite Database

As you seen in previous part, how we have created Interface for Contact Book App. So lets continue with next part.

Now we have the user interface to see Contacts and Input the Contact details. But where to store contact data. There is several ways of storing data. i.e. Text file, SQL database on web and accessing data through web services, SQLite etc.

In this app we are going to use SQLite database. The reason behind using SQLite database is, Android has native support for it, it data will store locally on user device.

Table of content


  1. Defining Table Structure
  2. Creating Contact Class
  3. Creating SQLite Database Handler Class to Handle Database
  4. Test Before Use
  5. Download Source

1. Defining Table Structure

Before we start implement database handler class we have to define table structure of the database. That is Columns of the table and it type as well as key. See the structure below.

Table Structure

2. Creating Contact Class

Create the Contact object class, for this create a new class file and name it Contact. This object will hold all the data of the single Contact.
So let’s create class for Contact and define all the getter and setter methods.

3. Creating SQLite Database Handler Class to Handle Database

Now create the Database Handler class named “Contact Handler” under src folder and extend this class with SQLiteOpenHelper.

We will create Contact table inside the onCreate() method. This method calls after create the database.

If there will be any changes in database structure, the onUpgrade() method calls.

a. Adding contact

To add the contact details in to database, define the method named “addContactDetails” with the Contact object argument and return type will be the boolean. so after inserting contact into database the method will return true or false boolean to confirm the data is stored in table or not.

b. Reading Contacts

Now let’s write the method named ” readAllContacts ” to reading contacts with return type List.

c. Updating Contact

Define the method to update the contact.

d. Deleting Contact

And method for delete single Contact

Complete ContactHandler Class

4. Test Before use.

Before using Contact Handler Class, let’s take a simple test.

Add few contacts from main file.

And read all inserted contact using LogCat.

Run app on Emulator and see the result into LogCat.

LogCat

OOP PRINCIPLE:COMPOSITION – CREATING RANDOM NUMBER GENERATION CLASS

Creating multiple instances of the same class is called “Composition”. This simple few words single line definition describe the what is composition. but how to use this principle or how it works….

Here is the simple example i have created.

randomN.as [ Document class ]
[as3]
package{
import flash.display.MovieClip;
import flash.events.TimerEvent;
import flash.utils.Timer;
import num;

public class randomN extends MovieClip{

private var create_timer:Timer;

// Constructor
public function randomN():void{
// generating random delay value
// between 500-1000 miliseconds
var delay:Number = Math.random()*800+200;
create_timer = new Timer(delay);

// Start creation
create_timer.addEventListener(TimerEvent.TIMER, create_number);
create_timer.start();
}

private function create_number(e:TimerEvent):void{
var number:num = new num();
addChild(number);
}
}
}

[/as3]

 

Inside the above given document class you can find the instances of the given below class.

num.as [ Random number generation class ]
[as3]
package{
import flash.display.MovieClip;
import flash.events.TimerEvent;
import flash.text.*;
import flash.utils.Timer;

public class num extends MovieClip{

private var format:TextFormat = new TextFormat();
private var fade_timer:Timer = new Timer(100);

// Constructor
public function num(){
// setting up text format
format.color = Math.random()*0xFFFFFF;
format.font = “Arial”;
format.size = 50;

// Generating random number between 1 to 10.
var mynum:Number = Math.floor(Math.random()*9)+1;

// Creating textfield
var mytext:TextField = new TextField();
mytext.text = String(mynum);
mytext.setTextFormat(format);
mytext.selectable = false;
this.addChild(mytext);

// setting x,y position
this.x = Math.random()*500;
this.y = Math.random()*350;

// start fading this
fade_timer.addEventListener(TimerEvent.TIMER, fade);
fade_timer.start();
}

// fade transition
private function fade(e:TimerEvent):void{
var fade_val:Number = (10 – fade_timer.currentCount)/10;
this.alpha = fade_val;

if(this.alpha <= 0){
fade_timer.addEventListener(TimerEvent.TIMER, fade);
fade_timer.stop();
MovieClip(parent).removeChild(this);
}
}
}
}
[/as3]

Here you can find the source of the file.
Download source [randomN.zip]