JAVA Recursion In this lab, you will create three recursive methods along with t
ID: 3804441 • Letter: J
Question
JAVA 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 For each project, submit a class listing (or listings) 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 Project 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 Don't nod Not even close Campus motto: Bottoms up, Mac! A man, a plan, a canal Panama Project 2: Greatest Common Denominator (gcd) Create methods which use Euclid's 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 259Explanation / Answer
PALINDROME:
RECURSSION:
project 2:
project 4:
package com.java2novice.algos;
public class BinaryToDecimal {
public int getDecimalFromBinary(int binary){
int decimal = 0;
int power = 0;
while(true){
if(binary == 0){
break;
} else {
int tmp = binary%10;
decimal += tmp*Math.pow(2, power);
binary = binary/10;
power++;
}
}
return decimal;
}
public static void main(String a[]){
BinaryToDecimal bd = new BinaryToDecimal();
System.out.println("11 ===> "+bd.getDecimalFromBinary(11));
System.out.println("110 ===> "+bd.getDecimalFromBinary(110));
System.out.println("100110 ===> "+bd.getDecimalFromBinary(100110));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.