Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why use the c.incremen(); in the Ex_1_1_27a.java? #3

Open
ghost opened this issue Jul 26, 2015 · 1 comment
Open

Why use the c.incremen(); in the Ex_1_1_27a.java? #3

ghost opened this issue Jul 26, 2015 · 1 comment

Comments

@ghost
Copy link

ghost commented Jul 26, 2015

In the Ex_1_1_27a.java binomial() function, I test the code,but there is a error :

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method incremen() is undefined for the type Counter

In your program, the incremen() is undefined, so, how can the this program run successfully?

I know the Counter c is used to cumulative the number of recursive calls, but can't understand why use the function c.incremen().

I think now that c is used to cumulative the number of recursive calls, I use this program:

public static double binomial( int n, int k, double p, int c) {
        if ( n == 0 && k == 0) {
            return 1.0;
        }
        if ( n < 0 || k < 0) {
            return 0.0;
        }

        c++;  

        return (1.0 - p) * binomial( n - 1, k, p, c) + p * binomial( n - 1, k - 1, p, c);
    }
    public static void main(String[] args) {
        int n = Integer.parseInt(args[0]),
            k = Integer.parseInt(args[1]);
        double p = Double.parseDouble(args[2]);

        int c = 0;

        double b = binomial(n, k, p, c);

        StdOut.println(b);
        StdOut.println(c);
    }

I test 10 5 0.5, but I get the result: 0.24609375 0.

I hope to help me Solve this doubt,thanks a lot !

@Akshay199456
Copy link

You received the incremen() error because there does not exist any such method in the file Counter.java . However, it does contain the method increment() which is used to increment the value of the instance variable count used to measure the number of recursive calls. Therefore, the command
c.increment()
increments the value of the instance variable count associated with the object c.

The result that you obtained for b is correct but not for c. This is because the line

StdOut.println(c); in your code
prints out the local value of c in your main() method rather than the value of c local to the binomial() method even though it is the latter value that you want. Since, the value of c in your main method has been set to 0, that is the value which is displayed.

On the other hand, the line
StdOut.println(c); in the original code

calls the toString() method associated with the Counter object c which prints the current value of count, which is continuously incremented and stored in the Counter object c. Since this value is 1223 at the end of the recursive calls, that is the value displayed

Hope that helps...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant