Question

Create two Java classes called Recursive, RecursiveDemo. Write four methods a- int sum_sqr_rec(stack<int> stk) which will...

Create two Java classes called Recursive, RecursiveDemo.

Write four methods

  • a- int sum_sqr_rec(stack<int> stk)
    • which will receive a stack of "int" and output the sum of the squares of the elements in the stack.
  • b- int plus_minus_rec(stack<int> stk)
    • which will receive a stack of "int" (example: {a,b,c,d,e,f,g,h,i,j}) and output the sum of the elements in the stack as follows:
    • a - b + c - d + e - f + g - h + i -j
  • c- void prt_chars_rev_rec(stack<char> stk)
    • which will receive a stack of "char" and print its elements in reverse.
  • d- void prt_chars_rec(queue<char> stk) which will receive a queue of "char" and print its elements

Remember to use the stack and queue STL.

Create 2 files:

  1. Recursive.java which contains the details of creating the 4 methods as specified above:
    • int sum_sqr_rec(stack<int> stk), (15 points)
    • int plus_minus_rec(stack<int> stk), (15 points)
    • void prt_chars_rev_rec(stack<char> stk), (15 points)
    • void prt_chars_rec(queue<char> stk), (15 points)
  2. RecursiveDemo.java which:
    • A- reads a string expression:
      • {(1+2)+[4*(2+3)]}
      • and store the expression in a stack and a queue.(15 points)
      • a- prints the corresponding expression "in reverse" using: prt_chars_rev_rec ( 5 points)
      • b- prints the corresponding expressing "as is" using: prt_chars_rec.( 5 points)
    • B- reads an array of integers: { 1, 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 }
      • and store them in a stack of ints.(5 points)
    • Then it:
    • C- prints the sum of the squares of the elements in the stack using int sum_sqr_rec(stack<int> stk) and outputting the value(5 points):
      • 385
    • D- prints the sum of the elements in the stack using:
      • int plus_minus_rec(stack<int> stk) and outputting the value(5 points):
      • 1 - 2 + 3 - 4 + 5 - 6 + 7 - 8 + 9 - 10 = -5
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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[+

-----------------------------------------------------------------------------------------------------------------------------

COMMENT DOWN FOR ANY QUERIES...............................
HIT A THUMBS UP IF YOU DO LIKE IT!!!

Add a comment
Know the answer?
Add Answer to:
Create two Java classes called Recursive, RecursiveDemo. Write four methods a- int sum_sqr_rec(stack<int> stk) which will...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • » Part A: Stack Create a Stack struct that is a wrapper for your linked list o You should impleme...

    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...

  • Write an application with two classes USING JAVA METHODS HAVE TO BE CREATED USING RECURSION. First...

    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...

    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[]...

    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...

  • I have a Graph.java which I need to complete four methods in the java file: completeGraph(),...

    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 =...

  • Write code in static void main method class, which creates a boolean array called flags, size...

    Write code in static void main method class, which creates a boolean array called flags, size 10, and using a for loop sets each element to alternating values (true at index zero, false at index one Write code in a static void main method class, which creates a float array called sales, size 5, uses an initializer list to instantiate its elements with positive values having two decimal places (example 7.25), and using a for loop reads those values of...

  • The loop below stores the values 1 to 5 in myList: for (int i = 0;...

    The loop below stores the values 1 to 5 in myList: for (int i = 0; i < 5; i ++) { myList [ i ] = i + 1; //store values using a loop } The loop below prints the values from first to last in myList: for (int i = 0; i < myList.length; i++) { System.out.print(myList [ i ] + " "); } Use the examples on the slides to complete the lab below. Please submit the...

  • Write a simple Java program with the following naming structure: Open Eclipse Create a workspace called...

    Write a simple Java program with the following naming structure: Open Eclipse Create a workspace called hw1 Create a project called hw1 (make sure you select the “Use project folder as root for sources and class files”) Create a class called Hw1 in the hw1 package (make sure you check the box that auto creates the main method). Add a comment to the main that includes your name Write code that demonstrates the use of each of the following basic...

  • 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>? ________...

    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...

  • Q1) Write a method called inToTree of ExpressionTree class which takes a string of infix expression...

    Q1) Write a method called inToTree of ExpressionTree class which takes a string of infix expression (fully parenthesized), resets the root of the expression tree to such tree which constructed from the expression. The root set to null of the expression contains characters other than value, operator, and parenthesis or if it’s not an in-fix full parenthesized expression. You may assume each value is single digit only . public void inToTree(String expression){ You may use a JCF Deque class as...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT