Urgent Help Java Recursive Function Please
Hi,
I am a first year student studying Computing for Business. I have some coursework set with three questions. I am currently stuck on the last question.7
I AM USING BLUEJ
My task is:
A builder wishes to build a (three-dimensional) pyramid. Each layer is
comprised of square blocks. The top layer comprises a single block, the next
layer comprises four blocks (2 x 2 blocks). The next expands so that each side
is 3 blocks long. The central block is not inserted so the layer comprises 8
blocks. Each additional layer adds another block to the edge and only lays the
outer blocks.
1. Write a series of tests in Q3Test that would ensure a function calculated
the correct number of blocks required based on the above specification.
Document (using comments) the test cases that you are going to use.
2. Write a recursive function that will perform the calculations and run your
tests against the method.
I have written the tests for example.
@Test
public void pyramidTest() {
assertEquals(0, pyramidLayer(0), 0);
assertEquals(1, pyramidLayer(1), 0);
assertEquals(5, pyramidLayer(2), 0);
assertEquals(13, pyramidLayer(3), 0);
assertEquals(25, pyramidLayer(4), 0);
I am stuck on writing the recursive function... I have figured that pyramid with layers 0 and 1 are special case therefore the following does not apply; although i have found out the following formula 4(nlayer-1) = number of blocks on a particluar layer. (nlayer being the number of layer)
currently this is the function that i have written.
public class Q3Test
{
public int pyramid(int nlayer) {
if(nlayer<=0) return 0;
if(nlayer ==1) return 1;
could anyone be kind enough to point me in the right direction of what to do please??
Thanks
Re: Urgent Help Java Recursive Function Please
Hi i am a first year student studying computing for business. I am attempting a coursework but am stuck on one last question.. the question is abotu writing a recursive function... this is the question:
A builder wishes to build a (three-dimensional) pyramid. Each layer is
comprised of square blocks. The top layer comprises a single block, the next
layer comprises four blocks (2 x 2 blocks). The next expands so that each side
is 3 blocks long. The central block is not inserted so the layer comprises 8
blocks. Each additional layer adds another block to the edge and only lays the
outer blocks.
1. Write a series of tests in Q3Test that would ensure a function calculated
the correct number of blocks required based on the above specification.
Document (using comments) the test cases that you are going to use.
2. Write a recursive function that will perform the calculations and run your
tests against the method.
I have wrote the tests for part 1 as follows:
@Test
public void pyramidTest() {
assertEquals(0, pyramidLayer(0), 0);
assertEquals(1, pyramidLayer(1), 0);
assertEquals(5, pyramidLayer(2), 0);
assertEquals(13, pyramidLayer(3), 0);
assertEquals(25, pyramidLayer(4), 0);
this works out the total number of blocks needed for a pyramid of layers; 0 1 2 3 4, e.g. a pyramid with 4 layers consists of 25 blocks total..
i am stuck on part 2, i have realised that the formula for working the blocks in any ONE layer is 4(nlayer-1) where n = no. of layers. e.g. a pyramid with 4 layers would be 4*4 -4 = 12 blocks. also i have realised that pyramid with bases 0 and 1 are special case... so far i have written the following function.. can anyone help me please..
{
public int pyramid(int nlayer) {
if(nlayer<=0) return 0;
if(nlayer ==1) return 1;
Re: Urgent Help Java Recursive Function Please
After a bit of doodling on a piece of paper, I found that the number of bricks on layer i+1 is four more bricks than the number of bricks on layer i. The exception to the rule is the top layer: it has one brick instead of zero bricks. So a simple interval definition does the job: L(0) == 1, L(1) == 4, L(i+1) == L(i)+4
kind regards,
Jos
ps. I closed your other (duplicate) thread.
Re: Urgent Help Java Recursive Function Please
thanks for the quick reply.. how would you represent that in a function as a recursive.. sorry mate im new to all this java stuff really have no idea..
Re: Urgent Help Java Recursive Function Please
Quote:
Originally Posted by
khehrap
thanks for the quick reply.. how would you represent that in a function as a recursive.. sorry mate im new to all this java stuff really have no idea..
Something like this'll do (you were on the right track):
Code:
int bricks(int layer) {
if (layer < 0) return -1; // no layer above the top exists
if (layer == 0) return 1; // by definition
if (layer == 1) return 4; // also by definition
return bricks(layer-1)+4;
}
kind regards,
Jos
Re: Urgent Help Java Recursive Function Please
Ok i think i understand what you have done. I think that gives me the total number of bricks for each layer.. but i need them as a collective... for example
Layer 4 would be layer3+layer2+layer1
Re: Urgent Help Java Recursive Function Please
Quote:
Originally Posted by
khehrap
Ok i think i understand what you have done. I think that gives me the total number of bricks for each layer.. but i need them as a collective... for example
Layer 4 would be layer3+layer2+layer1
You mean something like this?
Code:
int pyramid(int layer) {
if (layer < 0) return -1; // no layer above the top exists
if (layer == 0) return 1; // by definition
return pyramid(i-1)+bricks(i);
}
kind regards,
Jos
Re: Urgent Help Java Recursive Function Please
Thanks i know what you mean:
i have written this code but i am getting fails for my test.. eg. when nlayer = 2 i am returned 3 when it should be 5 (layer 1 = 1 layer 2 = 4 both added = 5)
i know my return (nlayer + nlayer-1) is wrong but dont know what to change it to lol
this is my code:
public class Q3Test
{
public int pyramid(int nlayer) {
if(nlayer<=0) return 0;
if(nlayer ==1) return 1;
if(nlayer ==2) return 4;
return (nlayer + nlayer-1);
}
@Test
public void pyramidTest() {
assertEquals(0, pyramid(0), 0);
assertEquals(1, pyramid(1), 0);
assertEquals(5, pyramid(2), 0);
assertEquals(13, pyramid(3), 0);
assertEquals(25, pyramid(4), 0);
}
}
Re: Urgent Help Java Recursive Function Please
Of course that is wrong; this is what we have so far (B(n) == number of bricks in layer n, P(n) == number of bricks in a pyramid with n layers)
B(0) == 1; B(1) == 4; B(n) == B(n-1)+4
P(0) == 1; P(1) == 5; P(n) == P(n-1)+B(n)
we can deduce that B(n) == P(n-1)-P(n-2)+4; combining the last two equations:
P(n) == P(n-1)+P(n-1)-P(n-2)+4 == 2*P(n-1)-P(n-2)+4
So in code, we have:
Code:
int pyramid(int level) {
if (level < 0) return -1;
if (level == 0) return 1;
if (level == 1) return 5;
return 2*pyramid(level-1)-pyramid(level-2)+4;
kind regards,
Jos
Re: Urgent Help Java Recursive Function Please
i understand your equation and it works correctly in my head! but when i put it into blue j it gives it wrong. eg. if i try working out nlayer 3. it should to nlayer 2 (which is 5)*2 = 10 - nlayer 1 (which is 1) + 4 which is 13! CORRECT WOOHOO!!
but when i put it into bluej i put this equation
return (2*nlayer-1 - nlayer-2 +4);
it gives me 4 as IT USES THE VALUE OF THE NLAYER SO FOR EXAMPLE.. FOR NLAYER 2 IT USES 2 INSTEAD OF 5... what am i doing wrong!!!??
Re: Urgent Help Java Recursive Function Please
Did you use my code, or did you use your own? If you used your own, show it because I can't say anything about it without seeing it; it most definitely isn't BlueJ's fault.
kind regards,
Jos
Re: Urgent Help Java Recursive Function Please
this is the code that i have used mate :
public int pyramid(int nlayer) {
if(nlayer<=0) return 0;
if(nlayer ==1) return 1;
if(nlayer ==2) return 5;
return (2*nlayer-1) - (nlayer-2) +4;
this is the testing i have used :
@Test
public void pyramidTest() {
assertEquals(0, pyramid(0), 0);
assertEquals(1, pyramid(1), 0);
assertEquals(5, pyramid(2), 0);
assertEquals(13, pyramid(3), 0);
assertEquals(25, pyramid(4), 0);
}
Thanks alot for your help mate been about 2 hours since you been helping lol.. im going mental once i start something i want to finish it :)
Re: Urgent Help Java Recursive Function Please
You should do your own course work, you are cheating!! And your university will find out about that.
Re: Urgent Help Java Recursive Function Please
How am i cheating im just asking for a pointer into the right direction
Re: Urgent Help Java Recursive Function Please
i have emailed my lecturer and asked if it is cheating and am waiting for a reply... and if you look at the top i have used my own code not the other guys... so to be honest dont know what the problem is mate
Re: Urgent Help Java Recursive Function Please
if this is actually cheating.. i am sorry could the moderator please delete the post.. thanks for the help i have recieved till now..
Re: Urgent Help Java Recursive Function Please
Dude I'm just giving you a heads up!I'm sure you will find all about plagiarism in your student handbook.
Re: Urgent Help Java Recursive Function Please
Yeah i understand what u mean mate... but i thought if i dont use the code provided it wont be... hopefully ill be in the clear
COULD MOD DELETE POST PLEASE
really dont want to risk anything lol
cheers dude.
Re: Urgent Help Java Recursive Function Please
I am not going to delete this thread and I don't think you were trying to cheat. It is almost impossible not to supply at least a substantial amount of the code when discussing recursive functions or recurrent equality formulas.
kind regards,
Jos
Re: Urgent Help Java Recursive Function Please
Hey Jos
I finally completed it this morning mate :D Thanks alot for your help yesterday. really it was very helpful :) Ill be coming back again if any more problems :) and hope one day ill be helping people like you did on the forum :D (obviously its not in my skillset yet).
Thanks
Prab