Site hosted by Angelfire.com: Build your free website today!
       
The Digital Reference Section


JAVA Methods()
by Rodelio P. Barcenas

JAVA is an object oriented progamming environment. The thrust of JAVA is in the creation of objects. In reality, objects can perform a noticeable action or behavior know as methods(). For example, a car engine is an object, you can see its action (method) such as the rotating action which provides power for the car. Another example can be a dog. A dog is an object, where its actions (methods) are barking, eating, guarding the territory.

To illustrate the discussion using JAVA programming, we then translate the above example into JAVA psuedo codes. For an example, we will use objects, Kate and Larry, both human beings and their two(2) animal dogs Husky and Boomer. Larry and Kate eats dinner. Both of them watch tv. Kate,bored of the movieShow,slept afterwards. Larry felt something unusual and verified his dogs if the dogs are barking. If the dogs are barking, then look around otherwise stops watching TV and fed the two dogs. Later, Larry sleeps afterwards..

public class earth{

// when the earth was created man and woman was
// created namely Larry and Kate
// After human beings are created, animals are created too

humanBeing Larry, Kate;
// Larry and Kate are supposed to be human beings

Larry = new humanBeing("male","6ft","200lbs");
Kate = new humanBeing("female","5.6ft","110lbs");
// Larry and Kate are created and now are now humanBeing
// objects(with distinct qualities) present in the
object earth
// Their qualities are defined during the instance of their
// creation


animalDog Husky, Boomer;
// Husky and Boomer are supposed to be animal dogs

Husky = new animalDog("Terrier","playfull");
Boomer = new animalDog("chow-chow","reserved");
// Two dogs are created with different qualities in an
// instance

 

public static void main(String[] arg){ // This is their life

Kate.eat("Big Mac Hamburger");
Larry.eat("Chicken Nuggets");
Larry.watchTV("MTV");
Kate.watchTV("MTV");
Kate.sleep();
Larry.feed(Husky);
Larry.feed(Boomer);
Larry.checkDog(Husky);
Larry.checkDog(Boomer);
Larry.sleep();

 

}

}

The humanBeing object


public class humanBeing {

private property gender,height,weight;

humanBeing(property g,property h, property w){

gender = g;
height = h;
weight = w;

}

public void eat(food someType){

mouth = someType;
... program how to eat food follows here

}

public void watchTV(movieShow someShow){

if (TV.powerON() = false){

TV.turnpowerON();

}

else {

... do nothing just watch the show;

}

if(TV.movieShow != someShow){

TV.voiceActivate(someShow)

}

else {

... relax and enjoy the show

}

}

public void endWatchTV(){

if(TV.powerON == true){

TV.turnpowerOFF();

}

else {

... its already off don't stare at it!!!

}

}

public void sleep(){

... dont forget to brush your teeth
... take a short bath
... zzzzzzzz

}

public void feed(animalDog dogName){

... prepare the food
dogName.eat("bone");

}

public void checkDog(animalDog dogName){

if (dogName.bark==true){

lookAround();

}

else {

... ah never mind, don't bother at all

}

}

}

The animalDog object


public class animalDog{

private qualities breed,attitude;

animalDog(quality b, quality a){

breed = b;
attitude = a;

}

public static boolean bark(){

if (smellSomething())or(hearSomething())or feelMovement()) {

return true;

}

else return fase;

}

public static boolean smellSomething(){

... a program how to smell
if smell something unusual return true
else return false

}

public static boolean hearSomething(){

... a program of dog using his ear and listen to environment
if heard of strange sound return true
else return false

}

public static boolean feelSomething(){

a program how to uses other senses for feeling.
returns true if feel something strange
return false if feel nothing strange

}

public void eat(food dogfood) {

mouth = dogfood;
digest();
drink();

}

}



The PinoyTechZone.Com
Copyright 2002-2003
All rights Reserved