Answer to Question #310738 in Java | JSP | JSF for shahriar

Question #310738

We want make a package of goal kilos of chocolate. We have small bars (1 kilo

each) and big bars (5 kilos each). Return the number of small bars to use, assuming

we always use big bars before small bars. Return -1 if it can't be done.


makeChocolate(4, 1, 9) → 4

makeChocolate(4, 1, 10) → -1

makeChocolate(4, 1, 7) → 2

Skeleton of the method you should use to return the number:

public int makeChocolate(int small, int big, int goal) {

}


1
Expert's answer
2022-03-13T09:35:39-0400
public static void main(String[] args) {
    System.out.println(makeChocolate(4,1, 9));
    System.out.println(makeChocolate(4,1,10));
    System.out.println(makeChocolate(4,1,7));

}

public static int makeChocolate(int small, int big, int goal) {
    int tmp = goal - big - small;
    if (tmp > small) {
        return -1;
    } else {
        return tmp;
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS