Question

In: Computer Science

All Tasks are commented in the code: public class MandelbrotUtil { private MandelbrotUtil() { } /**...

All Tasks are commented in the code:

public class MandelbrotUtil {

private MandelbrotUtil() {

}

/**

* Return the number of iterations needed to determine if z(n + 1) = z(n) * z(n) + c

* remains bounded where z(0) = 0 + 0i. z(n + 1) is bounded if its magnitude

* is less than or equal to 2. Returns 1 if the magnitude of c

* is greater than 2. Returns max if the magnitude

* of z(n + 1) is less than or equal to 2 after max iterations.

*

* <p>

* If z(n + 1) remains bounded after max iterations then c is assumed to

* be in the Mandelbrot set.

*

* @param c a complex number

* @param max the maximum number of iterations to attempt

* @return the number of iterations needed to determine if z(n + 1) = z(n) * z(n) + c

* remains bounded where z(0) = 0.0 + 0.0i.

* @pre. max is greater than 0

*/

public static int mandelbrotIterations(Complex c, int max) {

Complex z = new Complex(0.0, 0.0);

// You need a loop here. Inside the loop, set z to z * z + c

// (i.e. perform one iteration of the equation) and

// check if the magnitude of z is greater than 2; if

// the magnitude is greater than 2 then return the

// number of times you computed z * z + c.

// If you compute z * z + c max times and the magnitude

// of z is still less than or equal to 2 you should

// return max.

return max;

}

}

Solutions

Expert Solution

public class MandelbrotUtil {

private MandelbrotUtil() {

}

public static int mandelbrotIterations(Complex c, int max) {

Complex z = new Complex(0.0,0.0);

for (int t = 0; t < max; t++) {
if (abs(z) > 2.0) return t;
z = times(z,z);
z = plus(z,c);
}

return max;

}

// HELPER FUNCTIONS

public static double abs(Complex c){ //Function to calculate the absolute value of Complex number
double ab;
ab = Math.hypot(c.real, c.imag);
return ab;
}

// return a new Complex object whose value is (this * b)
public static Complex times(Complex a,Complex b) { //Multiplying two Complex numbers
double real = a.real * b.real - a.imag * b.imag;
double imag = a.real * b.imag + a.imag * b.real;
return new Complex(real, imag);
}

public static Complex plus(Complex a, Complex b){ //Adding two Complex numbers
double real = a.real+b.real;
double imag = b.imag+a.imag;
return new Complex(real,imag);
}

}

Complex number class:

public class Complex {
double real,imag;
  
public Complex(double a, double b){
real=a;
imag=b;
}
  
}


Related Solutions

The question is as follows with all of the requirements commented in. /** * A class...
The question is as follows with all of the requirements commented in. /** * A class that represents a Hounsfield unit. Hounsfield units are the units of * measurement used in computed tomography (CT or CAT) scanning. * * <p> * The Hounsfield scale is defined by specifying the radiodensity of air as * {@code -1000} Hounsfield units and the radiodensity of distilled water as * {@code 0} Hounsfield units. Adjacent tissues in the human body can be * distinguished...
java code ============ public class BankAccount { private String accountID; private double balance; /** Constructs a...
java code ============ public class BankAccount { private String accountID; private double balance; /** Constructs a bank account with a zero balance @param accountID - ID of the Account */ public BankAccount(String accountID) { balance = 0; this.accountID = accountID; } /** Constructs a bank account with a given balance @param initialBalance the initial balance @param accountID - ID of the Account */ public BankAccount(double initialBalance, String accountID) { this.accountID = accountID; balance = initialBalance; } /** * Returns the...
COMPLETE JAVA CODE public class Point2 { private double x; private double y;    /** *...
COMPLETE JAVA CODE public class Point2 { private double x; private double y;    /** * Create a point with coordinates <code>(0, 0)</code>. */ public Point2() { complete JAVA code this.set(0.0, 0.0); COMPLETE CODE }    /** * Create a point with coordinates <code>(newX, newY)</code>. * * @param newX the x-coordinate of the point * @param newY the y-coordinate of the point */ public Point2(double newX, double newY) { complete Java code this.set(newX, newY); }    /** * Create a...
Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private...
Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private String name;    private double price;    private int bulkQuantity;    private double bulkPrice;    /***    *    * @param name    * @param price    * @param bulkQuantity    * @param bulkPrice    */    public Item(String name, double price, int bulkQuantity, double bulkPrice) {        this.name = name;        this.price = price;        this.bulkQuantity = bulkQuantity;        this.bulkPrice = bulkPrice;   ...
Please add comments to this code! Item Class: import java.text.NumberFormat; public class Item {    private...
Please add comments to this code! Item Class: import java.text.NumberFormat; public class Item {    private String name;    private double price;    private int bulkQuantity;    private double bulkPrice;    /***    *    * @param name    * @param price    * @param bulkQuantity    * @param bulkPrice    */    public Item(String name, double price, int bulkQuantity, double bulkPrice) {        this.name = name;        this.price = price;        this.bulkQuantity = bulkQuantity;        this.bulkPrice = bulkPrice;   ...
public class StringNode { private String item; private StringNode next; } public class StringLL { private...
public class StringNode { private String item; private StringNode next; } public class StringLL { private StringNode head; private int size; public StringLL(){ head = null; size = 0; } public void add(String s){ add(size,s); } public boolean add(int index, String s){ ... } public String remove(int index){ ... } } In the above code add(int index, String s) creates a StringNode and adds it to the linked list at position index, and remove(int index) removes the StringNode at position...
With the code that is being tested is: import java.util.Random; public class GVdate { private int...
With the code that is being tested is: import java.util.Random; public class GVdate { private int month; private int day; private int year; private final int MONTH = 1; private final int DAY = 9; private static Random rand = new Random(); /** * Constructor for objects of class GVDate */ public GVdate() { this.month = rand.nextInt ( MONTH) + 1; this.day = rand.nextInt ( DAY );    } public int getMonth() {return this.month; } public int getDay() {return this.day;...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final ArrayList<ItemOrder> itemOrder;    private double total = 0;    private double discount = 0;    ShoppingCart() {        itemOrder = new ArrayList<>();        total = 0;    }    public void setDiscount(boolean selected) {        if (selected) {            discount = total * .1;        }    }    public double getTotal() {        total = 0;        itemOrder.forEach((order) -> {            total +=...
public class SinglyLikedList {    private class Node{        public int item;        public...
public class SinglyLikedList {    private class Node{        public int item;        public Node next;        public Node(int item, Node next) {            this.item = item;            this.next = next;        }    }       private Node first;    public void addFirst(int a) {        first = new Node(a, first);    } } 1. Write the method add(int item, int position), which takes an item and a position, and...
this is my matlab code for class, my professor commented "why is the eps an input...
this is my matlab code for class, my professor commented "why is the eps an input when it is set inside the function and not specified as a variable? how do i fix? function[] = () %Declare Global Variables global KS; global KC; KC = 0; KS = 0; End = 0; while (End == 0) choice = questdlg('Choose a function', ... 'Fuction Menu', ... 'A','B','B'); switch choice; case 'A' Program = 'Start'; while strcmp(Program,'Start'); Choice = menu('Enter the Trigonometric...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT