Menu
Question Index
...

What will be the output of the following program?

public class FillFromMugsIntoBuckets {

    public static int NUMBER_OF_MUGS_PER_BUCKET = 8;
    public static int NUMBER_OF_LITERS_PER_MUG = 1.5;

    public static void main(String[] args) {
        System.out.print(encode(100) + " " + encode(50) + " " + encode(25) + " " + encode(12.5));
    }

    public static String encode(double size) {
        String result = "";
        while (size > NUMBER_OF_MUGS_PER_BUCKET * NUMBER_OF_LITERS_PER_MUG) {
            result += "B";
            size -= NUMBER_OF_MUGS_PER_BUCKET * NUMBER_OF_LITERS_PER_MUG;
        }
        while (size > NUMBER_OF_LITERS_PER_MUG) {
            result += "M";
            size -= NUMBER_OF_LITERS_PER_MUG;
        }
        result += String.format("%1.1f", size);
        return result;
    }
}


BBBBBBBBMM1.0 BBBBM0.5 BB1.0 B0.5
BBBBBBBBMM 1.00 BBBBM 0.50 BB 1.00 B 0.50
BBBBBBBBBMMMMMMMMM 1.00 BBBBMMMMMMMMM 1.00 BBMMMM 1.00 BMM 0.50
Some other output or Compilation Error

Doubts

Problems

Topic: Unknown

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App