gEVAL class

Step 1: problem domain

  • Main goal of this class is implementing eval method. But gEVAL is not just a method. 
  • gEVAL is used to implement mathematics equations and operations among them. (y=F(x))
  • Users can define objects from gEVAL and set their equations.
    • For example:  gEVAL x=new gEVAL(“-3*x^2-sin(x)“);
    • For another example: gEVAL y=new gEVAL(“log(x*sin(2*x))-exp(x^2-1) + 2*x^12-10-x+sqr(x)*x^3“);
  • Then users can call mathematics operations such as: *, /, -, +.
  • The main method of this class is getF(double x) that is similar to eval method. By this method users want to evaluate given equation for x.
  • At the first implementation, it is better that this class be implemented without knowledge of Data Structures course.
  • In this class students can use Java Math Class methods. But implementing them have extra points. (similar to BigMath Class and students can define gMath class exactly similar to Java Math Class)

 

Step 2: Public Interface

2-1- Constructor Methods

public gEVAL();  // set to 0
public gEVAL(String e);
public gEVAL(gEVAL e);

2-2- Setter Methods

public void Set(String e);
public void Set(gEVAL e);

2-3- Getter Methods

Nothing

2-4- Functionalities of class (Methods)

To overcome the complexity of getF method, students should follow below steps:

Step 0: Replace all ‘x’ notations in the equation with given number named it strEVAL. Then follow other steps.

Step 1: Implement a private method named getF1. Suppose that strEVAL is just integer numbers and ‘+’. For example “31+4+132″

Step 2: getF2. Suppose that strEVAL is just integer numbers and ‘+ , -‘. For example “-31-423+132-1″

Step 3: getF3. Suppose that strEVAL includes integer and double numbers and ‘+,-‘. For example “-31.002+4-0.98+132-9.00123″

Step 4: getF4. strEVAL includes elements like getF3 and “(,)”. For example “-1.0004-(-1.43-4+0.001)+3.201-(2.1-0.00001)”

Step 5: getF5. strEVAL includes elements like getF4 and “*, /”. For example “-1.0004/(-1.43*4+0.001)+3.201*(2.1-0.00001)”

Step 6: getF6. strEVAL includes elements like getF5 and “^”. For example “-1.0004/(-1.43*4^1.2+0.001)+3.201*(2.1^0.01-2)”

Step 7: getF7. strEVAL includes elements like getF6 and math functions. For example “log(x*sin(2*x))-exp(x^2-1) + 2*x^1.2-10-x+sqr(x)*x^3“. (replace any doubles into x)

// to get value of equation F(x) in specific x (eval method)
public double getF(double x);

// to get roots of equation, (which Xes that F(x)=0)
// to do this, there are different ways such as 
// 1- Newton: http://en.wikipedia.org/wiki/Newton's_method
// 2- Bisection: http://en.wikipedia.org/wiki/Bisection_method
// 3- Secant: http://en.wikipedia.org/wiki/Secant_method
// and etc
public double[] getRoots(int iMethod)

// this=a+b;
public void SetSum(gEVAL a, gEVAL b);

// this=a-b;
public void SetMinus(gEVAL a, gEVAL b);

// this=a*b;
public void SetMultiple(gEVAL a, gEVAL b);

// this=a/b;
public void SetDiv(gEVAL a, gEVAL b);

2-5- Standard Library Methods overloading (such as toString, equals,…)

public String toString();
public int compareTo(gEVAL a);
public boolean equals(Object anObject);
public int hashCode();
public gEVAL clone();

 

Step 3: private variables

Private variable of this class is just a String.

 

Step 4: implementing main classes

Students work !

 

Step 5: useful application

gTotalCALC is application of this class.