0

My code works fine, but this is what is killing me and i cannot find an answer. I cannot pass a string directly from the command line arguments into my constructor, to then be passed to a method (evaluateMethod) called during construction. For some reason, the method is not seeing it as a String. Why is it doing this? The only way i got it to work was to put if/else statements in my main method which call the constructor and pass in arguments. Attached is my code.

/**
 * Program to calculate approximation, actual value, absolute error, and relative error with a definite integral
 * @author c-dub
 *
 */
public class Integrate {
    private String whichWay;
    double upper, lower, deltaX, actual, absoluteError, relativeError;  
    int rect;   

    // Constructor
    Integrate(String str, double a, double b, int n) {
        this.whichWay = str;
        this.lower = a;
        this.upper = b;
        this.rect = n;
        this.deltaX = (b - a) / rect;
        this.actual = (Math.pow(upper, 3) / 3) - (Math.pow(lower, 3) / 3);
        evaluateMethod(whichWay);
        System.out.println("Actual value: " + actual);
        System.out.println("Absolute error: " + absoluteError + ", Relative error: " + relativeError + ".");
    }    

    /**
     * Evaluates all calculations
     * @param whichWay, the string to determine left, right, trapezoid method
     */
    private void evaluateMethod(String whichWay) {
        if(whichWay == "l" || whichWay == "L") {
            System.out.println("Using left hand endpoints, on the integral (" + lower + ", " + upper + ") with "
                                + rect + " rectangles.....");
            System.out.println("The approximate value is: " + integrateLeft() + ".");           
        }
        else if(whichWay == "r" || whichWay == "R") {
            System.out.println("Using right hand endpoints, on the integral (" + lower + ", " + upper + ") with "
                                + rect + " rectangles.....");
            System.out.println("The approximate value is: " + integrateRight() + ".");          
        }
        else if(whichWay == "t" || whichWay == "T") {
            System.out.println("Using the trapezoid method, on the integral (" + lower + ", " + upper + ") with "
                                + rect + " rectangles.....");
            System.out.println("The approximate value is: " + integrateTrapezoid() + ".");          
        }
        else {
            throw new IllegalArgumentException("Bad string input. Please restart program");
        }
    }

    /**
     * Left hand endpoint integration
     * @return the approximate value as a double
     */
    private double integrateLeft() {        
        double sum = 0;
        // First interval to the last interval, depending on number of rectangles
        double lowerAlt = lower;
        for(int i = 0; i < rect; i++) {         
            sum += evaluate(lowerAlt);
            lowerAlt = lowerAlt + deltaX;
        }
        absoluteError = Math.abs(actual - (sum * deltaX));
        relativeError = Math.abs(absoluteError / actual);
        // Multiply entire sum by deltaX
        return Math.abs(sum * deltaX);
    }

    /**
     * Right hand endpoint integration
     * @return the approximate value as a double
     */
    private double integrateRight() {
        double sum = 0;
        // First interval to the last interval, depending on number of rectangles
        double lowerAlt = lower + deltaX;
        for(double i = 0; i < rect; i++) {
            sum += evaluate(lowerAlt);
            lowerAlt = lowerAlt + deltaX;
        }
        absoluteError = Math.abs(actual - (sum * deltaX));
        relativeError = Math.abs(absoluteError / actual);
        // Multiply entire sum by deltaX
        return Math.abs(sum * deltaX);
    }

    /**
     * Trapezoid method of integration
     * @return the approximate value as a double
     */
    private double integrateTrapezoid() {
        double sum = 0;
        // first interval after the lower bound, to the last interval before upper bound depending on # of rectangles
        double lowerAlt = lower + deltaX;
        for(int i = 1; i < rect; i++) {
            sum += evaluate(lowerAlt) * 2;
            lowerAlt = lowerAlt + deltaX;
        }
        // Now pass the lower and upper values into the function and add to total sum
        sum += evaluate(lower) + evaluate(upper);
        absoluteError = Math.abs(actual - (sum * (deltaX / 2)));
        relativeError = Math.abs(absoluteError / actual); 
        // Mulitply calculation by deltaX / 2
        return Math.abs(sum * (deltaX / 2));
    }

    /**
     * Method to define the actual function
     * @param x, function input value
     * @return the function output value f(x)
     */
    private double evaluate(double x) {
        //Please enter your desired function here:
        return Math.pow(x, 2);
    }


    /**
     * Main method
     * @param args, the command line arguments
     */
    public static void main(String[] args) {
        String str;
        double a = Double.parseDouble(args[1]);
        double b = Double.parseDouble(args[2]);
        int n = Integer.parseInt(args[3]);
        if(args[0].equals("l") || args[0].equals("L")) {
          new Integrate("l", a, b, n);
        }
        else if(args[0].equals("r") || args[0].equals("R")) {
          new Integrate("r", a, b, n);
        }
        else if(args[0].equals("t") || args[0].equals("T")) {
          new Integrate("t", a, b, n);
        }    
      }
}

Inside my evaluateMethod() method, it checks for a string, and even though the command args was passing in a legitimate string letter (not a char), it wasnt seeing it as a string and was throwing the exception. Thanks

Chris Wilson
  • 135
  • 3
  • 16

2 Answers2

0

In your evaluateMethod() change whichWay == "l" with whichWay.equals("l") like in the main():

private void evaluateMethod(String whichWay) {
        if(whichWay.equals("l") || whichWay.equals("L")) {
            System.out.println("Using left hand endpoints, on the integral (" + lower + ", " + upper + ") with "
                                + rect + " rectangles.....");
            System.out.println("The approximate value is: " + integrateLeft() + ".");           
        }
        else if(whichWay.equals("r") || whichWay.equals("R")) {
            System.out.println("Using right hand endpoints, on the integral (" + lower + ", " + upper + ") with "
                                + rect + " rectangles.....");
            System.out.println("The approximate value is: " + integrateRight() + ".");          
        }
        else if(whichWay.equals("t") || whichWay.equals("T")) {
            System.out.println("Using the trapezoid method, on the integral (" + lower + ", " + upper + ") with "
                                + rect + " rectangles.....");
            System.out.println("The approximate value is: " + integrateTrapezoid() + ".");          
        }
        else {
            throw new IllegalArgumentException("Bad string input. Please restart program");
        }
    }

Because to compare String value you have to use this function. (see documentation)

== tests for reference equality

.equals() tests for value equality

Simone Pessotto
  • 1,561
  • 1
  • 15
  • 19
  • thanks everyone. its stupid little mistakes like this one that i want to prevent. Theres no need to have if/else statements in my main method anymore. Appreciate the help. – Chris Wilson Oct 24 '15 at 22:40
0

In the evaluateMethod() method you are using == when you should be using .equals()

For example

if(whichWay == "l" || whichWay == "L")

should be:

if(whichWay.equals("l") || whichWay.equals("L")

replace all your string comparisons with .equals("[Letter]") and it should work.

Interestingly, in NetBeansIDE your program runs fine. It just gives a warning about the above mistake.

Subito
  • 36
  • 1
  • 5