Calculate work progress in percentage of batches that contain tasks and task contain sub tasks I am trying to calculate work progress in percentage.  I have multiple batches, and each batch contains a set of tasks and each task also contains a set of sub tasks.  Below code is the best I could do.  Is there any more efficient way to do this?

I just edited the code using the Math static method to do help with rounding and getting the next up value.

    double numOfBatches = 3;
    double weightOfEachBatch = (1*100)/numOfBatches;

    double numOfTasksInEachBatch = 1;
    double weightOfEachTask =
        (1*weightOfEachBatch)/numOfTasksInEachBatch;

    double numOfSubTasksInEachTask = 2;
    double weightOfEachSubTask =
        (1*weightOfEachTask)/numOfSubTasksInEachTask;

    System.out.println("Weight of each batch: " + weightOfEachBatch + "%");
    System.out.println("Weight of each task: " + weightOfEachTask + "%");
    System.out.println("Weight of each sub task: " + weightOfEachSubTask + "%");

    double fromBatchWeight = 0;
    for(int i = 1; i &lt;= numOfBatches; i++) {

        double toBatchWeight = weightOfEachBatch * i;
        System.out.println(
                "Batch " + i +
                " from " + Math.floor(fromBatchWeight) +
                "% to " + Math.floor(toBatchWeight) + "%");

        double fromTaskWeight = fromBatchWeight;
        for(int j = 1; j &lt;= numOfTasksInEachBatch; j++) {

            double toTaskWeight = (weightOfEachTask * j) + fromBatchWeight;
            System.out.println(
                    "\tTask " + j +
                    " from " + Math.floor(fromTaskWeight) +
                    "% to " + Math.floor(toTaskWeight) + "%");

            double fromSubTaskWeight = fromTaskWeight;
            for(int k = 1; k &lt;= numOfSubTasksInEachTask; k++) {

                double toSubTaskWeight = (weightOfEachSubTask * k) + fromTaskWeight;
                System.out.println(
                        "\t\tSub Task " + k +
                        " from " + Math.floor(fromSubTaskWeight) +
                        "% to " + Math.floor(toSubTaskWeight) + "%");

                fromSubTaskWeight = Math.nextUp(toSubTaskWeight);
            }

            fromTaskWeight = Math.nextUp(toTaskWeight);
        }

        fromBatchWeight = Math.nextUp(toBatchWeight);
    }


The output looks like this

Weight of each batch: 33.333333333333336%
Weight of each task: 33.333333333333336%
Weight of each sub task: 16.666666666666668%
Batch 1 from 0.0% to 33.0%
    Task 1 from 0.0% to 33.0%
        Sub Task 1 from 0.0% to 16.0%
        Sub Task 2 from 16.0% to 33.0%
Batch 2 from 33.0% to 66.0%
    Task 1 from 33.0% to 66.0%
        Sub Task 1 from 33.0% to 50.0%
        Sub Task 2 from 50.0% to 66.0%
Batch 3 from 66.0% to 100.0%
    Task 1 from 66.0% to 100.0%
        Sub Task 1 from 66.0% to 83.0%
        Sub Task 2 from 83.0% to 100.0%
System.out.println("\t\tSub Task " + k + " from " + Math.floor(fromSubTaskWeight) + "% to "
    + Math.floor(toSubTaskWeight) + "%");


I'd use System.out.format here, it's easier to read than string concatenation:

System.out.format("\t\tSub Task %d from %f%% to %f%%%n", 
    k, Math.floor(fromSubTaskWeight), Math.floor(toSubTaskWeight));


With that you can easily format floating numbers.

Weight of each task: 33,333333%
Weight of each sub task: 16,666667%


Could be a readable

Weight of each task: 33,33%
Weight of each sub task: 16,67%


With

System.out.format("Weight of each task: %.2f%%%n", weightOfEachTask);
System.out.format("Weight of each sub task: %.2f%%%n", weightOfEachSubTask);

Having three nested for loops is hard to read. I'd try to extract out a printSubtaskWeights and a printTaskWeights method.
I've seen earlier the numOfSubTasksInEachTask style variables but I've found subTasksInEachTaskNumber more easier to work with. Maybe because if more than one variable starts with numOf it's hard to differentiate them.
i, j, k variables could be more descriptive and could express their purpose. For example, 
batchNumber, taskNumber and subtaskNumber.
Batches, tasks and subtasks looks very similar. I'd try to create a common abstraction for them and use that three times instead of duplication.

public class Item {

    private int subItemCount;
    private double weight;

    public Item(final int subItemCount, double weight) {
        super();
        this.subItemCount = subItemCount;
        this.weight = weight;
    }

    public int getSubItemCount() {
        return subItemCount;
    }

    public double getOneSubItemWeight() {
        final double subItemWeight = (1 * weight) / subItemCount;
        return subItemWeight;
    }

    public String getWeightInfo() {
        return String.format("Weight of each %s: %.2f%%", 
            name, getOneSubItemWeight());
    }
}


(I'd call it Task but it hasn't already been used.)

I started refactoring the code but I guess I won't finish it. The beginning of the code looked like this:

final Item batches = new Item(3, 100);
final Item tasks = new Item(1, batches.getOneSubItemWeight());
final Item subtasks = new Item(2, tasks.getOneSubItemWeight());


100 is the batches' weight, tasks and subtasks inherit the weight of their parents.

Note that the duplicated Weight of each print statement was also moved to the Item class.

I'd also try to move a loop into the Item class and call subitem printing from that method. I'd reduce the number of loops to one. Something like this: 

public class Item {

    private final Item subitems;
    ...

    public void print(double fromWeight) {
        for (int item = 1; item &lt;= itemCount; item++) {

            double toWeight = getItemWeight() * item;
            System.out.format("%s %d from %f%% to %f%%%n", 
                name, item, Math.floor(fromBatchWeight),
                Math.floor(toBatchWeight));

            if (subitems != null) {
                subitems.print(fromWeight);
            }
            fromWeight = Math.nextUp(toWeight);
    }
}