Concurrent Programming with Java
Lab Manual, Version 1.0, F. Astha Ekadiyanto, 2002.

[Contents] [Next] [Previous]

Lab 2: Building Application


Code the Topology construction in Java Language

Constructing Topology using Bluej in the previous section will certainly consume a lot of time. More over, once you have successfully constructing the topology, you will definitely don't want to go through the same work again each time you have to reconstruct them. But learning to construct the topology manually may help you to know the steps and hopefully be able to write a Java code for it.

This page will guide you to define the topology in a new Class definition called MobileSystem Class. This Class is a subClass of Msc with a defined topology in it. A MobileSystem should have a String name field that represents a Firm/Company/Brand name (i.e. "VodaPhone"). The only additional behavior necessary is regarding to the name of the MobileSystem itself (probably getName()). The topology definition of the MobileSystem will be defined when the MobileSystem Object is initialized. Thus, this should be performed in the Class Constructor method.

To construct the topology, we will define all the steps in constructing topology using BlueJ into Java code.
The steps are as follows:

  1. Create Msc Objects: Since we are defining the topology in the MobileSystem's constructor, it is no need for us to define any Msc creation since whenever the MobileSystem Class is instantiated, the Msc Object which is the superclass the MobileSystem Object will be created inside.
  2. Create Rbs Objects and Cell Objects: Creating Rbs objects can be performed using the following command.

    Cell tempCell = new SquareCell(95,230,20); 
    Rbs tempRbs = new Rbs("RBS-1",tempCell);

    The commands above can be joined into a single line command as follows:

    Rbs tempRbs = new Rbs("RBS-1",new SquareCell(95,230,20));

  3. Registering Rbs to Msc: Similar to the process in BlueJ, the registration of Rbs to Msc can be performed by calling the addRbs(Rbs) method as given in the following.

    addRbs(tempRbs);

    If we observe how lines of command can be joined into a single line, we can do the same for statements in no. 2 and no. 3 to become:

    addRbs(new Rbs("RBS-1",new SquareCell(95,230,20)));

    This will certainly save coding time and make the code easier to read. You can define the other RBS in the same way.

  4. Setting neighboring Rbs: The solution for setting neighboring Rbs is very straight forward. The only difference in how to call the static method.

    Rbs tempRbs1 = findRbs("RBS-1");
    Rbs tempRbs2 = findRbs("RBS-2");
    Msc.setNeighboring(tempRbs1,tempRbs2);

    One may try to join the three lines together the same as the previous codes to become:

    Msc.setNeighboring(findRbs("RBS-1"),findRbs("RBS-2"));

    This is certainly a very nice code, but have a potential error generation in runtime. The error will happen when the findRbs(String) method cannot find the corresponding Rbs and return a null instead. This null will generate error when passed into the arguments. Thus, a safer code will be defining a new method that performs the same as the first solution, but with null checking.

    public void setNeighboring(String station1, String station2) 
    {
        Rbs rbs1 = findRbs(station2);
        Rbs rbs2 = findRbs(station1);
        if ((rbs1 != null) && (rbs2 != null))
            Msc.setNeighboring(rbs1, rbs2);
    }

    You may be curious to find out that the name of the method is the same as the name of the Msc method setNeighboring. This is true with intention.
    Using the same name, the method became overloaded since the argument's type are different. Java will detect the argument type and call the correct method based on them.
    In this case, one can call the setNeighboring("RBS-1","RBS-1") to use the newly defined method, and within the method there is another setNeighboring method to call the original one with Rbs objects as arguments. This property is called polymorphism in Java.

The complete codes of this stage is in the followings.

File name : MobileSystem.java

public class MobileSystem extends Msc
{
   private String name;
   /**
    * Constructor for objects of class MobileSystem
    */
    public MobileSystem(String name)
    {
       //Creating Mobile Network Topology
       this.name = name;
       addRbs(new Rbs("RBS-1", new SquareCell(95,230,20)));
       addRbs(new Rbs("RBS-2", new HexCell(125,210,20)));
       addRbs(new Rbs("RBS-3", new HexCell(90,190,20)));
       addRbs(new Rbs("RBS-4", new HexCell(160,190,20)));
       addRbs(new Rbs("RBS-5", new CircleCell(125,170,20)));
       addRbs(new Rbs("RBS-6", new HexCell(160,150,20)));
       addRbs(new Rbs("RBS-7", new SquareCell(125,130,20)));
       addRbs(new Rbs("RBS-8", new CircleCell(90,150,20)));
       addRbs(new Rbs("RBS-9", new CircleCell(55,170,20))); 
       //Registering neighboring Cells
       setNeighboring("RBS-1","RBS-2");
       setNeighboring("RBS-2","RBS-3");
       setNeighboring("RBS-1","RBS-3");
       setNeighboring("RBS-3","RBS-5"); 
       setNeighboring("RBS-3","RBS-9");
       setNeighboring("RBS-3","RBS-8");
       setNeighboring("RBS-2","RBS-5");
       setNeighboring("RBS-2","RBS-4");
       setNeighboring("RBS-5","RBS-4");
       setNeighboring("RBS-4","RBS-6");
       setNeighboring("RBS-5","RBS-6");
       setNeighboring("RBS-5","RBS-7");
       setNeighboring("RBS-5","RBS-8");
       setNeighboring("RBS-6","RBS-7");
       setNeighboring("RBS-7","RBS-8");
       setNeighboring("RBS-8","RBS-9");
    }
    public String getName()
    {
       return name;
    }
   /**
    * Overflow SuperClass method to enable different argument (Polymorph)
    */
    public void setNeighboring(String station1, String station2) 
    {
       Rbs rbs1 = findRbs(station2);
       Rbs rbs2 = findRbs(station1);
       if ((rbs1 != null) && (rbs2 != null))
            Msc.setNeighboring(rbs1, rbs2);
    }
}

[Contents] [Next] [Previous]