Thursday, December 15, 2011

Collection Framework-Differences between Hash Map, set and Hash Table

Hashtable

Hashtable is basically a datastructure to retain values of key-value pair.

It didn’t allow null for both key and value. You will get NullPointerException if you add null value.
It is synchronized. So it comes with its cost. Only one thread can access in one time
Hashtable; cityTable = new Hashtable();
cityTable.put(1, "Lahore");
cityTable.put(2, "Karachi");
cityTable.put(3, null); /* NullPointerEcxeption at runtime*/

System.out.println(cityTable.get(1));
System.out.println(cityTable.get(2));
System.out.println(cityTable.get(3));
HashMap

Like Hashtable it also accepts key value pair.

It allows null for both key and value
It is unsynchronized. So come up with better performance
HashMap productMap = new HashMap();
productMap.put(1, "Keys");
productMap.put(2, null);
HashSet

HashSet does not allow duplicate values. It provides add method rather put method. You also use its contain method to check whether the object is already available in HashSet. HashSet can be used where you want to maintain a unique list.

HashSet stateSet = new HashSet();
stateSet.add ("CA");
stateSet.add ("WI");
stateSet.add ("NY");

if (stateSet.contains("PB")) /* if CA, it will not add but shows following message*/
System.out.println("Already found");
else
stateSet.add("PB");

Monday, December 12, 2011

ANDROID ACTIVITY CLASS EXPLAINED IN DETAILS

As a fledgling Android developer one of the first things you’ll need to do is get your head around Activity Classes. We can’t overstate the importance of this. A sharp, fluid understanding of how each class interacts with each other class, and the end result for end users will not only speed along your project dev time, but open up new possibilities for your programming. So let’s take a look at these in some detail.
onCreate(): This is called when the activity first starts up. You can use it to perform one-time initialization such as launching the user interface. onCreate() utilizes one parameter that is either null or state information previously saved by the onSaveInstanceState( ) method, discussed below.
onStart(): This indicates the activity is about to be displayed to the user.
onResume(): This is called when the user can start interacting with the activity. This is a good place to start animations and music.
onRestart(): If this is called, it indicates your activity is being redisplayed to the user from a stopped state.
onFreeze(): Allows you to save your current state when one activity is being paused and another one resumes to interact with the user. After being paused, the system may at any time need to stop (or even outright kill) your application in order to claim resources for the current foreground activity. If this should happen, the state you supply here will later be recalled with onCreate(), when the user starts a new instance of your activity.
onPause(): This runs when the activity is about to go into the background, usually because another activity has been launched in front of it. This is where you should save your program’s persistent state, such as a database record being edited.
onStop(): This is called when your activity is no longer visible to the user and it won’t be needed for a while. If memory is tight, onStop() may never be called (the system may simply terminate your process).
onDestroy(): This is called right before your activity is destroyed. If memory is tight, onDestroy( ) may never be called (the system may simply terminate your process).
onSaveInstanceState(Bundle): Android calls this method to allow the activity to save per-instance states, such as a cursor position within a text field. Usually you won’t need to override it because the default implementation saves the state for user interface controls automatically.
onRestoreInstanceState(Bundle): This is called when the activity is being reinitialized from a state previously saved by the onSaveInstanceState() method. The default implementation restores the state of your user interface.
I hope this brings you up to speed on Android Activity Classes. Be sure to have a thorough look at the attached diagram below (click –> full size) for a concrete overview of what we’ve been discussing here. The more familiar you are with Activity Classes and how and they affect the end user experience, the more successful your Android apps development will be.

Wednesday, October 12, 2011

Android Game with Code

Android Game with code for android freshers with and engine.
You can also download full code from fllowing link.
Click here


First you have to configure andEngine lib files.then use following code


public class MainActivity extends BaseGameActivity implements
IOnSceneTouchListener{
// ===========================================================
// Constants
// ===========================================================

private static final int CAMERA_WIDTH = 780;
private static final int CAMERA_HEIGHT = 460;
// ===========================================================
// Fields
// ===========================================================

private BuildableTexture mBuildableTexture;
private TextureRegion mRedBlockTextureRegion;
private TextureRegion mBlueBlockTextureRegion;
private TextureRegion mBisopTextureRegion;


public Engine onLoadEngine() {
final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
final EngineOptions engineOptions = new EngineOptions(true,
ScreenOrientation.LANDSCAPE, new FillResolutionPolicy(), camera);
engineOptions.getTouchOptions().setRunOnUpdateThread(true);
return new Engine(engineOptions);
}


public void onLoadResources() {
mBuildableTexture = new BuildableTexture(1024, 1024,
TextureOptions.BILINEAR_PREMULTIPLYALPHA);
Texture mTexture = new Texture(1024, 1024,
TextureOptions.BILINEAR_PREMULTIPLYALPHA);
TextureRegionFactory.setAssetBasePath("gfx/");
mBlueBlockTextureRegion = TextureRegionFactory.createFromAsset(mBuildableTexture, this, "blueblock.png");
mRedBlockTextureRegion = TextureRegionFactory.createFromAsset(mBuildableTexture, this, "redblock.png");
mBisopTextureRegion = TextureRegionFactory.createFromAsset(mBuildableTexture, this, "bisop.png");


try {
mBuildableTexture.build(new BlackPawnTextureBuilder(1));
} catch (TextureSourcePackingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.getEngine().getTextureManager()
.loadTextures(this.mBuildableTexture, mTexture);
}


public Scene onLoadScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
final Scene scene = new Scene(2);
scene.setBackground(new ColorBackground(0, 0, 0));
scene.setOnSceneTouchListener(this);
return scene;
}

private void initLevel(int[][] level) {

int posx =0;
int posy = 50;
Sprite block = null;

for (int r = 0; r < level.length; r++) {
posx = 200;
for (int c = 0; c < level[r].length; c++) {
Log.d(r+" "+c,level[r][c]+"" );
if (level[r][c] == 1) {
block = new Sprite(posx, posy, mBlueBlockTextureRegion);
this.getEngine().getScene().getLastChild().attachChild(block);
}else if (level[r][c] == 0) {
block = new Sprite(posx, posy, mRedBlockTextureRegion);
this.getEngine().getScene().getLastChild().attachChild(block);
}
posx += block.getWidth();

}
posy += block.getHeight();
}
Sprite bisop = new Sprite(posx+(mBisopTextureRegion.getWidth()/4), posy+(mBisopTextureRegion.getHeight()/4), mBisopTextureRegion);
this.getEngine().getScene().getLastChild().attachChild(bisop);
}

public void onLoadComplete() {
int[][] level1 = { { 0, 1, 1, 1 },
{ 1, 0, 0, 1 },
{ 0, 1, 1, 1 },
{ 1, 1, 0, 1 }
};
initLevel(level1);
}

public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
// TODO Auto-generated method stub
return false;
}
}