Question

JAVA programming, please help /** * Write the method named makeBricks(). * * We want to...

JAVA programming, please help

/**
* Write the method named makeBricks().
*
* We want to make a row of bricks that is goal
* inches long. We have a number of small bricks
* (1 inch each) and big bricks (5 inches each).
* Return true if it is possible to make the
* goal by choosing from the given bricks.
*
* Examples:
* makeBricks(3, 2, 9) returns false
* makeBricks(1, 4, 12) returns false
* makeBricks(2, 1000000, 100003) returns false
* makeBricks(3, 1, 8) returns true
* makeBricks(3, 1, 9) returns false
* makeBricks(3, 2, 10) returns true
*
*
* @param small number of small bricks.
* @param big number of big bricks.
* @param goal number of bricks desired in row.
* @return true if possible with available bricks.
*/

public boolean makeBricks(int small, int big, int goal)

{
// TODO - Write the makeBricks method here

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please let me know if you have any doubts or you want me to modify the answer. And if you find this answer useful then don't forget to rate my answer as thumps up. Thank you! :)


public class MakeBricks {


    public static boolean makeBricks(int small, int big, int goal) {
        if (goal == 0) {
            return true;
        } else if (goal != 0 && small == 0 && big == 0) {
            return false;
        }

        if (big != 0) {
            int times = goal / 5;
            if(times > big){
                times = big;
            }
            goal = goal - (times * 5);
            big = 0;

        }else if(small != 0){
            goal = goal - small;
            if(goal < 0){
                goal = 0;
            }

            small = 0;
        }
        return makeBricks(small ,big, goal);
    }

    public static void main(String[] args) {
       System.out.println(makeBricks(3, 2, 9));
       System.out.println(makeBricks(1, 4, 12));
       System.out.println(makeBricks(2, 1000000, 100003));
       System.out.println(makeBricks(3,1,8 ));
       System.out.println(makeBricks(3, 2, 10));
        System.out.println(makeBricks(3, 1, 9));
    }
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------



Add a comment
Know the answer?
Add Answer to:
JAVA programming, please help /** * Write the method named makeBricks(). * * We want to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT