Create two Java classes called Recursive, RecursiveDemo.
Write four methods
Remember to use the stack and queue STL.
Program code to copy:
// ------------------------------------ Recursive.java ------------------------------------ //
class Recursive {
// --- Defining all 4 methods which are mentioned in statement
// ------------------- Method_1
------------------------------
// This method will return the sum of all elements of stack by
first taking square of them
public int sum_sqr_rec(Stack<Integer> stk){
// --- Declaring a variable to sum the squares of all elements of stack
int sum = 0;
// --- Going through whole stack and squaring each element and then adding in sum
for (int i=0; i<stk.size(); i++){
sum += (stk.get(i) * stk.get(i));
}
// --- Finally return the sum to calling function
return sum;
}
// -------------------
Method_2------------------------------
// This method will add elements of even indices and subtract
elements of odd indices in stack
public int plus_minus_rec(Stack<Integer> stk){
int sum = 0;
for (int i=0; i<stk.size(); i++){
if(i % 2 == 0) // --- If index is even add that
integer
sum += stk.get(i);
else // --- If it is odd, just subtract that
sum -= stk.get(i);
}
return sum;
}
// -------------------
Method_3------------------------------
// This method will print character elements of stack in reverse
order
public void prt_chars_rev_rec(Stack<Character> stk){
System.out.println("The elements of Stack in reverse order are: ");
for (int i=stk.size()-1; i>=0 ; i--) { // --- Just go through whole stack in reverse order from last index to first index
System.out.print(stk.get(i));
}
}
// -------------------
Method_4------------------------------
// This method will print the character elements of queue
public void prt_chars_rec(Queue<Character> stk){
System.out.println("The elements of Queue are: ");
// --- Go through whole queue
// --- Removing each element from queue in order to print it
// --- Until the queue becomes empty
for (int i=0; !stk.isEmpty(); i++){
System.out.print(stk.remove());
}
}
}
// ------------------------------------ RecursiveDemo.java ---------------------------- //
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class RecursiveDemo {
public static void main(String[] args) {
// --- Declaring a String expression as specified in statement
String expression = "{(1+2)+[4*(2+3)]}";
// --- Creating a stack and a queue
Stack<Character> stack = new Stack<>();
Queue<Character> queue = new LinkedList<>();
// --- Iterating through whole String and storing each character in a Stack and a queue
for (int i=0; i<expression.length(); i++){
stack.push(expression.charAt(i));
queue.add(expression.charAt(i));
}
// --- Creating an object of Recursive class
Recursive r = new Recursive();
// --- printing the expression in reverse order by calling prt_chars_rev_rec method
System.out.println();
r.prt_chars_rev_rec(stack);
System.out.println("\n\n============================================\n");
// --- printing the elements as is using prt_chars_rec method
r.prt_chars_rec(queue);
System.out.println("\n\n============================================\n");
// --- Read an array of integers as given in statement and storing them in a stack of ints
int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Stack<Integer> ints = new Stack<>();
for (int i=0; i<array.length; i++){
ints.add(array[i]);
}
// --- Calling sum_sqr_rec method to output sum of squared elements
System.out.println("The sum of squares of elements in the Stack
is " + r.sum_sqr_rec(ints));
System.out.println("\n============================================\n");
// --- Now printing the sum of elements by calling plus_minus_rec method
System.out.println("The sum of elements of stack is " +
r.plus_minus_rec(ints));
System.out.println("\n============================================\n");
}
}
Sample output:
![RecursiveDemo x C:\Program Files\Java\jdk1.8.0_111\bin\java.exe ... The elements of Stack in reverse order are: }])3+2(*4[+](http://img.homeworklib.com/questions/e7f52060-8382-11eb-9a60-995b6abf3968.png?x-oss-process=image/resize,w_560)
-----------------------------------------------------------------------------------------------------------------------------
COMMENT DOWN FOR ANY
QUERIES...............................
HIT A THUMBS UP IF YOU DO LIKE IT!!!
Create two Java classes called Recursive, RecursiveDemo. Write four methods a- int sum_sqr_rec(stack<int> stk) which will...
Using C
Please comment
» Part A: Stack Create a Stack struct that is a wrapper for your linked list o You should implement the following functions that take a stack o void push(Stack * stack, int value) ● int pop(Stack * stack) Prompt the user to input 5 integers, and use the stack to reverse the integers Print the result to the screen. o o » Part B: Queue o Create a Queue struct that is a wrapper for...
Question1 ) The STL provides similiar classes called stack and queue, both of which gave functions called push and pop. Answer the following questions: 1.1) Does the push perform a different action in the stack class then in the queue class? Explain the answer. 1.2) Does the pop perform a different action in the stack class then in the queue class? Explain the answer. Question 2) 2.1) Suppose you are tasked with implementing a reverse queue in which elements are...
Write an application with two classes USING JAVA METHODS HAVE TO BE CREATED USING RECURSION. First class named Recursion has the following three recursive methods: // PRECONDITION n is positive integer. Method sumTerms returns sum of terms that are // reciprocal values of first n integers, with alternating signs. // For example, sumTerms(7) returns the following: 1/1 – 1/2 + 1/3 – 1/4 + 1/5 -1/6 + 1/7. // Terms with an odd denominator have positive sign, and terms with even denominator have // negative sign. ...
JAVA Implement a MyQueue class which implements a queue using two stacks. private int maxCapacity = 4; private Stack stack1; private Stack stack2; Note: You can use library Stack but you are not allowed to use library Queue and any of its methods Your Queue should not accept null or empty String or space as an input You need to implement the following methods using two stacks (stack1 & stack2) and also you can add more methods as well: public...
Done in C++ using visual studio 1. Write a program with one additional function called int[] reverseArray(int array). This function will receive an array from main and then reverse and should return the reversed array to the main to print out. Use a single array and a single loop, you’re are neither allowed to print out just in reverse order nor allowed to use another array variable to store the original array in reverse order. 2. Write a program which...
In Java, create a program implementing the functionalities of a standard queue in a class called Queue3503. You will test the functionalities of the Queue3503 class from the main() method of the Main class. In a queue, first inserted items are removed first and the last items are removed at the end (imagine a line to buy tickets at a ticket counter). Do NOT change your class name from "Main". The Main class should come first in your code. Your filename should be "Main.java". The Queue3503 class will contain:...
I need assistance with this code. Is there any way I can create
this stack class (dealing with infix to postfix then postfix
evaluation) without utilizing <stdio.h> and
<math.h>?
____________________________________________________________________________________________
C++ Program:
#include <iostream>
#include <string>
#include <stdio.h>
#include <math.h>
using namespace std;
//Stack class
class STACK
{
private:
char *str;
int N;
public:
//Constructor
STACK(int maxN)
{
str = new char[maxN];
N = -1;
}
//Function that checks for empty
int empty()
{
return (N == -1);
}
//Push...
I have a Graph.java which I need to complete four methods in the java file: completeGraph(), valence(int vid), DFS(int start), and findPathBFS(int start, int end). I also have a JUnit test file GraphTest.java for you to check your code. Here is Graph.java: import java.util.ArrayList; import java.util.LinkedList; import java.util.Queue; import java.util.Stack; /* Generic vertex class */ class Vertex<T> { public T data; public boolean visited; public Vertex() { data = null; visited = false; } public Vertex(T _data) { data =...
In Java Write a recursive method called sumUpto(n) which will return the sum of the first n integers. For example, sumUpto(4) will return 10 because 1+2+3+4 = 10. sumUpto(5) will return 15 because 1+2+3+4+5=15. So you can see that sumUpto(5) = sumUpto(4) + 5. Make this more general for n : sumUpto(n) = sumUpto(??) + n Figure out the base case and write the method……….. import java.util.Scanner; public class Lab12Num1 { public static int sumUpto(int num) { } public...
Create a new Java Application that test MyStack by having the following methods in main class: 1. A method to generate a number of element between two given values and save them in a stack 2. A method to print a stack (10 elements per line). The original stack should remain as is after the print 3. A method to exchange the first element and the last element in a stack 4. A Boolean method to search for a value...