We want to make a row of bricks that is X meter long. We have a number of small bricks (Y meter each) and big bricks (Z meter each). Return true if it is possible to make the X meter long row by using a combination of the two kinds of brick. Remember, we have to use both kinds of bricks. We basically need you to create a method which takes 3 integer inputs - X, Y, Z and return a boolean as the result.

You just copy/pasted your assignment without even a moment taken to explain what help you need. That's highly disrepestectful to the many people who give their time to help others here.

There are lots of people here who will freely give their time to help you become the best Java programmer you can be. There's nobody here who is interested in helping you cheat or doing your homework for you.

DaniWeb Member Rules (which you agreed to when you signed up) include:
"Do provide evidence of having done some work yourself if posting questions from school or work assignments"
http://www.daniweb.com/community/rules

Post what you have done so far and someone will help you from there.

apologies...:)

is the following solution correct? actually i have to give the optimal solution, can any other solution be better.?

assuming that either can be big or small
x= total length of row.
y= length of one brick.
z= length of other brick.

Inline Code Example Here
boolean Bricks(long x,long y,long z)

boolean Bricks(long x,long y,long z)
{
long bigger,smaller;
if(y>z)
{
bigger=y;
smaller=z;
}
else 
{
bigger=z;
smaller=y;
}
remainder= x%bigger;
if(remainder%smaller==0)
return true;
else
return false;

is the following solution correct?

You can test that for yourself by trying a few small test cases.

However, I think you'll find it's not too good - you may need multiple bricks and nowhere in your code can I see anything to handle that.
The solution may require up to y/x of the first bricks and x/z of the other bricks, or any combination of those (it doesn't matter which is larger or smaller, although you could use that info to optimise the algorithm once you have it working). That immediatly suggests two loops (or equivalent) counting from 1..x/y and 1..x/z respectively.

There's no point trying to write code if you haven't first got a solid grasp of the algorithm you are trying to implement. The best thing toi do now is to set the Java aside, get a pen and some paper, and work out a few test cases by hand. That will help you see exactly what the process is, and therefore what you have to code.

ps. The spec syas x,y,z are integers, and you defined them as long. While that is valid, you may ask where will you find a wall longer than 2 billion meters? An int will be big enough methinks.

Edited 3 Years Ago by JamesCherrill

Thanks, On giving the input as some size y and z, we dont need the number of bricks required of each size,we just need to find if we can cover the size x or not by using both kinds of bricks in any proportion.
i cant understand why we need loop from 1....x/y or 1.......x/z?

Yes, you were not asked for the number of bricks, but you will have to deal with that inside your logic. Suppose we write the number of y bricks and z bricks as a pair of numbers. Then you will need to check lots of possibilities like (1,1), (1,2), (1.3) ... (1, x/z) ... (2,1), (2,2) ... ... (x/y, x/z). It will take two nested loops to run through all those combinations to see if any of them fits.

(That's before optimising the algorithm - you can improve it later to skip some combinations that can't possibly be right. But get it working first.)

Edited 3 Years Ago by JamesCherrill

This article has been dead for over six months. Start a new discussion instead.