Use the following program to do the above questions: // creating more recursive
ID: 3734667 • Letter: U
Question
Use the following program to do the above questions:
// creating more recursive methods for Lab 7
import java.util.Scanner;
public class Lab7Recursions {
public static void main(String args[]) {
int num1, num2;
String wrd1, wrd2;
Scanner scan = new Scanner(System.in);
System.out.print("Enter an integer for testing summationSquare method: ");
num1 = scan.nextInt();
System.out.println("summationSquare(" + num1+") is equal to "+ summationSquare(num1));
System.out.print("Enter an integer for testing the toBinary method: ");
num1 = scan.nextInt();
System.out.print(num1+"(10) is equal to ");
toBinary(num1);
System.out.println("(2)");
System.out.print("Enter an integer for testing ReverseNum method: ");
num1 = scan.nextInt();
System.out.println("reverseNum("+num1+") is equal to "+ reverseNum(num1));
System.out.print("Enter two words for testing equal method:");
wrd1 = scan.next();
wrd2 = scan.next();
System.out.println("equal("" +wrd1 +"",""+wrd2+"") is " + equal(wrd1,wrd2));
} //method main
//other class methods
public static long summationSquare (int n){
// summation from 1^2 to n^2
}//method summationSquare
public static void toBinary (int n){
//print n as a binary stream
}// method toBinary
private static long reverse(long n, long tempresult) {
//compute and return the reverse form of n using tempresult
}
public static long reverseNum (long n){
return reverse (n, 0);
} // method reverseNum
private static boolean isEqual (String word1, String word2, int index){
//compare character by character between word1 and word2
}
public static boolean equal (String word1, String word2){
return isEqual (word1, word2, 0);
}
}//class Recursion
3. Write a few recursive methods to compute some specific values or perform some specific tasks as specified below: i. I 15%) Compute the sum from 12 to n^2 , where n is an input integer. For examples: n-10, summationSquare(10) is equal to 385 n-20, summationSquare(20) is equal to 2870 115%) Print out the binary stream of the value n, where n is an input integer For examples: n-123, 123(10) is equal to 1111011(2) n -234, -234(10 is equal to -1101010(2) i.Explanation / Answer
import java.util.Scanner; public class Lab7Recursions { public static void main(String args[]) { int num1, num2; String wrd1, wrd2; Scanner scan = new Scanner(System.in); System.out.print("Enter an integer for testing summationSquare method: "); num1 = scan.nextInt(); System.out.println("summationSquare(" + num1+") is equal to "+ summationSquare(num1)); System.out.print("Enter an integer for testing the toBinary method: "); num1 = scan.nextInt(); if(num1 < 0) { num1 = -num1; System.out.print("-" + num1 +"(10) is equal to -"); } else System.out.print(num1+"(10) is equal to "); toBinary(num1); System.out.println("(2)"); System.out.print("Enter an integer for testing ReverseNum method: "); num1 = scan.nextInt(); System.out.println("reverseNum("+num1+") is equal to "+ reverseNum(num1)); System.out.print("Enter two words for testing equal method:"); wrd1 = scan.next(); wrd2 = scan.next(); System.out.println("equal("" +wrd1 +"",""+wrd2+"") is " + equal(wrd1,wrd2)); } //method main //other class methods public static long summationSquare (int n){ // summation from 1^2 to n^2 if (n == 1) return n; else return n*n + summationSquare(n-1); }//method summationSquare public static void toBinary (int n){ //print n as a binary stream int temp; if(n > 0) { temp = n % 2; toBinary(n / 2); System.out.print(temp + ""); } }// method toBinary private static long reverse(long n, long tempresult) { //compute and return the reverse form of n using tempresult if(n == 0) { return tempresult; } else { tempresult = (n % 10) + tempresult * 10; return reverse(n/10, tempresult); } } public static long reverseNum (long n){ return reverse (n, 0); } // method reverseNum private static boolean isEqual (String word1, String word2, int index){ //compare character by character between word1 and word2 if((word1.length() == index && word2.length() == index)) return true; else if(word1.charAt(0) == word2.charAt(0)) isEqual(word1.substring(0),word2.substring(0),index+1); else return false; return true; } public static boolean equal (String word1, String word2){ return isEqual (word1, word2, 0); } }//class RecursionRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.