java - How to implement Recursion -
hello trying implement recursion in 1 of assignments.
these classes.
public class bear implements totempole { int bearcount; public bear(totempole rest){} public bear() { bearcount = 3; } public int power() { return + 5; } public int height(){ return bearcount + 5; } public boolean chiefpole(int bearcount){ if(this.bearcount >= bearcount){ return true; } else { return false; } } }
// snake class
public class snake implements totempole { public snake(totempole rest){} int bearcount; public snake() { bearcount = 0; } public int power() { return + 3; } public int height(){ return bearcount + 1; } public boolean chiefpole(int bearcount){ if(this.bearcount >= bearcount){ return true; } else { return false; } } }
// eagle class
public class eagle implements totempole { int bearcount; public eagle(){ bearcount = 0; } public int power() { return + 2; } public int height(){ return bearcount + 1; } public boolean chiefpole(int bearcount){ if(this.bearcount >= bearcount){ return true; } else { return false; } } }
basically trying figure out how recursion works power() method. tester expecting value of 26. however, code not working. new java appreciated.
//tester
p1 = new bear( new bear( new snake( new snake( new snake( new bear( new eagle()))))));
base on comments, think need add totempole properties each class. in power() method, need calculate it. example in class bear can add:
class bear implements totempole { int bearcount; totempole totem; public bear(totempole totem){ bearcount = 3; this.totem = totem; } public int power() { int result = 0; if(totem == null) { result = 5; } else { result = totem.power() + 5; } return result; } public bear() { bearcount = 3; } public int height(){ return bearcount + 5; } public boolean chiefpole(int bearcount){ if(this.bearcount >= bearcount){ return true; } else { return false; } } }
same other classes. hope help.
Comments
Post a Comment