Archive

Archive for the ‘Java’ Category

Difference between interface and abstract in OOP

October 11, 2010 Leave a comment

Now what is interface really?Interface is kind of like template.In that you tell people what standard they should follow but not telling them how to.For example.. assume you create box when people order it to you.In that case every  time they order a box with telling you ”i want some box like that you made for my neighbour”  then you define properties and  behaviour of box yourself.After some days your  workload has been so overflowed that you have to open your own company and need to recruit some worker in that company.And later you’ve to open some branches for it.Now in that case again when order comes to you ,you are not going to go to the every branch to do the job.You need to give some kind of standard to the branch manager’s or ceo whatever you call about how they should  make a box.Give them some template of box where you define what a box should behave when they create it.Tell them something like this:

public interface boxTemplate{

double setWidth(double w);

void getWidth();

double setHeight(double h);

void getHeight();

double setLength(double l);

void getLength();

double getArea();

}

And then one of the branch,design a box class something like this:

public class box1 implements boxTemplate{

public double width;

public double height;

public double length;

public box1(double wd,double hg,double ln){

this.setWidth(wd);

this.setHeight(hg);

this.setLength(ln);

}

public void setWidth(double w){

this.width=w;

}

public    double getWidth(){

return width;

}

public    void setHeight(double h){

this.height=h;

}

public    double getHeight(){

return height;

}

public    void setLength(double l){

this.length=l;

}

public    double getLength(){

return length;

}

public    double getArea(){

return (width*height*length);

}

}

No matter how all branches want to make a box, should always follow your template .Box class design of different branches can differ from one another but since they all are implementing your boxTemplate you know that what behavior a box can behave when it’s ready no matter what it’s origin is.

Abstract:

One day somehow you get a huge order of making box but with some condition.By imagining that you can be next millioniare you accepted those condition.Now the condition was that they want box with same width and length but with red,orange,yellow,green,black,white  in  color and different type of height.Since it’s a big order  you call your three branch manager.And give all of them a new kind of template like this:

abstract class boxNewTemplate {

public double width=22.0;

public double length=22.0;

boxNewTemplate()

{

this.setWidth(this.width);

this.setLength(this.length);

}

public void setWidth(double w){

this.width=w;

}

public    double getWidth(){

return width;

}

public    void setLength(double l){

this.length=l;

}

public    double getLength(){

return length;

}

abstract void setHeight(double h);

abstract double getHeight();

abstract void setColor(int colorCode);

abstract double getArea();

}

And then tell one of them to make boxes where half of them should be red and half orange and you also told the same to others for making other colors.After that one of them create a box class like this:

public class box2 extends boxNewTemplate{

static final int RED=1;

static final int ORANGE=2;

public double height;

public box2(double hg,int colorCode) {

super();

this.setHeight(hg);

this.setColor(colorCode);

}

public  void setHeight(double h){

this.height=h;

}

public    double getHeight(){

return height;

}

public    double getArea(){

return (width*height*length);

}

public void setColor(int colorCode){

if(colorCode==RED){

//give the color

}else if(colorCode==ORANGE){

//give the color

}else{

//do nothing

}

}

}

So the meaning of abstract class is that you do some implementation of your own to a class but some of them you leave for others.So if someone wants to include your class in their system they just have to implement those things that you did not in that class and you do that by telling them it’s abstract.Now if a method is not implemented yet which is abstract but most of the method of that class are how they are going to know that some of this class’s mehod is not implemented yet if you do not put abstract  before that class name.Think about it.

I think that explains a lot about interface and abstract.

Again what you are reading in here is the concept that i figure out myself.It can contain any or many conceptual error.If you find some,feel free to tell me about it.And also if you have any question feel free to ask.

HAVE A GOOD PROGRAMMING

Categories: Java, OOP Tags: ,

Concept of Object in OOP

October 9, 2010 Leave a comment

When we  first try to learn java(Object Oriented Programming),we all must have a conflict with Object.To be a developer having the idea about,”what is object?” is must.You must have a basic understanding about Object.Cz ,by the time pass by you will see that all things ,if it gui or others it always end up with concept of Object.So it is really very very important to have clear concept of Object Oriented Programming.

