Results 1 to 20 of 20
 11152011, 09:14 PM #1Member
 Join Date
 Nov 2011
 Posts
 14
 Rep Power
 0
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 (threedimensional) 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(nlayer1) = 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??
ThanksLast edited by khehrap; 11152011 at 09:23 PM.
 11152011, 09:30 PM #2Member
 Join Date
 Nov 2011
 Posts
 14
 Rep Power
 0
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 (threedimensional) 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(nlayer1) 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;
 11152011, 09:47 PM #3
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,068
 Blog Entries
 7
 Rep Power
 24
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.Last edited by JosAH; 11152011 at 09:51 PM.
The only person who got everything done by Friday was Robinson Crusoe.
 11152011, 09:51 PM #4Member
 Join Date
 Nov 2011
 Posts
 14
 Rep Power
 0
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..
 11152011, 09:55 PM #5
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,068
 Blog Entries
 7
 Rep Power
 24
Re: Urgent Help Java Recursive Function Please
Something like this'll do (you were on the right track):
Java 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(layer1)+4; }
JosThe only person who got everything done by Friday was Robinson Crusoe.
 11152011, 09:57 PM #6Member
 Join Date
 Nov 2011
 Posts
 14
 Rep Power
 0
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
 11152011, 10:08 PM #7
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,068
 Blog Entries
 7
 Rep Power
 24
Re: Urgent Help Java Recursive Function Please
The only person who got everything done by Friday was Robinson Crusoe.
 11152011, 10:15 PM #8Member
 Join Date
 Nov 2011
 Posts
 14
 Rep Power
 0
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 + nlayer1) 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 + nlayer1);
}
@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);
}
}
 11152011, 10:46 PM #9
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,068
 Blog Entries
 7
 Rep Power
 24
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(n1)+4
P(0) == 1; P(1) == 5; P(n) == P(n1)+B(n)
we can deduce that B(n) == P(n1)P(n2)+4; combining the last two equations:
P(n) == P(n1)+P(n1)P(n2)+4 == 2*P(n1)P(n2)+4
So in code, we have:
Java Code:int pyramid(int level) { if (level < 0) return 1; if (level == 0) return 1; if (level == 1) return 5; return 2*pyramid(level1)pyramid(level2)+4;
JosThe only person who got everything done by Friday was Robinson Crusoe.
 11152011, 11:04 PM #10Member
 Join Date
 Nov 2011
 Posts
 14
 Rep Power
 0
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*nlayer1  nlayer2 +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!!!??
 11152011, 11:16 PM #11
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,068
 Blog Entries
 7
 Rep Power
 24
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,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 11152011, 11:30 PM #12Member
 Join Date
 Nov 2011
 Posts
 14
 Rep Power
 0
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*nlayer1)  (nlayer2) +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 :)
 11162011, 12:45 AM #13Member
 Join Date
 Nov 2011
 Posts
 2
 Rep Power
 0
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.
 11162011, 12:58 AM #14Member
 Join Date
 Nov 2011
 Posts
 14
 Rep Power
 0
Re: Urgent Help Java Recursive Function Please
How am i cheating im just asking for a pointer into the right direction
 11162011, 12:59 AM #15Member
 Join Date
 Nov 2011
 Posts
 14
 Rep Power
 0
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
 11162011, 01:01 AM #16Member
 Join Date
 Nov 2011
 Posts
 14
 Rep Power
 0
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..
 11162011, 01:02 AM #17Member
 Join Date
 Nov 2011
 Posts
 2
 Rep Power
 0
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.
 11162011, 01:06 AM #18Member
 Join Date
 Nov 2011
 Posts
 14
 Rep Power
 0
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.
 11162011, 09:39 AM #19
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,068
 Blog Entries
 7
 Rep Power
 24
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,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 11162011, 01:17 PM #20Member
 Join Date
 Nov 2011
 Posts
 14
 Rep Power
 0
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
Similar Threads

Urgent! How to display this function,use symbol"*" to draw line
By jaxber in forum New To JavaReplies: 3Last Post: 04292011, 01:08 AM 
Time a recursive function
By überfuzz in forum New To JavaReplies: 1Last Post: 03252011, 09:27 AM 
recursive function
By jayden in forum New To JavaReplies: 11Last Post: 09022010, 03:00 PM 
Understanding this recursive function
By LifeWithJava in forum New To JavaReplies: 3Last Post: 12302008, 06:26 AM 
Help with recursive function in java
By cachi in forum Advanced JavaReplies: 2Last Post: 07312007, 06:51 PM
Bookmarks