Question

In: Computer Science

How to print these methods in a separate main class? need the output to look like...

How to print these methods in a separate main class?

need the output to look like this:

Enter three integers whose GCD is to be found ->

Enter an integer n to find the nth Fibonacci number ->

Enter the base and exponent , an integer, of a power ->

Enter two positive integers 1 and j where i < j ->

gcd() =

fib() =

(a number ^ a number) = a number

There are ___ palindromic numbers between (a number) and (a number)

package recursiveauxiliarymath;


public class RecursiveAuxiliaryMath {

  
  

public static boolean recIsPalindrome(String num, int i, int j){
if(i>=j){
return true;
}
else{
if(num.charAt(i)!=num.charAt(j)){
return false;
}
else{
recIsPalindrome(num,i+1,j-1);
}
}
return true;
}
  
public static long recFibonacci(int n)

{
if (n<=2)
return 1;
else
{
long fib;
  
fib = recFibonacci(n - 1) + recFibonacci (n - 2);
return fib;
  
  
}
  
  
}
public static int recGCD(int a, int b)
  
{
if(b!=0){
return recGCD(b,b%a);
}
else{
return a;
}

  
  

}
  
  
  
  
public static double recPowInt (double a, int n)
  
{
if(n==1){
return a;
}
else{
return recPowInt(a*a,n-1);

}
}
}

Solutions

Expert Solution

Hi, Please find my implementation.

Please let me know in case of any issue.

import java.util.Scanner;

public class RecursiveAuxiliaryMath {

  

   public static boolean recIsPalindrome(String num, int i, int j){

      

       if( i > j)

           return false;

       if( i == j) // one digit

           return true;

      

       if(num.charAt(i) == num.charAt(j)){

           if(i+1 == j) // only two digits

               return true;

          

           return recIsPalindrome(num, i+1, j-1);

       }else{

           return false;

       }

   }

  

   public static long recFibonacci(int n){

       if(n <= 2)

           return 1;

      

       else{

           long fib;

           fib = recFibonacci(n-1) + recFibonacci(n-2);

           return fib;

       }

   }

  

   public static int recGCD(int a, int b){

       if (a == 0)

      return b;

      return recGCD(b%a, a);

   }

  

   public static double recPowInt(double a, int n){

      

       double temp;

      if( n == 0)

   return 1;

      temp = recPowInt(a, n/2);

      if (n%2 == 0)

      return temp*temp;

      else

      {

      if(n > 0)

      return a*temp*temp;

      else

      return (temp*temp)/a;

      }

   }

  

   public static void main(String[] args) {

      

       Scanner sc= new Scanner(System.in);

      

       System.out.print("Enter three digits whose GCD is to be found -> ");

       int a1 = sc.nextInt();

       int a2 = sc.nextInt();

       int a3 = sc.nextInt();

      

       int gcd = Math.abs(recGCD(a1, a2));

       gcd = Math.abs(recGCD(gcd, a3));

      

       System.out.print("Enter an integer n to ind the nth Fibonacci number -> ");

       int a4 = sc.nextInt();

      

       long fib = recFibonacci(a4);

      

       System.out.print("Enter the base and exponenet, an integer, f a power -> ");

       double a5 = sc.nextDouble();

       int a6 = sc.nextInt();

      

       double pow = recPowInt(a5, a6);

      

       System.out.print("Enter two positive numbers i and jhere i < j -> ");

       int a7 = sc.nextInt();

       int a8 = sc.nextInt();

      

       int count = 0;

       for(int k = a7; k<=a8; k++){

           String num = Integer.toString(k);

           if(recIsPalindrome(num, 0, num.length()-1)){

               count++;

           }

       }

      

      

       System.out.println();

      

       System.out.println("gcd("+a1+", "+a2+", "+a3+") = "+gcd);

       System.out.println("fib("+a4+") = "+fib);

       System.out.println(a5+"^"+a6+" = "+pow);

       System.out.println("There are "+count+" palindrome numbers beween "+a7+" and "+a8);

   }

  

}

/*

Sample run:

Enter three digits whose GCD is to be found -> 120 90 -75

Enter an integer n to ind the nth Fibonacci number -> 30

Enter the base and exponenet, an integer, f a power -> -4.5 -3

Enter two positive numbers i and jhere i < j -> 1 1000

gcd(120, 90, -75) = 15

fib(30) = 832040

-4.5^-3 = -0.010973936899862825

There are 108 palindrome numbers beween 1 and 1000

*/


Related Solutions

Just Need to see how the dataset and the output will Look in SPSS for I...
Just Need to see how the dataset and the output will Look in SPSS for I can check my answer. Please use SPSS... I need the answer ASAP.. Thanks. Only SPSS Chi-Square Test of Independence satisfied Freshmen Sophomore Junior Senior Yes 21 19 10 20 No 14 9 17 11
What would the pseudocode look like for this UML class diagram? Class - oranges: String -...
What would the pseudocode look like for this UML class diagram? Class - oranges: String - bananas: String - grapes: String - apples: String + Class(String bananas, String grapes, String apples) + oranges( ): void + isValidGrapes( ): boolean + isValidApples( ): boolean + getOranges( ): String + getBananas( ): String + setBananas(String bananas): void + getGrapes( ): String + setGrapes(String grapes): void + getApples( ): String + setApples(String apples): void
JAVA In the last lesson, we wrote a class with methods to print out the song...
JAVA In the last lesson, we wrote a class with methods to print out the song The Ants Go Marching. Notice that this is a class where there are no instance variables and we don’t really need to generate multiple objects. With students or pets, it makes sense to have multiple objects. With the Song, we can just make the methods static and have just 1 copy of them. Copy in your class from the last lesson into this active...
The class Person, uploaded on Blackboard with Lab 5, only has methods to set and print...
The class Person, uploaded on Blackboard with Lab 5, only has methods to set and print the name of a person. Redefine the class Person to include the following operations: Set the last name only Set the first name only Set the middle name Check whether a given last name is the same as the last name of this person Check whether a give first name is the same as the first name of this person Check whether a given...
Modify StudentLinkedList class by adding the following methods: printStudentList: print by calling and printing “toString” of...
Modify StudentLinkedList class by adding the following methods: printStudentList: print by calling and printing “toString” of every object in the linkedList. Every student object to be printed in a separate line.  deleteStudentByID(long id): delete student object from the list whose ID is matching with the passed parameter.  sortListByID(): sort the linkedlist according to students IDs.  findMarksAverage(): find the average of all marks for all students in the list.  findMinMark(int markIndex): find the student with the minimum...
Problem 3: Modify StudentLinkedList class by adding the following methods:  printStudentList: print by calling and...
Problem 3: Modify StudentLinkedList class by adding the following methods:  printStudentList: print by calling and printing “toString” of every object in the linkedList. Every student object to be printed in a separate line.  deleteStudentByID(long id): delete student object from the list whose ID is matching with the passed parameter.  sortListByID(): sort the linkedlist according to students IDs.  findMarksAverage(): find the average of all marks for all students in the list.  findMinMark(int markIndex): find the student...
3. [Method 1] In the Main class, write a static void method to print the following...
3. [Method 1] In the Main class, write a static void method to print the following text by making use of a loop. Solutions without a loop will receive no credit. 1: All work and no play makes Jack a dull boy. 2: All work and no play makes Jack a dull boy. 3: All work and no play makes Jack a dull boy. 4: All work and no play makes Jack a dull boy. 4. [Method 2] In the...
Modify the following code to make the input and output look like this. Input 5 Vader...
Modify the following code to make the input and output look like this. Input 5 Vader 1300 Kirk 1250 Adama 1000 Reynolds 1615 Oneill 1470 Output Enter the number of candidates: 5 Enter candidate's name :Vader 1300 Enter votes received :Enter candidate's name :Kirk 1250 Enter votes received :Enter candidate's name :Adama 1000 Enter votes received :Enter candidate's name :Reynolds 1615 Enter votes received :Enter candidate's name :Oneill 1470 Enter votes received : Name Votes Percentage Vader 1300.00 19.59% Kirk...
c# code working but output not right, I need to output all numbers like : Prime...
c# code working but output not right, I need to output all numbers like : Prime factors of 4 are: 2 x 2 here is just 2 Prime factors of 7 are: 7 Prime factors of 30 are: 2 x 3 x 5 Prime factors of 40 are: 2 x 2 x 2 x 5 here is just 2,5 Prime factors of 50 are: 2 x 5 x 5 here is just 2,5 1) How I can fix it 2)I...
package design; public class FortuneEmployee { /** * FortuneEmployee class has a main methods where you...
package design; public class FortuneEmployee { /** * FortuneEmployee class has a main methods where you will be able to create Object from * EmployeeInfo class to use fields and attributes.Demonstrate as many methods as possible * to use with proper business work flow.Think as a Software Architect, Product Designer and * as a Software Developer.(employee.info.system) package is given as an outline,you need to elaborate * more to design an application that will meet for fortune 500 Employee Information *...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT