Question

In: Computer Science

Without using extra data structures, write a recursive method recursiveProdcutQueue ( Queue <Integer> q) in Test...

Without using extra data structures, write a recursive method
recursiveProdcutQueue ( Queue <Integer> q) in Test class (in
stacks_queues package) that receives a queue of integers and return the
product of the integers inside the queue. Then don’t forget to test the
method in the main.

Make sure not to change values in queue after calling the method

use java eclipse please

Solutions

Expert Solution

`Hey,

Note: If you have any queries related to the answer please do comment. I would be very happy to resolve all your queries.

import java.util.LinkedList;
import java.util.Queue;
public class Test{
public static int recursiveProductQueue(Queue<Integer> q)
{
if(q.size()==0)
{
return 1;
}
int num=q.poll();
return num*recursiveProductQueue(q);
}


public static void main(String []args){
Queue<Integer> q
= new LinkedList<>();
  
for (int i = 1; i <=5; i++)
q.add(i);
System.out.println(recursiveProductQueue(q));
}
}

Kindly revert for any queries

Thanks.


Related Solutions

Without using extra structures, write a recursive method recursivenumberOfNonZeros (Queue<Integer> q)in Test class (in stacks_queues package)...
Without using extra structures, write a recursive method recursivenumberOfNonZeros (Queue<Integer> q)in Test class (in stacks_queues package) that receives a queue contains integer objects and return total number of non zeros in this queue. Then don’t forget to test the method in the main. Note: Make sure not to change the order of the other values in queue after calling the method.
Write a recursive method that displays an integer value reversely on the console using the following...
Write a recursive method that displays an integer value reversely on the console using the following header: public static void reverseDisplay(int value) For example, reverseDisplay(12345) displays 54321. Write a test program that prompts the user to enter an integer, invokes the method above, and displays its reversal.
/** * Write a recursive function that accepts a Queue<Integer>. It * should change every int...
/** * Write a recursive function that accepts a Queue<Integer>. It * should change every int in this queue to be double its original * value. You may NOT use loops or any other data structures besides * the queue passed in as a parameter. You may use a helper function. * @param q */ public static void doubleElements(Queue<Integer> q) {}
Without using method size(), write recursive method stackSize(Stack > s1 ) that receives a stack and...
Without using method size(), write recursive method stackSize(Stack > s1 ) that receives a stack and returns number of elements in the stack. The elements in the stack should not be changed after calling this method. please do it in java
Data Structures ( Recursion ) Assignment Write a recursive method removeMiddle that receives an array list...
Data Structures ( Recursion ) Assignment Write a recursive method removeMiddle that receives an array list which has odd number of elements, then it deletes the element in the middle only. The method should receive only one parameter (the array list) The base case is when the array list has only one element, just remove that, otherwise, you need to remove the first one and the last one then call the method, then you need to add them again after...
Write a RECURSIVE method that receives as a parameter an integer named n. The method will...
Write a RECURSIVE method that receives as a parameter an integer named n. The method will output n # of lines of stars. For example, the first line will have one star, the second line will have two stars, and so on. The line number n will have "n" number of ****** (stars) so if n is 3 it would print * ** *** The method must not have any loops!
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
Please write in java: Write a recursive method toNumber that forms the integer sum of all...
Please write in java: Write a recursive method toNumber that forms the integer sum of all digit characters in a string. For example, the result of toNumber("3ac4") would be 7. Hint: If next is a digit character ('0' through '9'), Character.isDigit(next) is true and the numeric value of next is Character. digit(next, 10).
In this lab, using C++, you will create two data structures: a stack and a queue....
In this lab, using C++, you will create two data structures: a stack and a queue. You will use STL containers to demonstrate basic ADTs. Queue For the queue, you will simulate a buffer. Remember it is first-in-first-out. The user will enter a number for the number of rounds to run your simulation. You need one function that randomly generates a number. You will also have a user specified percentage, and the function uses this percentage to randomly put the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT