Ambrose Suen
Ambrose Suen

Reputation: 13

my integer counter doesn't work if i apply more than 1 method

I am using supplyCounter which is a global variable to count how many times did the manufacture generate a new random number to the stock

public class test {

private static Random manufacturer;
private static ArrayList<Integer> ordersForFirstFit, stocksForFirstFit, ordersForBestFit, stocksForBestFit;
private static int supplyCounter = 0;

public test() {

}

public static void main(String[] args) 
{
    ordersForFirstFit = createCustomerOrders(1000);
    ordersForBestFit = ordersForFirstFit;// using same data as first fit for best fit
    stocksForFirstFit = createStockOfRope();
    stocksForBestFit = stocksForFirstFit;// using same data as first fit for best fit
    manufacturer = new Random();

    supplyCounter = 0;
    firstFitAlgorithm(stocksForFirstFit, ordersForFirstFit);
    //showCustomerOrders(ordersForFirstFit);//for testing and debugging
    //showStockOfRope(stocksForFirstFit);//for testing and debugging

    supplyCounter = 0;
    bestFitAlgorithm(stocksForBestFit, ordersForBestFit);
    //showCustomerOrders(ordersForBestFit);//for testing and debugging
    //showStockOfRope(stocksForBestFit);//for testing and debugging

}

public static ArrayList<Integer> createCustomerOrders(int n) {
    ArrayList<Integer> ropeOrder = new ArrayList<Integer>();
    Random randomOrders = new Random();
    for(int i = 0; i < n+1; i++) 
    {
        ropeOrder.add(randomOrders.nextInt(100)+1);// because the range include 0 but exclude 100, therefore +1
        //System.out.println("Rope Orders "+i+": "+ropeOrder.get(i));
    }
    return ropeOrder;
}

public static void showCustomerOrders(ArrayList<Integer> ropeOrder) {
    for(int i = 0; i < ropeOrder.size()-1; i++) 
    {
        System.out.println("Rope Orders "+i+": "+ropeOrder.get(i));
    }
}

public static ArrayList<Integer> createStockOfRope() {
    ArrayList<Integer> ropeStock = new ArrayList<Integer>();
    Random randomStocks = new Random();
    for(int i = 0; i < 6; i++) 
    {
        ropeStock.add(i,randomStocks.nextInt(100)+101);//for a range between 100 to 200
        //System.out.println("Rope(s) in Stock "+i+": "+ropeStock.get(i));
    }
    return ropeStock;
}

public static void showStockOfRope(ArrayList<Integer> ropeStock) {
    for(int i = 0; i < ropeStock.size()-1; i++) 
    {
        System.out.println("Rope(s) in Stock "+i+": "+ropeStock.get(i));
    }
}

public static void insertionSort(ArrayList<Integer> data) {
    //initialize variables
    int key = 0, j = 0;

    for(int i = 1; i < data.size(); i++)
    {
        key = data.get(i);
        j = i;
        while( (j>0)&&(key < data.get(j-1)) )
        {
            data.set(j, data.get(j-1));
            j = j-1;
        }           
        data.set(j, key);
    }
}


public static void firstFitAlgorithm(ArrayList<Integer> ropeStock, ArrayList<Integer> ropeOrder) {
    //int supplyCounter = 0;
    long begin = System.nanoTime();

    for(int i = 0; i < ropeOrder.size(); i++) 
    {
        int j = 0;
        while( ropeOrder.get(i) > ropeStock.get(j) )
        {
            if(j == ropeStock.size()-1)
            {
                ropeStock.add(j,manufacturer.nextInt(100)+101);//Buy a rope from manufacturer then put the rope to the stock
                supplyCounter++;
                //then check to see whether it fits the requirement, if not, do the previous step again
            }
            else
                j++;
        }

        if( ropeOrder.get(i) <= ropeStock.get(j) )
        {
            if(ropeStock.get(j) - ropeOrder.get(i) > 5)//Assume rope coils shorter than 5m are scrapped
            {   
                ropeStock.set(j, ropeStock.get(j) - ropeOrder.get(i));
                ropeOrder.set(i, 0);
            }
            else
            {
                ropeStock.remove(j);
                if(ropeStock.size() == 0)//when the last rope in stock is sold, buy a new coil of rope from manufacturer
                    ropeStock.add(manufacturer.nextInt(100)+101);
                supplyCounter++;
                ropeOrder.set(i, 0);
            }
        }
    }

    long total = System.nanoTime() - begin;
    System.out.println("The time spent for Firstfit is: "+total+" nanoSeconds");
    System.out.println("The number of rope supplied by manufacturer for Firstfit is: "+supplyCounter+" ropes");

}

public static void bestFitAlgorithm(ArrayList<Integer> ropeStock, ArrayList<Integer> ropeOrder) {
    //int supplyCounter = 0;
    long begin = System.nanoTime();

    insertionSort(ropeStock);
    insertionSort(ropeOrder);

    int j = 0;
    for(int i = 0; i < ropeOrder.size(); i++) 
    {

        while( ropeOrder.get(i) > ropeStock.get(j) )
        {
            if(j == ropeStock.size()-1)
            {
                ropeStock.add(manufacturer.nextInt(100)+101);//Buy a rope from manufacturer then put the rope to the stock
                insertionSort(ropeStock);//then check to see whether it fits the requirement, if not, do the previous step again
                j++;
                supplyCounter++;
            }
            else
                j++;
        }

        if( ropeOrder.get(i) <= ropeStock.get(j) )
        {
            if(ropeStock.get(j) - ropeOrder.get(i) > 5)//Assume rope coils shorter than 5m are scrapped
            {   
                ropeStock.set(j, ropeStock.get(j) - ropeOrder.get(i));
                insertionSort(ropeStock);
                ropeOrder.set(i, 0);
            }
            else
            {
                ropeStock.remove(j);
                if(ropeStock.size() == 0)//when the last rope in stock is sold, buy a new coil of rope from manufacturer
                    ropeStock.add(manufacturer.nextInt(100)+101);
                supplyCounter++;
                ropeOrder.set(i, 0);
            }
            j = 0;
        }
    }

    long total = System.nanoTime() - begin;
    System.out.println("The time spent for Bestfit is: "+total+" nanoSeconds");
    System.out.println("The number of rope supplied by manufacturer for Bestfit is: "+supplyCounter+" ropes");
}
}

