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...
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 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.
Implement and complement the recursive code to compute the product of two positive integers, x and...
Implement and complement the recursive code to compute the product of two positive integers, x and y, using only addition and/or subtraction. The function will take as its arguments two integers to multiply together ( x x y ) and will return the product. Hint: consider the following: 6 x 1 = 6 6 x 2 = 6 + (6 x 1) 6 x 3 = 6 + (6 x 2) = 6 + [6 + (6 x 1)] =...
Please use java language in an easy way and comment as much as you can! Thanks...
Please use java language in an easy way and comment as much as you can! Thanks 1. A driver class with a main method, which creates instances of various objects (two of each subclass) and adds them as items to an ArrayList named "inventory". A foreach loop should loop through the ArrayList and call the use() method of each item. Define the ArrayList in a way that it only holds elements of the GameItem class and its subclasses. Make proper...
Java Language Add a recursive method to the program shown in the previous section that states...
Java Language Add a recursive method to the program shown in the previous section that states how many nodes does the stack have. Code: class Stack { protected Node top; Stack() { top = null; } boolean isEmpty() { return( top == null); } void push(int v) { Node tempPointer; tempPointer = new Node(v); tempPointer.nextNode = top; top = tempPointer; } int pop() { int tempValue; tempValue = top.value; top = top.nextNode; return tempValue; } void printStack() { Node aPointer...
Java Language Add a recursive method to the program shown in the previous section that allows...
Java Language Add a recursive method to the program shown in the previous section that allows remove the last node from the stack. Code: class Stack { protected Node top; Stack() { top = null; } boolean isEmpty() { return( top == null); } void push(int v) { Node tempPointer; tempPointer = new Node(v); tempPointer.nextNode = top; top = tempPointer; } int pop() { int tempValue; tempValue = top.value; top = top.nextNode; return tempValue; } void printStack() { Node aPointer...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT