Question

In: Computer Science

Please use java language Thanks! Implement a recursive method called "pow" that takes 2 integers, x...

Please use java language Thanks!

  1. Implement a recursive method called "pow" that takes 2 integers, x and y, as parameters and returns the value xy (x raised to the power y). The exponent must be non-negative. If a negative argument is given for the exponent, then an exception should be thrown.

  2. Implement a recursive method called "fib" that takes a positive integer, n, as a parameter and returns the nth Fibonacci value. Assume that the first 2 values in the Fibonacci sequence are 1 and 1. So, fib(1) should return 1, fib(2) should return 1, fib(3) should return 2, fib(4) should return 3, and so on. The return type should be double. Note that Fibonacci numbers are integers, but they get very large very quickly. So we will use double as our return type only because that will allow very large values to be returned. If a non-positive value is passed as an argument, then an exception should be thrown.

    NOTE: You would never implement Fibonacci recursively in real life.

  3. Implement a method called "add" that takes an array of doubles as a parameter and returns the sum of all the values in the array. You must use recursion in your solution. (You will probably want to implement private "helper" method that is recursive. Your public "add" method won't actually be recursive, but it will call the recursive helper method.)

Solutions

Expert Solution

package math;

public class PowerAndFib {

public static double power(int x,int y) throws Exception{

if(y<0) //if y <0 throw exception

throw new Exception("Exponent can not be negative");

if(y==0)

return 1;

return x*power(x,y-1); //if Exponent is positive

}

public static double fib(int n) throws Exception{

if(n<0)

throw new Exception("Exponent can not be negative");

if(n==1 || n==2)

return 1;

return fib(n-1)+fib(n-2); //return sum of prev two element

}

public static double Add(double ar[]){

return Add(ar,0);

}

public static double Add(double ar[],int i){ //takes arrray and index as paramter

if(i<ar.length){ //i is less than length

return ar[i]+Add(ar,i+1); //add sum to next recursive call

}

return 0; //else return 0

}

public static void main(String[] args) throws Exception {

System.out.println(power(4,2));

System.out.println(fib(4));

double ar[]={2,4,5,6,8};

System.out.println(Add(ar));

}

}

output

16.0

3.0

25.0


Related Solutions

Write a recursive method pow(x, y) to calculate xy, where x and y are positive integers....
Write a recursive method pow(x, y) to calculate xy, where x and y are positive integers. If x=2, y=4, the method pow should return 16. Java answers only please.
use java for : 1. Write a method called indexOfMax that takes an array of integers...
use java for : 1. Write a method called indexOfMax that takes an array of integers and returns the index of the largest element. 2. The Sieve of Eratosthenes is “a simple, ancient algorithm for finding all prime numbers up to any given limit” (https://en.wikipedia. org/wiki/Sieve_of_Eratosthenes).Write a method called sieve that takes an integer parameter, n, and returns a boolean array that indicates, for each number from 0 to n -1, whether the number is prime.
java/ netbeans Write a recursive method smallestNumber which takes an ArrayList of Integers as input and...
java/ netbeans Write a recursive method smallestNumber which takes an ArrayList of Integers as input and returns the smallest number in the array. You can use a helper method if needed. Write a main method that asks the user for a series of numbers, until the user enters a period. Main should create an ArrayList of these Integers and call smallestNumber to find the smallest number and print it. Compile and test your code in NetBeans and then on Hackerrank.
Code in Java Write a recursive method smallestNumber which takes an ArrayList of Integers as input...
Code in Java Write a recursive method smallestNumber which takes an ArrayList of Integers as input and returns the smallest number in the array. You can use a helper method if needed. Write a main method that asks the user for a series of numbers, until the user enters a period. Main should create an ArrayList of these Integers and call smallestNumber to find the smallest number and print it. Input Format A series of integers Constraints None Output Format...
// the language is java, please implement the JOptionPane Use method overloading to code an operation...
// the language is java, please implement the JOptionPane Use method overloading to code an operation class called CircularComputing in which there are 3 overloaded methods and an output method as follows: • computeObject(double radius) – compute the area of a circle • computeObject(double radius, double height) – compute area of a cylinder • computeObject(double radiusOutside, double radiusInside, double height) – compute the volume of a cylindrical object • output() use of JOptionPane to display instance field(s) and the result...
FOR JAVA Write a method called findNum that takes a two-dimension array of integers and an...
FOR JAVA Write a method called findNum that takes a two-dimension array of integers and an int as parameters and returns the number of times the integer parameter appears in the array. For example, if the array (as created by the program below) is 10 45 3 8 2 42 3 21 44 And the integer parameter is 3, the value returned would be 2 (the number 3 appears two times in the array) public class HomeworkA { public static...
Please use Java language! With as many as comment! ThanksWrite a static method called "evaluate"...
In Java language Write a static method called "evaluate" that takes a string as a parameter. The string will contain a postfix expression, consisting only of integer operands and the arithmetic operators +, -, *, and / (representing addition, subtraction, multiplication, and division respectively). All operations should be performed as integer operations. You may assume that the input string contains a properly-formed postfix expression. The method should return the integer that the expression evaluates to. The method MUST use a stack...
Please use Java language! with as much as comment! thanks! Write a program that displays a...
Please use Java language! with as much as comment! thanks! Write a program that displays a frame with a three labels and three textfields. The labels should be "width:", "height:", and "title:" and should each be followed by one textfield. The texfields should be initialized with default values (Example 400, 600, default title), but should be edited by the user. There should be a button (label it whatever you want, I don't care). If you click the button, a new...
Please use Java language! with as much as comment! thanks! Write a program that displays a...
Please use Java language! with as much as comment! thanks! Write a program that displays a frame with a three labels and three textfields. The labels should be "width:", "height:", and "title:" and should each be followed by one textfield. The texfields should be initialized with default values (Example 400, 600, default title), but should be edited by the user. There should be a button (label it whatever you want, I don't care). If you click the button, a new...
(Please solve the question using C Language. Thanks). Write a function called is_perfect which takes an...
(Please solve the question using C Language. Thanks). Write a function called is_perfect which takes an integer n and returns 1 if n is a perfect number, otherwise it will return 0. If the sum of a number’s proper divisors are equal to the number, than the number is called a perfect number. For example, 6 is a perfect number: 6=1+2+3.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT