In this lab you will be writing a handful of recursive method in order to practi
ID: 3601042 • Letter: I
Question
In this lab you will be writing a handful of recursive method in order to practice recursion.
Concepts Covered
Writing recursive methods
Calling upon recursive methods
Create a new Eclipse project titled, "Lab8Recursion".
starter file and add it to the project. This file contains test code for methods you must write. The methods you must write are:
rangeSum
gcd
isPalindrome
reverseString
public class Lab8Recursion
{
/**
* Main method, alls upon tests for your recursive methods.
* @param args Not used.
*/
public static void main(String[] args)
{
testRangeSum();
testGCD();
testIsPalindrome();
}
/**
* Method for testing your answer against the expected answer. Reports if the test passed or failed.
* If failed will report the correct and your answer.
* @param answer Answer from your method call.
* @param expected Expected answer.
* @param test Name of the test.
*/
public static void testCheck(int answer, int expected, String test)
{
if (answer == expected)
{
System.out.printf("%s Passed ", test);
}
else
{
System.out.printf("%s Failed Correct Result: %d Your Result: %d ", test, expected, answer);
}
}
/**
* Tests your rangeSum method by creating two int arrays and passing them to rangeSum.
* Magic numbers used for testing purposes.
*/
public static void testRangeSum()
{
//Magic numbers used for testing
int[] arr1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] arr2 = {34, 64, 19, 30, 55};
System.out.println("***Testing rangeSum***");
int arr1Sum = rangeSum(arr1, 0);
int arr2Sum = rangeSum(arr2, 0);
//Magic numbers used for testing
testCheck(arr1Sum, 55, "rangeSum Test 1");
testCheck(arr2Sum, 202, "rangeSum Test 2");
}
/**
* Tests your gcd method.
* Magic numbers used for testing purposes.
*/
public static void testGCD()
{
System.out.println(" ***Testing gcd***");
//Magic numbers used for testing
int gcd1 = gcd(24, 60);
int gcd2 = gcd(60, 24);
int gcd3 = gcd(7, 13);
int gcd4 = gcd(25, 25);
//Magic numbers used for testing
testCheck(gcd1, 12, "gcd Test 1");
testCheck(gcd2, 12, "gcd Test 2");
testCheck(gcd3, 1, "gcd Test 3");
testCheck(gcd4, 25, "gcd Test 4");
}
/**
* Tests results of isPalindrome. Reports if the test passed or failed.
* If failed will report what the result of your reverseString method is.
* @param s String to test if it is a palindrome.
* @param palindrome If the String tested is a palindrome or not.
* @param test Name of the test.
*/
public static void palindromeTest(String s, boolean palindrome, String test)
{
if (isPalindrome(s) == palindrome)
{
System.out.printf("%s Passed ", test);
}
else
{
System.out.printf("%s Failed Correct Result: %b Your Reversed String is %s ",
test, palindrome, reverseString(s));
}
}
/**
* Runs tests your isPalindrome method.
*/
public static void testIsPalindrome()
{
System.out.println(" ***Testing isPalindrome***");
palindromeTest("racecar", true, "isPalindrome Test 1");
palindromeTest("RAcEcAr", true, "isPalindrome Test 2");
palindromeTest("selfless", false, "isPalindrome Test 3");
palindromeTest("WILLOW", false, "isPalindrome Test 4");
}
}
The rangeSum, gcd, and reverseString methods MUST be recursive in order to get credit. The isPalindrome is not recursive, but calls upon reverseString. See the documentation for instructions on how to write these methods
The test driver runs the tests for you. If any of the tests fail it will be reported in the output, reporting the expected and your own results. See expected output below.
EXPECTED OUTPUT
***Testing rangeSum***
rangeSum Test 1 Passed
rangeSum Test 2 Passed
***Testing gcd***
gcd Test 1 Passed
gcd Test 2 Passed
gcd Test 3 Passed
gcd Test 4 Passed
***Testing isPalindrome***
isPalindrome Test 1 Passed
isPalindrome Test 2 Passed
isPalindrome Test 3 Passed
isPalindrome Test 4 Passed
Explanation / Answer
public class Lab8Recursion
{
Lab8Recursion()
{
}
public static void main(String args[])
{
System.out.println(isPalindrome("Gaurav"));
}
public static int gcd(int num1,int num2)
{
while(num1!=num2)
{
if(num1>num2)
{
return gcd((num1-num2),num2);
}
else
{
return gcd(num1,(num2-num1));
}
}
return num1;
}
public static String reverseString(String s)
{
if(s.equals(""))
{
return s;
}
int l=s.length();
return s.charAt(l-1)+reverseString(s.substring(0,l-1));
}
public static boolean isPalindrome(String s)
{
if(s.equals(reverseString(s)))
{
return true;
}
else
{
return false;
}
}
public static void palindromeTest(String s,boolean plaindrome,String test)
{
if(plaindrome==isPalindrome(s))
{
System.out.println(" Passed "+test);
}
else
{
System.out.println(" Failed "+test);
}
}
public static int rangeSum(int[] values,int current)
{
if(current==(values.length-1))
{
return values[current];
}
else
{
return values[current]+rangeSum(values,current+1);
}
}
public static void testCheck(int answer,int expected,String test)
{
if(answer==expected)
{
System.out.println(" Passed "+test);
}
else
{
System.out.println(" Failed "+test);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.