# Help with logic problem

• 01-01-2011, 11:06 AM
jch02140
Help with logic problem
Hi, I have a question involving me to write a program asking user to input the number of small and big bricks, as well as the intended result of the brick wall's length. The program will then return true or false if the wall length is possible

For example, if user input 3 small bricks and 2 big bricks, the following results input by user should return true. Otherwise, it will return false.

Results that return true: 1,2,3,5,6,7,8,10,11,12,13

Here is the code

Code:

```package program1; import java.util.*; import java.io.*; import java.text.*; import java.lang.*; public class Program1 {     public int small, big, goal;     public boolean result;     /**     * @param args the command line arguments     */     public int getSmall() {         return this.small;     }     public void setSmall(int smallBricks) {         small = smallBricks;     }     public int getBig() {         return this.big;     }     public void setBig(int bigBricks) {         big = bigBricks;     }     public int getGoal() {         return this.goal;     }     public void setGoal(int goalBricks) {         goal = goalBricks;     }     public boolean getResult() {         return this.result;     }     public void setResult(boolean resultCheck) {         result = resultCheck;     }     public static void main(String[] args) {         Program1 program = new Program1();         Scanner keyboard;         keyboard = new Scanner(System.in);         System.out.println("Please input number of small bricks:");         program.setSmall(keyboard.nextInt());         System.out.println("Please input number of big bricks:");         program.setBig(keyboard.nextInt());         System.out.println("Please input number of goal bricks:");         program.setGoal(keyboard.nextInt());         program.makeBricks(program.getSmall(), program.getBig(), program.getGoal());         System.out.println("makeBricks(" + program.getSmall() + ", " + program.getBig() + ", "                                       + program.getGoal() +") ? " + program.getResult());           }     public void makeBricks(int small, int big, int goal) {         int newSmall = small * 1;         int newBig = big * 5;         if ((goal <= newSmall) || (goal % newBig == 0) || (goal % (newSmall + newBig) == 0))  {             setResult(true);         }         else {             setResult(false);         }     } }```
The above logic is somewhat wrong or missing something. For example, if I input 3 for small bricks, 5 for big bricks and the results as 23, the program returns false but it should be true...

I am not sure what is wrong here...
• 01-01-2011, 11:13 AM
JosAH
Quote:

Originally Posted by jch02140
The above logic is somewhat wrong or missing something. For example, if I input 3 for small bricks, 5 for big bricks and the results as 23, the program return false but it should be true...

I am not sure what is wrong here...

Shouldn't you also suply the size of the small and big bricks? So you end up with five numbers:

sb) number of small bricks
sl) size of a single small brick
bb) number of big bricks
bl) size of a single big brick
tl) goal, i.e. total length.

So the question becomes, find values for x <= sb and y <= bb such that x*sl+y*bl == tl. Is this interpretation correct?

kind regards,

Jos
• 01-01-2011, 11:37 AM
JavaHater
Quote:

Originally Posted by jch02140
The above logic is somewhat wrong or missing something. For example, if I input 3 for small bricks, 5 for big bricks and the results as 23, the program returns false but it should be true...

I am not sure what is wrong here...

when you give 3 , 5 and 23, newSmall = 3 , newBig = 25 and goal = 23.

goal <= newSmall is false, because 23 cannot be less than 3.

(goal % newBig == 0) is false as well, because 23 divide by 5 has remainders. So cannot be equal 0.

(goal % (newSmall + newBig) == 0) is false as well, because 23 divide by 28 cannot have no remainders.

That's why all of them are false... check your logic again.
• 01-01-2011, 11:41 AM
jch02140
Hi Jos,

Thank you for your reply. Sorry about that. I left out those info.
The Question mentioned each small brick is 1 inch long and each big brick is 5 inches long. And the follow method is provided.

Code:

`public boolean makerBricks(int small, int big, int goal)`
• 01-01-2011, 11:55 AM
JosAH
Quote:

Originally Posted by jch02140
Hi Joe,

Thank you for your reply. Sorry about that. I left out those info.
The Question mentioned each small brick is 1 inch long and each big brick is 5 inches long. And the follow method is provided.

Code:

`public boolean makerBricks(int small, int big, int goal)`

Ah, so the length of those bricks is fixed; your problem is easy to solve then:

Code:

```for (int i= nof_small_bricks; i >= 0; i--)   if ((total-i*1)%5 == 0)       if ((total-i*1)/5 <= number_of_large_bricks)         // use i small bricks and (total-i*1)/5 large bricks```
kind regards,

Jos
• 01-01-2011, 12:39 PM
jch02140
Thanks Jos. :D