Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can someone please write the code for this java project as simple as you want it

ID: 3806147 • Letter: C

Question

Can someone please write the code for this java project as simple as you want it to be with explanation comments:

The website for the API is:

https://www.cs.umd.edu/class/spring2017/cmsc131/Spring17Proj4/doc/index.html

The project is:

ComplexNumber class

You must implement all of the data members and methods described below.  You may NOT add any instance variables or static variables to this class other than those described below.  You may add methods of your own, as long as they are private.  NOTE: Not all of the methods described below are required for drawing the Mandelbrot Set, but they must all be implemented correctly as part of this assignment.  The class you are writing is a very general class that could be of use in a wide variety of projects, not just for drawing the Mandelbrot Set.

Private Instance Variables

The class will have exactly two instance variables. THESE VARIABLES MUST BE DECLARED PRIVATE AND FINAL! The complex number class you are implementing is an immutable class.

private final MyDouble real;

private final MyDouble imag;

These variables represent the state of a complex number. For example, if the current object is supposed to represent the complex number 17.2 + 3.7i, then the value of real would be 17.2, and the value of imag would be 3.7. Note that we have no intention of "storing" the number i -- there is no reason to! The variables are final because once they are set in the constructors, there is no reason to ever change them. (Instance variables that are final can be initialized from within a constructor, but nowhere else.)

Public Constructors

A standard constructor that takes two parameters (both MyDoubles) representing the real and imaginary components (in that order) that are desired for the ComplexNumber being constructed. The data members real and imag are to be initialized with these values.

A constructor that takes one parameter (a MyDouble) representing the real component that is desired for the ComplexNumber being constructed. The data member real is to be initialized with that value, but the imag component will be set to zero.

A copy constructor.

Public Instance Methods

getReal -- A simple "getter" for the value of the real data member. (This one has a funny name -- GET REAL!)

getImag -- A simple "getter" for the value of the imag data member.

add -- this method takes one parameter (a ComplexNumber). It will return a ComplexNumber that is equal to the sum of the current object and the parameter. (Do not modify the current object.)

subtract -- this method takes one parameter (a ComplexNumber). It will return a ComplexNumber that is computed by subtracting the value of parameter from the current object. (Do not modify the current object.)

multiply -- this method takes one parameter (a ComplexNumber). It will return a ComplexNumber that represents the product of the current object and the parameter. (Do not modify the current object.)

divide -- this method takes one parameter (a ComplexNumber). It will return the quotient computed by dividing the current object by the parameter. (Do not modify the current object.)

