You must implement the following methods using Java's Stack Object.
/**
* Computes the factorial of n
* @param n-integer value greater or equal to 0
* @return n!
*/
public static int factorial( int n ) { }
/**
* Computes the nth term of the Fibonacci
sequence
* @param n -nth term to find
* @return -the nth term
*/
public static int fibonacci( int n ) {}
/**
* Find the min value using the comparable interface of
the elements found in theStack
* @param theStack - the stack of objects to
search
* @return the min value
*/
public static <T extends Comparable< T> >
T min( Stack< T > theStack ) {}
/**
* Computes the factorial of n
*
* @param n-integer value greater or equal to 0
* @return n!
*/
public static int factorial(int n) {
Stack<Integer> aux = new Stack<>();
while(n >= 1) {
aux.push(n--);
}
int value = 1;
while(! aux.isEmpty()) {
value *= aux.pop();
}
return value;
}
/**
* Computes the nth term of the Fibonacci sequence
*
* @param n -nth term to find
* @return -the nth term
*/
public static int fibonacci(int n) {
Stack<Integer> aux = new Stack<>();
aux.push(0);
aux.push(1);
int secondLast = 0;
for(int i=2; i<n; i++) {
int last = aux.peek();
aux.push(last + secondLast);
secondLast = last;
}
return aux.pop() + aux.pop();
}
/**
* Find the min value using the comparable interface of the elements found in
* theStack
*
* @param theStack - the stack of objects to search
* @return the min value
*/
public static <T extends Comparable<T>> T min(Stack<T> theStack) {
if(theStack.isEmpty()) {
return null;
}
T minEle = theStack.peek();
Stack<T> aux = new Stack<>();
while(!theStack.isEmpty()) {
T tmp = theStack.pop();
if(tmp.compareTo(minEle) < 0) {
minEle = tmp;
}
aux.push(tmp);
}
// restore/
while(!aux.isEmpty()) {
theStack.push(aux.pop());
}
return minEle;
}
You must implement the following methods using Java's Stack Object. /** * Computes the factorial of...
LC3 stack (factorial) I need help in writing factorial in Lc3 language by using stack.. ; Begin reserved section: do not change ANYTHING in reserved section! .ORIG x3000 BR Main ; Parameter and result Param .FILL x0004 Result .BLKW 1 ; Constants Stack .FILL x4000 One .FILL #1 MinusOne .FILL #-1 ; End reserved section: do not change ANYTHING in reserved section! ;------------------------------------------------------------------------------- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ; int Factorial(int N) ; Returns N! (must be a recursive function) ; Factorial ;__________________...
I just need to add comment for the code blow. Pleas add comment for each method and class if comments left out. package exercise01; /*************************************************************************** * <p> This program demonstrates the technique of recursion and includes * recursive methods that are defined for a variety of mathematical * functions. * * <br>A recursive method is one that directly or indirectly calls itself * and must include: * <br>(1) end case: stopping condition * which terminates/ends recursion * <br>(2) reduction:...
Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...
There is a data structure called a drop-out stack that behaves like a stack in every respect except that if the stack size is n, then when the n+1element is pushed, the bottom element is lost. Implement a drop-out stack using links, by modifying the LinkedStack code. (size, n, is provided by the constructor. Request: Please create a separate driver class, in a different file, that tests on different types of entries and show result of the tests done on...
The following recursive method factRecursive computes the factorial of positive integer n. Demonstrate that this method is recursive. public static int factRecursive(int n) { int result = 0; if (n == 0) { result = 1; } else { result = n * factRecursive(n - 1); } return result; }
a) Create a class MinHeap implementation using an Array. Find the kth smallest value in a collection of n values, where 0 < k < n. Write a program that uses a minheap method to find the kth smallest value in a collection of n values. Use the MinHeap class defined in part a. public final class MinHeap<T extends Comparable<? super T>> implements MinHeapInterface<T> { private T[] heap; // Array of heap entries; ignore heap[0] private int...
//implement the binomial heap please import java.util.LinkedList; import java.util.NoSuchElementException; /** * Binomial Heap Implementation * * @author First Last * @since ${date} */ public class BinomialHeap<T extends Comparable<? super T>> implements binomialHeapInterface<T> { private static final int DEFAULT = 5; // default size for the binomial heap public Node<T>[] forest; private int n; private boolean isMaxHeap; /** * Node Class for nodes in Binomial Heap */ protected class Node<T> { private T value; private int degree; private LinkedList<Node<T>> children; /**...
How to Submit Please submit your answers to the lab instructor once you have completed. Failure to submit will result in a ZERO FOR THIS LAB. NO EXCEPTIONS. 1. Implement a method factorial(int n), that takes a number (int) and returns its factorial. Factorial of an integer is the product of all positive integers less than or equal to it. Method needs to be implemented using recursion. 2. Implement a method fibonacci(int n), that takes a number (int) and prints...
I need the following merge-sort assignment completed using the "ArrayList<Comparable>" (the part I'm really stuck on) with comments: import java.util.ArrayList; public class Mergesort { /** * Sorts list given using the mergesort algorithm * @param list the list to be sorted * @param first the index of the first element of the list to be sorted * @param last the index of the last element of the list to be sorted */ public static void mergesort(ArrayList<Comparable> list, int first, int...
In Java. What would the methods of this class look like?
StackADT.java
public interface StackADT<T>
{
/** Adds one element to the top of this stack.
* @param element element to be pushed onto stack
*/
public void push (T element);
/** Removes and returns the top element from this stack.
* @return T element removed from the top of the stack
*/
public T pop();
/** Returns without removing the top element of this
stack.
* @return T...