the output is

The time spent for Firstfit is: 5009697 nanoSeconds
The number of rope supplied by manufacturer for Firstfit is: 649 ropes
The time spent for Bestfit is: 5358148 nanoSeconds
The number of rope supplied by manufacturer for Bestfit is: 0 ropes

but if I only run the bestfit algorithm the counter return a reasonable value which is not 0

public static void main(String[] args) 
{
    ordersForFirstFit = createCustomerOrders(1000);
    ordersForBestFit = ordersForFirstFit;// using same data as first fit for best fit
    stocksForFirstFit = createStockOfRope();
    stocksForBestFit = stocksForFirstFit;// using same data as first fit for best fit
    manufacturer = new Random();

    supplyCounter = 0;
    bestFitAlgorithm(stocksForBestFit, ordersForBestFit);
}

result:

The time spent for Bestfit is: 31895376 nanoSeconds
The number of rope supplied by manufacturer for Bestfit is: 465 ropes

I also tried to create the counter within the method instead of using global variables, but I got the same results.

Do anyone know what is happening?

Upvotes: 1

Views: 79

Answers (1)

Josh Chappelle
Josh Chappelle

Reputation: 1588

Your problem is that you are sharing data between your two algorithms.

Change

ordersForBestFit = ordersForFirstFit;

To

ordersForBestFit = new ArrayList<Integer>(ordersForFirstFit);

And change

stocksForBestFit = stocksForFirstFit;

To

stocksForBestFit = new ArrayList<Integer>(stocksForFirstFit);

The contents of the ArrayList will be the same integers. But when you sort the list in the first algorithm, it is affecting the state of the ArrayList, which is causing problems for your next algorithm. By creating a new ArrayList and passing that to the second algorithm, you avoid the side effects of the first algorithm.

Upvotes: 1

Related Questions