Data Structures: CHAPTER 5: WORKING WITH STRINGS, ARRAYS, AND ARRAYLISTS Assignm
ID: 3814682 • Letter: D
Question
Data Structures: CHAPTER 5: WORKING WITH STRINGS, ARRAYS, AND ARRAYLISTS
Assignment:
The purpose of the assignment is to practice writing methods that are recursive. We will write four methods each is worth 15 points.
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) and prints its elements which will receive a queue of "char" and print its elements.
Remember to use the stack and queue provided by java.
The Assignment will require you to create 2 files:
1- Recursive.java which contain 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
Explanation / Answer
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
class Recursive{
/**
*
* @param stk
* @return
*/
int sum_sqr_rec(Stack<Integer>stk){
int sum=0;
if(stk!=null && stk.size()>0){
int num=stk.pop(); // getting the element from stack
sum+=(num*num); // squaring and adding it to the sum
sum+=sum_sqr_rec(stk); // recursively calling the function
stk.push(num);
return sum;
}
return sum;
}
/**
*
* @param stk
* @return
*/
int plus_minus_rec(Stack<Integer>stk){
int sum=0;
if(stk!=null && stk.size()>0){
int num=stk.pop();// getting the element from stack
sum+=-num;
sum+=-plus_minus_rec(stk);// recursively calling the function
return sum;
}
return sum;
}
/**
*
* @param stk
*/
void prt_chars_rev_rec(Stack<Character>stk){
if(stk!=null && stk.size()>0){
Character c=stk.pop();// getting the element from stack
System.out.print(c);
prt_chars_rev_rec(stk);// recursively calling the function
}
}
/**
*
* @param stk
*/
void prt_chars_rec(Queue<Character> stk){
if(stk!=null && stk.size()>0){
Character c=stk.poll();// getting the element from stack
System.out.print(c);
prt_chars_rec(stk);// recursively calling the function
}
}
}
public class RecursiveDemo {
public static void main(String[] args) {
String expression="{(1+2)+[4*(2+3)]}";
//String expression="jayk";
Stack<Character> stack= new Stack<>(); // stack object
Queue<Character>queue= new LinkedList<>(); // queue object
String [] expressionArr=expression.split(""); // splitting the expression into array of characters
for(String expr:expressionArr){
stack.push(expr.charAt(0));// pushing to stack
queue.add(expr.charAt(0));// adding to queue
}
Recursive recursive=new Recursive();
System.out.println("----------prt_chars_rev_rec output---------------------");
recursive.prt_chars_rev_rec(stack);
System.out.println("");
System.out.println("----------prt_chars_rec output---------------------");
recursive.prt_chars_rec(queue);
int [] integers={1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Stack<Integer> stackInt= new Stack<>();
for(int num:integers){
stackInt.push(num);
}
int sumOfSquares=recursive.sum_sqr_rec(stackInt);
System.out.println("");
System.out.println("Sum of squares: "+sumOfSquares);
int sum=recursive.plus_minus_rec(stackInt);
System.out.println("Sum : "+sum);
}
}
--------------------------------------------------------Output----------------------------------------------------------
----------prt_chars_rev_rec output---------------------
}])3+2(*4[+)2+1({
----------prt_chars_rec output---------------------
{(1+2)+[4*(2+3)]}
Sum of squares: 385
Sum : -5
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.