// From: Matthew Gorle // Date: Wed, 23 Oct 2002 23:07:37 +0100 /* Solutions to CIS-212 Lab 3 23-10-02, Matthew Gorle Notes: o All programming problems are solved recursively and iteratively. o All solutions (except recursive fibonacci) by MG. o The "Recompiles" field in comments refers to the number of logical errors that needed to be fixed. On average, one error is fixed per recompile. Your Mileage May Vary!! o The solution used in the iterativeFibonacci method is, I think, too complicated. If you want to punish yourself, see if you can find another way of doing it. What?! No comments??! That's just how I write my code. In my opinion, descriptive method/variable names are equally, if not more, important. --MG (22:49. 23-10-02) */ class Lab3 { int recursiveFactorial(int x) /* Time spent on solution : <1 minute Recompiles : 0 Unsolved Issues : 0 Gave up after : N/A */ { if (x>1) return x*recursiveFactorial(x-1); else return 1; } int iterativeFactorial(int x) /* Time spent on solution : <1 minute Recompiles : 0 Unsolved Issues : 0 Gave up after : N/A */ { int y=1; for(int i=1;i<=x;i++) y*=i; return y; } String recursiveReverse(String x) /* Time spent on solution : 3 minutes Recompiles : 1 Unsolved Issues : 0 Gave up after : N/A */ { if (x.length()>0) return (recursiveReverse(x.substring(1))+x.charAt(0)); else return ""; } String iterativeReverse(String x) /* Time spent on solution : 1 minute Recompiles : 0 Unsolved Issues : 0 Gave up after : N/A */ { String y=""; for (int i=x.length();i>0;i--) { y+=""+x.charAt(i-1); } return y; } int recursiveFibonacci(int x) /* Time spent on solution : N/A Recompiles : 3 Unsolved Issues : 1 Gave up after : 15 minutes (looked at Ida's solution) */ { if (x>2) return recursiveFibonacci(x-2)+recursiveFibonacci(x-1); else return 1; } int iterativeFibonacci(int x) /* Time spent on solution : 6 minutes Recompiles : 4 Unsolved Issues : 0 Gave up after : N/A */ { int y=1; int z=1; int a=y; for(int i=2;i