Now when u try to understand oop the very first question comes in mind is that What is this Object means?

Object is something that has some properties.And has some behaviour that acts with those properties.I am going take an example of Box.Now a box is cube like thing.It has width,height,length.If you assume those are it’s properties then i will say finding it’s area can be considered as it’s behaviour .Again if you are still confused about behaviour now what is behaviour in plain text?Think about yourself.Can you feel your skin?… J of course you can.When it is cold what do u do?And when it is summer i don’t think you do same thing as you do at the time of winter.So i can say the behaviour of your ,change when the behaviour or properties of something other change.See deep inside this concept,the behaviour change of weather will change the body temperature property of your thus your behaviour will also change.Go back to the box.If you change any property of it,it will affect the area of it.Now you can think of as many as behaviour you can.It’s up to u.If someone told you that i want some box with different type of area and also with different type of color.Then you can add a property of color with also add one extra behaviour in it that is going to set it’s color.You can imagine anything as object.From electron to whole universe.Since you are probably not here to assume the whole world as object but  to understand object in respect to programming language.What about a window or the browser you are seeing or a button?All can be imagined as object.A window has title bar, closing option,minimizing option e.t.c.When you click on minimize, it’s behaviour should be minimizing the window.What about the properties clicking on it?Clicking on it will fire a change in properties will fire to it’s corresponding behaviour.Take another example of human.The lowest distance of human eye that it can see is 25cm.Now if you are wearing a glass or your friend is,tell yourself or him what happened why are you wearing a glass?Probably the answer will be lowest distance of eye has been reduced or increased.So if i tell you lowest _distance is a property of another property (can also be considered  as object itself)of human which is eye.Well sometimes creator create human’s with a defected eye or  anything can happen to one’s eye.Every human has a property of eye and it can be true or false depends on ,if it can see or not.Now the behaviour of eye is to see something where it has a property of seeing something with minimum distance of 25cm.If this 25cm is changed by some another behaviour then it will fire a change in the lowest_distance thus the behaviour  of eye change thus you or your friend or your neighbour wear a glass of plus or minus power.

I think that’s explain a lot what the object is.

One thing you should know that what you are reading in here is the concept that i figure out myself.It can contain any or many conceptual error.If you find some,feel free to tell me about it.And also if you have any question feel free to ask.

And “HAVE A GOOD PROGRAMMING

 

Categories: Java, OOP Tags: ,

beginning android with hello world

September 27, 2010 Leave a comment

In my last post we have talked about how to configure everything to start your android development.
Now in this post i will show you how we can create a very basic “HelloWorld” application for android.
And after that we will be doing some modification on our apps.

so are you ready to go..”yeah!”.but i am not ready.just kidding 🙂

steps:

1:Do i have to tell you that “Open your eclipse”?????? 🙂 i think not
then
you do what i did in my first android tutorial to create an new android project.

2:Ok, now that you’ve created a project run it as android application.

3:There you go…. you should see a virtual device saying “Hello World  (probably your project name)”.

4:Now we want to change something in it.Say… “Hello saiful103a”
So how do we do that
i.go res->values and open strings.xml
ii.Now you should understand where does this “Hello World ..” was coming from
iii.Now you’d think how this sentence is accessed from main file well then open gen->R.java
you should see an inner class called string. In that method what you see compare it with the strings.xml file
then you will understand not everything but enough to start with.
iv.Oh did you change the sentence from hello world to hello saiful103a.
v.Now run it….. voila….

is it worked ????yes then we should take a coffee break …..:)

5:Now we want our text to be red
create a file named colors.xml into res->values folder
inside that paste the below code
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorOfText">#ff0000</color>
</resources>

6.Open main.xml you should see a TextView element this element decide how the text will appear
on the screen upon it’s given attribute.
since we want to change color of the text add an extra attribute
->android:textColor="@color/colorOfText"
after that textview should be seen like:
<TextView
android:textColor="@color/colorOfText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>

