Monday, May 25, 2015

Graphs and Trees : Part 3



Yippy  ...I have written a program and that too in java.

The thing I learned is that Java is much better than C for most of the programs we write until and unless we are specifically writing code for machines with memory...like embedded device.


The program is to find outdegree and indegree of Graph.
The input will be stored in 2-D matrix format.


Since the program is in java , we have one class

it will have method to calculate Outdegree and Indegree

The method will be not of Class but of Objects/Instance and since the methods and not STATIC.
We will have variables 2D matrix , size of matrix. Now these will be also not STATIC, since these will be class variables.

Something I learned during debugging is
1) main method is always static, since it will be called once and is not object method , it is class method

2) From the Main method, we can access only static methods and variables. hence we need to instantiate Object to do anything in Object Oriented Class.

3) Good thing about Object is 1) by default all object have their own variables without us specifically mentioning , it provides SAFETY as change in one object variables can't change other Object variable. 2) Object variables act as GLOBAL variables of that object , so we can access any variable of the OBJECT in any method.

4) In case we need some common variable across all objects, we need essentially CLASS variable so just have STATIC in front of that variable of method which is common of all objects.

Below is GD link for the program

https://drive.google.com/file/d/0B6HF3JcgAklvYjN4QmVOVTlkRkU/view?usp=sharing

No comments:

Post a Comment