OOP(s) I Did It! Really? WARNING: CAN BE BORING!

Salam & Hello World!

Hehe today i would like to try and explain JAVA's Object Oriented Programming (OOP), my way. Why? Because my test on OOP is next week and i have to know that i got it straight.

Firstly, what's OOP like?
Well, it's like when you have 10 bulbs in a parallel connection, and like when one blows up the others still lights. Means like, theres these Objects linked to one another through the usage of Methods right? And if one method fails, like blows up and stuff, others can still function perfectly, given zero errors. You dig? Like I have a calculator, it has four basic functions: add, minus, multiply and divide. If the 'add' method crashes, minus, multiply and divide would still work. Get that? It's like this file grabbing the other file's stuff and using it.

So, why is it OBJECT Oriented?
SO, like everything in the JAVA world is like real life man~ Were all like objects. Objects have distinct traits/attributes/details which gives the character/look/shape. Like, a box is a box because it has width and height and breadth (lebar). A car is a car because it has four wheels, uses gas, looks like a car and stuff. So to make it simple, objects in JAVA world, has its own details of shapes and sizes. Example:

#1 (The object)
there's a box.

#2 (The obvious)
boxes have width.
boxes have height.

#3 (Normal/Un-altered/Unchanged details)
box has 1cm width by default.
box has 1cm height by default.

#4 (If its going to be altered, what part?)
give box newWidth.
give box newHeight.

#5 (What can you get from those details?)
box's Area (width*height).
box's Parameter(2*height+2*width).

Turn them into JAVA codes, and you get:

class Rectangle{ //The Object
double height; // The obvious
double width; // The obvious

Rectangle(){//default details
height=1.0;
width =1.0;
}//end default con

Rectangle(double newHeight, double newWidth){ //If values altered
height=newHeight;
width =newWidth;}

double getArea(){ //What can be obtained
return height*width;}

double getParameter(){
return 2*height+2*width;}

}//end Rectangle

So what now? Is it even a complete program?
NO. The one i just showed, was a simple blueprint of the program. It's like just a plan of what it can or cannot do. Something like a recipe instruction. It's there but you need to MAKE the recipe because it can't make itself. Still, save it in JAVA format k.

Speaking of recipes, WHAT?
It's like this, sometimes, you make according to the cookbook, sometimes you don't. If you do, you are following the DEFAULT/UNALTERED values stated inside the book. If you are not, then you are using your own values/measurements in the recipe/program. Look at the code below.

public class TestRec{
public static void main (String [] args){

Rectangle myBox = new Rectangle(40,4); //Summoning Rectangle, giving myBox new values

System.out.println("The area of a box with width "+myBox.width+" & height "+myBox.height+
" is "+myBox.getArea()+" cm^2.");

System.out.println("The parameter of a box with width "+myBox.width+" & height "+myBox.height+
" is "+myBox.getParameter()+" cm.");


Rectangle Box2 = new Rectangle(35.9,3);

System.out.println("\nThe area of a box with width "+Box2.width+" & height "+Box2.height+
" is "+Box2.getArea()+" cm^2.");

System.out.println("The parameter of a box with width "+Box2.width+" & height "+Box2.height+
" is "+Box2.getParameter()+" cm.");

}//end main
}//end TestRec

Kay, this code, is like the ingredient to test your recipe. So we call it TestRec(tangle). If you notice the parameters of Rectangle(), i used 40,4 inside it. That is modifying the recipe ok? Remember the default value? 1.0 and 1.0 in height and width? This means that TestRec calls up the Rectangle 'recipe' and modifies its values before processing it. Kind of like scribbling on the recipe book to modify the values. So then, after modifying the values, we use it to our liking.

Liking? What THE HELP?
Ok, now that you have a modded recipe/object, it is counting different values, namely 40=height and 4=width. SO i use those numbers and get the program to call/summon what the new width and height is, then use it to calculate the Area and Parameter. Oh and to call some functions, put the name of the reference you're using (myBox, is the reference because this new recipe is NOT the old one), type a dot(.) and the method you want to use (getArea()/getParameter()). So it becomes myBox.getArea().

So what if i do not change the value?
Simple, go old school. The program you're gonna make follows the default settings, 1.0 and 1.0.
And that, is basic OOP shizzle.

So that's it?
Pretty much. Dunno if i can make it any clearer. Sorry if you are not. One more say: if OOP was a recipe, you test the ingredients, as in, the TestRec, not the cookbook, a.k.a Rectangle k. Cause you can only see an output from your input. So RUN the ingredients, not the recipe k. Oh and those stuff i about default values, that's handeled by a default constructor. Not that it's important, just telling hehe.

Salam. Happy programming!

p/s: If the TestRec doesn't work out, adjust the words in the brackets =p Next time I'll tell about static, public and private objects.

5 text messages:

AwoL said...

hahahaa.. OOP?
bosan r wei..

Fær.han/Unown F/Gyan said...

Ler tau ar ko terer~ Ajar r aku huhu~

aidadidu said...

borrringggggg!

hahahaha
;P

Fær.han/Unown F/Gyan said...

I warned you hehe~ Besides, not for you all laa~ For my classmates!

moonsyi said...

tell me more about the private objects!!!! izzit long? izzit soft? ahahaahahah

twatter

follow.me.k, i'll follow u later =)

wat.u.missed

chocolates...

BLOGGER TIPS BlogMalaysia.com