Question

In this lab, you will practice using a stack as a substitute for recursion. Your task is to implement a non-recursive stack-based version of the following method: public void drawGasket(int x, int y, int side) t int sub -side / 3; //Draw center square g2d.fill (new Rectangle2D. Double(x + sub, y + sub, sub - 1, sub - 1)); if (sub >= 3) { //Draw 8 surrounding squares for (int i = 0; i < 3; i++){ for (int j = 0; j < 3; j++){ if (j1-1 II i != 1){ drawGasket (x + i * sub , y + j * sub , sub);Uses Java

0 0
Add a comment Improve this question Transcribed image text
Answer #1

public void drawGasket(int x,int y,int side)
{
//u have to import java.util.Stack
Stack x1 = new Stack();//for storing x
Stack y1 = new Stack();//for storing y
Stack z1 = new Stack();//for storing side
  
int sub = side/3;
//draw ccenter square
g2d.fill(new Rectangle2D.Double(x+sub,y+sub,sub-1,sub-1));
  
if(sub >= 3)
{//draw 8 surrrounding squares..
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(j!=1||i!=1)
{
// drawGasket(x+1*sub,y+j*sub,sub);//recursive funtion...
//replacing with push and pop operations...
x1.push(x);y1.push(y);z1.push(side);//pushing current function values...
x=x+i*sub;y=y+j*sub;side = sub;//updating new values,,,
sub = side/3;
g2d.fill(new Rectangle2D.Double(x+sub,y+sub,sub-1,sub-1));//drawing a rectangle//with new values..
//poping parameters...
x=(int) x1.pop();
y = (int) y1.pop();
side = (int) z1.pop();
}
  
}
  
}
  
  
  
  
}
  
  
}

Add a comment
Know the answer?
Add Answer to:
Uses Java In this lab, you will practice using a stack as a substitute for recursion....
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
  • LANGUAGE IS C++ Lab Ch14 Recursion In this lab, you are provided with startup code which...

    LANGUAGE IS C++ Lab Ch14 Recursion In this lab, you are provided with startup code which has six working functions that use looping (for, while, or do loops) to repeat the same set of statements multiple times. You will create six equivalent functions that use recursion instead of looping. Although looping and recursion can be interchanged, for many problems, recursion is easier and more elegant. Like loops, recursion must ALWAYS contain a condition; otherwise, you have an infinite recursion (or...

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

  • * Your goal in this exercise is to practice recursion and * to see how a...

    * Your goal in this exercise is to practice recursion and * to see how a properly written recursive solution can * take care of fairly complicated tasks with a few lines * of (well thought out) code. * * We will be solving Sudoku puzzles. In case you have never * solved or seen a Sudoku, you can learn more about them * here: * * https://en.wikipedia.org/wiki/Sudoku * * Your task if to write a function that takes an...

  • Lab #4 – Recursive Methods for Generic ArrayList ----- JAVA The problem Use recursion to implement...

    Lab #4 – Recursive Methods for Generic ArrayList ----- JAVA The problem Use recursion to implement some list functionalities on an ArrrayList of generic type using type parameters in method definition Our goal for this lab is to continue using the divide and conquer approach of splitting a list into its head and tail and keep recursing on the tail (which happens to be smaller). However, instead of trying the approach on a string (a list of characters) we would...

  • Recursion Exercises These exercises provide practice with recursion in Java. Please add notes if possible. Objectives...

    Recursion Exercises These exercises provide practice with recursion in Java. Please add notes if possible. Objectives Module: To write recursive solutions for basic problems that require iteration. To identify and address base cases in a recursive method. Reminders during development Each of your solutions to the problems below should be in their own method. If a method header is provided for a problem, then your solution should have that exact method header. Any changes to the method header will receive...

  • In JAVA Thank You What is the exact output produced by running the method test? /**...

    In JAVA Thank You What is the exact output produced by running the method test? /** * TestSwap.java * Demonstrates parameter passing involving arrays and integers, * scope of variables, flow of control, and overloaded methods. */ public class TestSwap { public void swap (int x, int y) { int temp; temp = x; x = y; y = temp; System.out.println("Inside swap version 1:"); System.out.println("x = " + x); System.out.println("y = " + y); } public void swap (int[] a,...

  • C++ Recursion Practice! 1)Multiplication #include <iostream.h> int Multiply(int M, int N) //Performs multiplication using the +...

    C++ Recursion Practice! 1)Multiplication #include <iostream.h> int Multiply(int M, int N) //Performs multiplication using the + operator. //Pre : M and N are defined and N > 0. //Post: Returns M x N { int Prod; if (N == 1)     Prod = M;                       //base case else     Prod = M + Multiply(M, N - 1); //recursive step return Prod; } 2) Reverse #include <iostream.h> void Reverse(int N) //Displays string of length N in the reverse order //Pre : N...

  • LC3 stack (factorial) I need help in writing factorial in Lc3 language by using stack.. ; Begin ...

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

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