# Thread: Help with logic problem

1. Member Join Date
Jan 2011
Posts
3
Rep Power
0

## 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

Java 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...
Last edited by jch02140; 01-01-2011 at 10:10 AM.  Reply With Quote

2. ##  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  Reply With Quote

3. Senior Member Join Date
Dec 2010
Posts
165
Rep Power
10

##  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.  Reply With Quote

4. Member Join Date
Jan 2011
Posts
3
Rep Power
0

## Hi Jos,

The Question mentioned each small brick is 1 inch long and each big brick is 5 inches long. And the follow method is provided.

Java Code:
`public boolean makerBricks(int small, int big, int goal)`
Last edited by jch02140; 01-01-2011 at 11:00 AM.  Reply With Quote

5. ##  Originally Posted by jch02140 Hi Joe,

The Question mentioned each small brick is 1 inch long and each big brick is 5 inches long. And the follow method is provided.

Java 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:

Java 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  Reply With Quote

6. Member Join Date
Jan 2011
Posts
3
Rep Power
0

## Thanks Jos. :D  Reply With Quote