Results 1 to 6 of 6
Thread: Help with logic problem
- 01-01-2011, 11:06 AM #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); } } }
I am not sure what is wrong here...Last edited by jch02140; 01-01-2011 at 11:10 AM.
- 01-01-2011, 11:13 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
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,
JosBuild a wall around Donald Trump; I'll pay for it.
- 01-01-2011, 11:37 AM #3
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 11
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 #4
Member
- Join Date
- Jan 2011
- Posts
- 3
- Rep Power
- 0
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.
Java Code:public boolean makerBricks(int small, int big, int goal)
Last edited by jch02140; 01-01-2011 at 12:00 PM.
- 01-01-2011, 11:55 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
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
JosBuild a wall around Donald Trump; I'll pay for it.
- 01-01-2011, 12:39 PM #6
Member
- Join Date
- Jan 2011
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Inheritance logic problem
By Gloomy in forum New To JavaReplies: 3Last Post: 08-28-2010, 09:23 AM -
Logic not working
By Prajin in forum AWT / SwingReplies: 1Last Post: 07-19-2010, 08:54 PM -
need a logic for this
By rajivjoshi in forum New To JavaReplies: 4Last Post: 06-12-2010, 03:18 PM -
problem with <logic:iterate> tag looping
By tsaswathy in forum Web FrameworksReplies: 0Last Post: 09-27-2008, 01:13 PM -
Cant get the logic right
By jermaindefoe in forum New To JavaReplies: 4Last Post: 03-11-2008, 01:22 AM
Bookmarks