0

So getArea calls getHeight which calls getArea and creates a never ending loop. I'm being graded on writing as little code as possible and reusing as many functions as possible.

Is there anyway I could get this working with the function calls rather then just substituting the forumula for getArea into getHeight where getArea is called?

//returns the area of a trapezoid
public double getArea(){
    double height = getHeight();
    double area = (.5 * ((getPoint(3).getX() - getPoint(4).getX()) + (getPoint(2).getX() - getPoint(1).getX()))) * height;
    return area;
}

//returns the height of a trapezoid
public double getHeight(){
    double area = getArea();
    double height = (2 * area) / (((getPoint(3).getX() - getPoint(4).getX()) + (getPoint(2).getX() - getPoint(1).getX())));
    return height;
}
9
  • 1
    Have one of the functions take the height / area as input, or just use the sin/cos builtins to do some basic trig (since you know the base and top height) Oct 2, 2015 at 0:23
  • I have to use the function headers provided which were public double getHeight() and public double getArea(), so I can't make one function take the other as a parameter. I'm willing to substitute the formula into the function to avoid the recursion but even then I'm still at a lost because each function relies on the other. Any help on how I would do this?
    – Basic
    Oct 2, 2015 at 1:05
  • I believe you're confusing the principle of recursion with simply calling one method from within another? Just use trig in the case of not being able to take extra Params. Oct 2, 2015 at 1:09
  • I'll write you a full answer in a moment. Oct 2, 2015 at 1:10
  • Please do I'm at a complete loss. Reason I thought it was a recursion issue is because the error: Exception in thread "main" java.lang.StackOverflowError Is recursion when a function calls itself? So that wouldn't apply for two functions calling on each other infinitely? Whoops. Still new to the programming grammar.
    – Basic
    Oct 2, 2015 at 1:14

1 Answer 1

0

For review:

  1. Circular

    void a() {
        b();
    }
    void b() {
        a();
    }
    
  2. Recursive

    bool binarySearch(int* list, int searchFor, int size) {     
        if (size == 1 && list[0] != searchFor)
            return false;
    
        int split = size / 2;
    
        if (list[split] == searchFor) {
            return true;
        } else if (list[split] > searchFor) {
            binarySearch(list, searchFor, split);
        } else {
            binarySearch(list[split], searchFor, split);
        }
    
  3. Recursive infinite/non-terminating

    void a() {
        a();
    }
    

On with the "answer":

enter image description here

So I'd need more information to give you the simplest answer but here is at least one options; reliant on you having x and y coordinates (anyone else can feel free to add).

Use points to find height:

You seem to be able to call .x() to get the x-values so if the y-values are present just find the height by taking the difference of each of the point pairs (A&D, D&C, C&B and B&A).

For each of these, you will have a vector m (m.x, m.y) = (a.x, a.y) - (b.x, b.y) which you can find the magnitude of magnitude = sqrt(m.x * m.x + m.y * m.y).

Now you have all four side lengths you can compute the height from that like so (a&b are top and bottom, c&d are sides): enter image description here

1
  • Y-values would only work if you have common Ys within the points. If the trapezoid is rotated to not be parallel to an axis then that wouldn't work. There seem to be a lot of unanswered pieces here. Are the points in a given guaranteed order or do you have to figure out what they are? At a minimum you need to figure out the length of the lines between the points I would think
    – Michael
    Oct 2, 2015 at 1:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.