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
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
Class Lab8Recursion java.lang.Object Lab8Recursion public class Lab8Recursion extends java.lang.Object Class for practicing simple recursive methods. Test driver provided. You must implement the methods rangeSum, gcd, isPalindrome, and reverseString Constructor Summary Constructors Constructor and Description Lab8Recursion) Method Summary All Methods Static Methods Modifier and Type static int Concrete Methods Method and Description gcd (int numl, int num2) Recursive method that gives the Greatest Common Divisor of two numbers static boolean isPalindrome ( java.lang.String s) Determines if a given String is a palindrome (same forwards and backwards) static void main (java.lang.String[] args) Main method, alls upon tests for your recursive methods static void palindromeTest (java.lang.String s, boolean palindrome, java.lang.String test) Tests results of isPalindrome
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started