save it….
run it……….
voila…………..

so isn’t that (i will not say awesome cz we do not yet explored it deep enough but we will be) goooooood..:)

but that’s it for today…

So .. “HAVE A GOOD PROGRAMMING”

Categories: Android, Java

oracle with netbeans ide…..

November 8, 2009 Leave a comment

somedays ago one of my friends just ask me that did i know how to connect oracle with netbeans ide?I said yes i know.And i have told some about it and i think that will help him,and also i think why don’t i share this topic in my blog,then the others can know it too.So here it goes..

open your ide. then you will see like this

1

go to databases, then right click on it.
and go to “new connection”

2

after that

3

in the name field select  “new driver”

4

now you will see something like this:

5

now click on add button and give it the location of  your oracle driver file
for mine it was:
…oracle/product/10.1.0/db_2/jdbc/lib/ojdbc14.jar
after adding this jar file your netbeans will automatically do the rest of the work for you.
Now Driver class:oracle.jdbc.OracleDriver
then look at your database url,it will be look like
“jdbc:oracle:*1:*2:*3:*4”

Now in no.
*1:
try first giving
oci8
if it don’t work with your driver then try
thin
*2:
@localhost
*3:
port number of your server
if you don’t know in which port is your server is active
then see java-oracle
tutorial.After getting the port number write it to in “*3”
*4:
in here you have to put session id[SID] from server.If you don’t know the [SID] then you can get it by following above tutorial

For me it was..
“jdbc:oracle:thin:@localhost:1521:orcl”

Now you have to give the username and password,for which user do you want to connect.
[remember the username and password is for ur database user
e.g like “system”,”manager” is a user ]
for me i have another user ‘saiful103a’ beside system so i give
user:saiful103a
pass:saiful103a
now connect with ur given username
and see the power of netbeans over oracle..
connect

And that’s it

And HAVE A GOOD PROGRAMMING

Categories: Java, Oracle

java oracle connection

July 16, 2009 1 comment

Tomorrow i was trying to connect my java program with oracle server.It was not easy for me to do this.Since i’m a new learner.I google it about half an hour.I read every page that seems to me helpful.Then i finally got the solution.If i describe you,you will be astonished and will say to yourself it is nothing at all.Then let go…

Check your configuration with mine:
OS: Windows Xp
JDK Version: JDK1.6
IDE: Any ide or compiler will work
Oracle:10g

open ur ide or compiler. now create a new project ‘database’.When you will give ur project name meantime you have to notice that anywhere there is a option that have classpath or not.If there is open your classpath settings.Now add a new class.Locate this class to

…oracle/product/10.1.0/db_2/jdbc/lib/ojdbc14.jar

If you you don’t have ojdbc14.jar then ojdbc[].jar can work for you.Try it.Or you can download it.From oracle website.

ok,now we locate a class that can connect our jdk with oracle.

If you know in which port your oracle server is active then ok,if not
open you command prompt in your windows
now type lsnrctl status
what have you seen.From results you will find

Listening Endpoints Summary…
(DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(PIPENAME=\\.\pipe\EXTPROC0ipc)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=myhostname)(PORT=1521)))

now we need the name of SID
Now open your sql+ window from oracle
type
show parameter db_name

you will get a value [for me it was oracl]

this.So PORT=1521 is your port no.
Now,In your project creat a new file named ‘database.java’


import java.sql.*;

public class database
{
public static void main(String[] args)
{
DB db = new DB();
Connection conn=db.dbConnect(
"jdbc:oracle:oci:@localhost:1521:oracl", "username", "pasword");
}

}

class DB
{
public DB() {}

public Connection dbConnect(String db_connect_string,
String db_userid, String db_password)
{
try
{
DriverManager.registerDriver(
new oracle.jdbc.OracleDriver());

Connection conn = DriverManager.getConnection(
db_connect_string, db_userid, db_password);

System.out.println("Connection Successful");
return conn;

}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
};

And that’s it.And it is definitely worked for me.Hope,will work for you too.Have a nice programming.

Categories: Java, Oracle