equals -- returns true if both fields match. This should be implemented as the .equals has been implemented for other classes during lecture. Use a parameter of type ComplexNumber for this one, even if you have seen this method implemented with type Object sometimes. Unfortunately, we are implementing equals incorrectly for now (we'll learn the right way later in the semester) and so the submit server will display a "FindBugs" warning about it. Please just ignore this warning.

compareTo -- this method takes one parameter (a ComplexNumber) and returns an int. It will compare the norm of the current object with the norm of the parameter. (See the norm method, below.) If the norms are equal, this method returns 0; if the norm of the current object is less than the norm of the parameter, this method returns -1; if the norm of the current object is greater than the norm of the parameter, this method returns 1.

toString -- there are four cases here, depending on the signs of the real and imaginary components of the current object. See examples below, which illustrate correct return values for the toString method in each case. Note that there are never any spaces in the value returned. Note there is always a real portion added to the string and an imaginary portion added to the string even if the value in that portion is 0. Note the numeric portion is always explicitly included even if the value is 1.

"6.7+2.3i"

"6.7-2.3i"

"-6.7+2.3i"

"-6.7-2.3i"

Public Static Methods

norm -- this method takes one parameter (a ComplexNumber) and returns a MyDouble object representing the norm of the complex number. Recall that for the complex number a + bi, the norm is equal to sqrt(a2 + b2)  [The square root of a squared plus b squared.] Note: You may wonder why we are making this method static! That is a good question. We have our reasons...

parseComplexNumber -- this method takes one parameter (a String) and returns a ComplexNumber. The parameter is a String that represents a complex number, such as "5.9 + 73.44i" or "-2.35 - 6.5i". There could be any number of spaces in the beginning, at the end, before the 'i', and surrounding the '+' and '-' characters. For example, the following Strings could be passed to this method: "   -   2.7   +   5.9   i" or possibly: " 79.3     - 5   i" or even: "-1.25-3.469i". The method will parse the String and return a ComplexNumber that represents the value described by the String. You may assume that the String being passed to this method is correctly formatted.

Note: Your method is not expected to handle parameters like "3.2" or "-7.99i" - we would expect those to be passed to this method as something like "3.2 + 0i" or "0 - 7.99i", respectively.   Also, your method is not expected to handle parameters like "+3.2+i" - we would expect something like "3.2+1i".

Hint: You will probably want to use some of the methods in the Java String class, so you should probably review the online documentation for the Java String class. Also, you might find it useful to use the static method Double.parseDouble. (See the online documentation for the Java Double class.)

MandelbrotTools class

This class contains just a couple of static methods, described below. You may add other methods, if you wish, as long as they are static and private.  You may not add any instance variables or static variables to this class.

Public Static Methods

isBig -- this method takes one parameter, a ComplexNumber, and will return type boolean. It will do a computation that is very similar to the norm method from the ComplexNumber class. We are not using the norm method here, because finding square roots slows the program down. For the complex number a + bi, this method will compute a2+ b2 , and will compare this value with the static variable called Controller.DIVERGENCE_BOUNDARY, which is built-in to one of the classes we have provided. If a2 + b2is greater than the value of the variable named DIVERGENCE_BOUNDARY, then the method returns true. Otherwise it returns false.

divergence -- this method takes one parameter, a ComplexNumber which we will call z0. The method will return type int. This method calculates a sequence of complex numbers z1, z2,z3,z4, etc. as follows:

z1 = z02 + z0           

z2 = z12 + z0

z3 = z22 + z0

z4 = z32 + z0

etc.

These values will be computed one by one. After each value is computed, test to see whether or not it isBig (using the previous method). There are two ways for this method to terminate:

As soon as one of the terms in the sequence isBig, exit the method immediately. In this case, the return value will be the index of the term that was too big. For example, if terms z0 through z34 are all not too big, but term z35 is too big, then you should return 35.  

There is a static variable of type int that we have provided called Controller.LIMIT. Suppose that Controller.LIMIT is 255, which is typical. In that case, if you get all the way through the first 256 terms  z0,z1, z2,z3,z4, ... z254,z255 and none of them isBig then the method should terminate, returning -1.

The full project can be found using the link:

https://www.cs.umd.edu/class/spring2017/cmsc131/Spring17Proj4/Proj4.html

Explanation / Answer

public class MartianCubic {

   private final DoubleWithAppx a ;

   private final DoubleWithAppx b;

   private final DoubleWithAppx c;

   private final DoubleWithAppx d;

  

   public MartianCubic() {

       a = new DoubleWithAppx(0);

       b = new DoubleWithAppx(0);

       c = new DoubleWithAppx(0);

       d = new DoubleWithAppx(0);

      

   }

   public MartianCubic(DoubleWithAppx dIn) {

       d = dIn;

       a = new DoubleWithAppx(0);

       b = new DoubleWithAppx(0);

       c = new DoubleWithAppx(0);

   }

   public MartianCubic(DoubleWithAppx cIn, DoubleWithAppx dIn) {

       c = cIn;

       d = dIn;

       a = new DoubleWithAppx(0);

       b = new DoubleWithAppx(0);

   }

   public MartianCubic(DoubleWithAppx bIn, DoubleWithAppx cIn, DoubleWithAppx dIn) {      

       this.b = bIn;

       this.c = cIn;

       this.d = dIn;

       a = new DoubleWithAppx(0);

   }

  

   public MartianCubic(DoubleWithAppx aIn, DoubleWithAppx bIn, DoubleWithAppx cIn, DoubleWithAppx dIn) {

       this.a = aIn;

       this.b = bIn;

       this.c = cIn;

       this.d = dIn;

   }

  

   public MartianCubic(MartianCubic other) {

       this(

               other.getA(),

               other.getB(),

               other.getC(),

               other.getD()

               );  

   }

  

   public DoubleWithAppx getA() {

       return a;

   }

  

   public DoubleWithAppx getB() {

       return b;

   }

  

   public DoubleWithAppx getC() {

       return c;

   }

  

   public DoubleWithAppx getD() {

       return d;

   }

public MartianCubic add(MartianCubic martianCubicIn) {

@Override

   public boolean equals (Object other) {

       if (other == null) {

           return false;

       }

       else if (this.getClass()!=other.getClass()) {

           return false;

       }

       else {

           MartianCubic casted = (MartianCubic)other;

           return (

                   a.equals(casted.a) &&

                   b.equals(casted.b) &&

                   c.equals(casted.c) &&

                   d.equals(casted.d)

           );

       }

   }

      

   }