Question

public static int findEscapeCount(double x, double y, int maxIterations) Determines how many iterations of the following...

public static int findEscapeCount(double x,
                                  double y,
                                  int maxIterations)

Determines how many iterations of the following operation are required until the condition (a * a + b * b) > 4 is reached:

   tempA = a * a - b * b + x
   tempB = 2 * a * b + y
   a = tempA
   b = tempB
 

where a and b are initially zero, and x and y are given parameters. For example, with x = 1 and y = 0.5, after one iteration a should be 1 and b should be 0.5. After two iterations a should be 1.75 and b should be 1.5, at which point the method returns with value 2 since (1.75)2 + (1.5)2 = 5.3, which is greater than 4. If the condition (a * a + b * b) > 4 is not reached within maxIterations, the method just returns maxIterations.

Parameters:

x - given x value

y - given y value

maxIterations - maximum number of iterations to attempt

Returns:

number of iterations required to get (a * a + b * b) > 4, or maxIterations

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

Please find the code below:

FindMaxInteration.java

package classes10;

import java.util.Scanner;

public class FindMaxInteration {
   private static double a=0;
   private static double b=0;
   public static int findEscapeCount(double x,double y,int maxIterations){
       int iteration = 0;
       while(!((a * a + b * b) > 4)){
           iteration++;
           double tempA = a * a - b * b + x;
           double tempB = 2 * a * b + y;
           a = tempA;
           b = tempB;
           if(iteration>maxIterations){
               return maxIterations;
           }
       }
       return iteration;
   }
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       double x,y;
       int maxIteration;
       System.out.print("Enter value of x : ");
       x = sc.nextDouble();
       System.out.print("Enter value of y : ");
       y = sc.nextDouble();
       System.out.print("Enter value of max iteration : ");
       maxIteration = sc.nextInt();
       System.out.println("Escape count is "+findEscapeCount(x, y, maxIteration));
   }
}

output:

Add a comment
Know the answer?
Add Answer to:
public static int findEscapeCount(double x, double y, int maxIterations) Determines how many iterations of the following...
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
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