public boolean makeBricks(int small, int big, int goal) {
if (small >= goal || (big * 5) >= goal)
return true;
else if (small + (big * 5) >= goal)
return true;
return false;
}Any help is greatly appreciated.
12 replies to this topic
#1
Posted 10 March 2011 - 09:36 PM
So there's a website called CodingBat that people can use to improve their Java and Python programming skills. There's a specific problem that I just can't get through, the "makeBricks" problem on the Java Logic-2, the very first one. Here's my solution so far:
|
|
|
#2
Posted 10 March 2011 - 10:00 PM
There's a logic error with (big * 5) >= goal.
Suppose our goal is 13, and we pass in 3 big bricks into the function. since 3*5 = 15, and 15 is >= 13, the function would return true, even though it is not possible to construct a goal of 13 inches using only 5 inch bricks.
Suppose our goal is 13, and we pass in 3 big bricks into the function. since 3*5 = 15, and 15 is >= 13, the function would return true, even though it is not possible to construct a goal of 13 inches using only 5 inch bricks.
#3
Posted 10 March 2011 - 10:06 PM
Ooh wait, I think I got it now. When I was doing the problem, I was thinking that it doesn't matter how the 1-inch bricks and the 5-inch bricks would combine, as long as it's at least equal to the goal.
But, when I change it to == instead of >=, I just get more errors.
UPDATE:
I believe I get the concept now, but I'm just trying to figure out how to select just the right amount of 1-inch bricks and 5-inch bricks and get the goal. So, let's say the goal is 18, and there's 4 1-inch bricks and 5 5-inch bricks. So the program should only take 3 1-inch bricks and 3 5-inch bricks. But how do I make the program do that... hmm...
But, when I change it to == instead of >=, I just get more errors.
UPDATE:
I believe I get the concept now, but I'm just trying to figure out how to select just the right amount of 1-inch bricks and 5-inch bricks and get the goal. So, let's say the goal is 18, and there's 4 1-inch bricks and 5 5-inch bricks. So the program should only take 3 1-inch bricks and 3 5-inch bricks. But how do I make the program do that... hmm...
#4
Posted 10 March 2011 - 10:17 PM
Your first if statement is not needed.
You need to:
1) Check if there are enough inches to fill the goal.
2) Check if there are enough small bricks at the end to satisfy goal (because five inch bricks can't satisfy 13, so is there 3 small blocks left?) Hint: The modulus operator will help.
You need to:
1) Check if there are enough inches to fill the goal.
2) Check if there are enough small bricks at the end to satisfy goal (because five inch bricks can't satisfy 13, so is there 3 small blocks left?) Hint: The modulus operator will help.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#5
Posted 10 March 2011 - 10:24 PM
I just checked. Alexander's solution works.
#6
Posted 10 March 2011 - 10:57 PM
So are you saying I can use the modulus operator to see how many 5-inch bricks the program should get? Using the example I gave above, the program should find that only 3 out of the 5 available 5-inch bricks should be used, so that would be 15 inches, and there's 4 1-inch bricks, so there's enough to reach the goal. But how exactly can the modulus operator be used to find that 3?
#7
Posted 10 March 2011 - 11:02 PM
goal % 5 will essentially do this: goal / 5, then tell you the remainder. If your goal is 13, then the remainder will be 3. Do you have enough small bricks to fill 3? You would need to add that condition to your code as well.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#8
Posted 10 March 2011 - 11:19 PM
By the way, can we put an if statement inside an else-if statement?
UPDATE:
Phew, I finally got it! Here's the solution that I found:
UPDATE:
Phew, I finally got it! Here's the solution that I found:
public boolean makeBricks(int small, int big, int goal) {
if (small == goal || big * 5 == goal)
return true;
else if ((big * 5) != goal && goal % 5 <= small && (big * 5) + small >= goal)
return true;
return false;
}If there's a simpler, or easier-to-read, solution, please let me know.
#9
Posted 11 March 2011 - 12:02 AM
Your first if statement is unneeded because the second would handle if there is enough for the goal.. Your second portion is correct! Although you can combine the first and last part in to
By the way, I am hooked on the site now!
if(small + (big * 5) >= goal && goal % 5 <= small)First checking if there are enough big and small bricks, second purely checking if there are enough small ones to satisfy results that are above or below five.
By the way, I am hooked on the site now!
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#10
Posted 11 March 2011 - 06:26 AM
I've been looking for this site! I ran across it while I was in high school.
I'm glad you solved your problem.
I'm glad you solved your problem.
#11
Posted 11 March 2011 - 10:59 AM
Alexander said:
Your first if statement is unneeded because the second would handle if there is enough for the goal.. Your second portion is correct! Although you can combine the first and last part in to
By the way, I am hooked on the site now!
if(small + (big * 5) >= goal && goal % 5 <= small)First checking if there are enough big and small bricks, second purely checking if there are enough small ones to satisfy results that are above or below five.
By the way, I am hooked on the site now!
lethalwire said:
I've been looking for this site! I ran across it while I was in high school.
I'm glad you solved your problem.
I'm glad you solved your problem.
#12
Posted 11 March 2011 - 05:12 PM
wow I just noticed that all my homework assignments for my java class in college have been coming from that site! lol w t f
anyways this is my teacher's answer to that problem
anyways this is my teacher's answer to that problem
if (goal / 5 <= big)
{
if (goal % 5 <= small)
result = true;
else
result = false;
}
else
{
if (goal - big * 5 <= small)
result = true;
else
result = false;
}
}
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









