Question
This program is a Recursion example problem. This is a pretty beginner class so we havent discussed any major topics. We are using abstract methods
This program is a Recursion example problem. This is a pretty beginner class so we havent discussed any major topics. We are using abstract methods and exception handling to deal with this problem/ arrays and other basic java topics binary object files.I will include an example as reference to how much we have used. Essentially this is an excersie on Recursion
In this lab, you will create three recursive methods along with their iterative counterparts:
1. Palindromes
2. Greatest common denominator
3. Decimal-to-binary converter
4. Binary-to-decimal convertor
For each method, submit a class listing and PrintScreens showing the results of running each program using the requested test data sets.
Both the recursive and iterative methods may be included in the same class as main.
Make sure that you include Introductory Comments, and that every program is well-documented and properly formatted.
Method 1: Palindromes
A palindrome is a word, phrase, or sequence of words that reads the same backward and forward. Only letters are considered spaces, punctuation, and digits are ignored.
Examples of single-word palindromes are: madam, noon, and civic. Examples of multi-word palindromes: Step on no pets and A Toyota! Race fast safe car: a Toyota.
Test data sets: Determine if the following words and phrases are palindromes:
Chic
Kayak
Dont nod
Not even close.
Campus motto: Bottoms up, Mac!
A man, a plan, a canal -- Panama.
Method 2: Greatest Common Denominator (gcd)
Create methods which use Euclids algorithm to find the greatest common denominator (gcd) of two positive numbers. The algorithm states that the gcd of two numbers a and b is the same as the gcd of b and a % b. As you recurse / iterate through successive cases, when the case where a % b equals 0 is reached, a is the gcd of the original two values
Test data sets: Find the greatest common denominators of 13 and 11, 17 and 51, 9 and 27, 100 and 2550, 111 and 259.
Method 3: Decimal-to-binary converter
Create methods which take a non-negative whole number (in decimal as specified in your Java source code) and convert it to a String of 0s and 1s representing the same number in binary (base 2).
For example, define an int variable and initialize it to (decimal) 100. Your recursive method, when passed this variable, should return the (binary) String "1100100".
Hint: use the integer divide / and the modulo % operators.
Test data sets: Print the binary string equivalents of following (decimal) numbers: 0, 5, 32, 240, and 682
Method 4: Binary-to-decimal converter
Perform the reverse of method3, that is, create methods (recursive and iterative) which take a binary string of 0s and 1s, and return an int with its decimal equivalent.
Use the strings created in method3 as test data sets.
EXAMPLE CODE NOT REALTED TO THE QUESTION BEING ASKED
* Project: Demonstration * Problem Statement: Create a recursive method which takes a multi-digit * number, then prints those digits down the screen * to 1. * * Algorithm: * 1. Create a main() program that prompts the user for a multi-digit * number (or 0 to exit) * 2. Invoke a recursive method named writeVertical() with the number entered * in step 1 above * 3. In writeVertical: * 3a. Take an int argument * 3b. If the argument is greater than 10, then invoke writeVertical again * using all of the digits except the last one (integer divide by 10) * 3c. Print the last digit of the argument (use modulo 10) */ import java.util.Scanner ; public class VerticalNumbers { public static void main(String[] args) { // Prompt the user for a non-negative number (or 0 to end) Scanner keyboard = new Scanner(System.in) ; int startingNumber = 0 ; while (true) { System.out.print(" Enter a multi-digit number (or \"0\" to exit): ") ; // Can we parse it? try { startingNumber = Integer.parseInt(keyboard.nextLine()) ; } catch (NumberFormatException e) { System.out.print("That is not a valid number -- try again... ") ; continue ; } // Do we stop? if (startingNumber < 1) { break ; } // Start the recursion demo System.out.println("Here are the digits vertically") ; writeVertical(startingNumber) ; } } /*************************************************************************** * writeVertical takes a non-negative argument. Print the last digit of the * number (using %10). If the argument is greater or equal to 10, then * invoke again using all digits except the last one. ***************************************************************************/ private static void writeVertical(int argument) { // If we're not at the end case, then start the recursion process if (argument >= 10) { writeVertical(argument / 10) ; } // Print the last digit of the argument (use the modulo operator) System.out.println(argument % 10) ; } }
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