0

What im trying to do is have the program to run the first set of numbers. Then Use output as 'X'. So it looks like this Tnew = Told - m(Told - Tair) Told is the last equations Tnew.

ex. new = old +6
old = 7
new = 13
new = 13 +6
repeat 

Heres the code ; package heatloss;

/**
 *
 * @author Eric Franzen
 */
public class HeatLoss {

    public static void heatloss(double x, double m, double a) {
        double heatloss = x - m * (x - a);

        if (x < 55) {
            System.out.println("potatoe too cold");
        }
        else {

            System.out.println(heatloss);
            heatloss(x,m,a);
            x = heatloss;
        }
    }


    public static void main(String[] args) {

        heatloss(80, .01515 ,25); 

    }  

}

Okay so I changed the code to look like this:

public static double heatloss(double x, double m, double a) {
    double heatloss = x - m * (x - a);

    if (x < 55) {
        System.out.println("potatoe too cold");
        return heatloss;
    }
    else {
        System.out.println(heatloss);
        x = heatloss(x,m,a);
        return heatloss;
    }
}

But I get an error at line

x = heatloss(x,m,a);

That "the Assigned value is never used." Im not really sure what that means? X is clearly used in the program.

0

3 Answers 3

2

In order for one invocation of a recursive method to access data inside of another (for example, data stored in local variables), you need to somehow communicate that data out of the second recursive call into the first. In this case, you might want to consider having the heatloss method return the value of the heatloss local variable. For example:

public static double heatloss(double x, double m, double a) {
    double heatloss = x - m * (x - a);

    if (x < 55) {
        System.out.println("potatoe too cold");
        return /* put something here */
    }
    else {
        System.out.println(heatloss);
        x = heatloss(x,m,a);
        return /* put something here */
    }
}

By filling in the appropriate blanks (I'm not sure how you'd do that here, since I'm not familiar with the particular problem you're solving), you should be able to communicate information out of the deeper calls into the higher-up calls.

Hope this helps!

Sign up to request clarification or add additional context in comments.

1 Comment

I adjusted the code to how you recomended. I got a diffrent error which im not sure how to fix. I posted it above.
0
/**
 *
 * @author Eric Franzen
 */
public class HeatLoss {

    public static void heatloss(double x, double m, double a) {
        double heatloss = x - m * (x - a);

        if (x < 55) {
            System.out.println("potatoe too cold");
        }
        else {

            System.out.println(heatloss);
            x = heatloss;
            heatloss(x,m,a);
        }
    }


    public static void main(String[] args) {

        heatloss(80, .01515 ,25); 

    }  
}

1 Comment

In your program u are calling heatloss with the old value of x.I swapped the two statements in ur else clause.Now it returns Potatoe too cold.
0
        heatloss(x,m,a);
        x = heatloss;

You're making the mistake of believing that heatloss the method and heatloss the variable are somehow related. They are totally unrelated. When heatloss the method returns its value you must assign that returned value to something (if indeed you intend to make use of the value).

You should "play computer", and step through, with pencil and paper, the operations that would occur in running your program. This will give you a better understanding of what's going on. It's not magic -- it's just a simple "imperative" language where each step uses the results of what's happened in previous (in time) steps.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.