Jeff Verdegan wrote:
Steve Luke wrote:
It seems like you are taking shots in the dark. You need to stop coding and start writing the algorithm out on paper. Not just in pseudo-code, but a step-by-step execution so you understand what is happening.
I cannot agree strongly enough with this.
Write the steps down in English. Very simple, precise steps. When you go to translate those steps to Java, make sure you know what each piece of the Java you write is for, and how it corresponds to your English description. If you're not completely sure how a given bit of Java will behave, stop right there. Create a new Java program that ONLY demonstrates what that Java does. Only once you understand it should you put it in your real program and move on to the next step. If you have just 2 or 3 lines of Java code where you go, "I think this will do something like this, but I'm not really sure," then when you run your program and something goes wrong, it will get exponentially harder to figure out what the source of the problem is.
Steve Luke wrote:What role does j play in that code (what is it supposed to do)? How does it change throughout the execution of the method? Why do you compare it to numUniqueWords? When you realize how j behaves through the method, you should understand how come you get exactly 1 unique word count.
It seems like you are taking shots in the dark. You need to stop coding and start writing the algorithm out on paper. Not just in pseudo-code, but a step-by-step execution so you understand what is happening.
Steve Luke wrote:Show the new code.
Steve Luke wrote:What do you think line 11 of your last code snippet does?
The answer to what it actually does explains why it puts 'null' in front of each word. Then, to explain why each word has a count of 1, is because you assign 1 to it in line 12:
Since you assign 1 to every index in the array, that is what you get out.
fred rosenberger wrote:Your for-loops in the countUniqueWordOccurrences method look odd...
Generally, you would do
I'm not sure what you are trying to do, but I would think what you have wouldn't loop at all.
I am a big proponent of putting in "System.out.println()" statements to see what the code is really doing.
Jeff Verdegan wrote:
Dan Bolens wrote:
Paul Clapham wrote:But in my opinion you've got too much code there, it's doing two different things, and that's leading to confusion. I would have written the loop like this:
[snip]
The code you have is partly for finding if a number is prime, and partly for repeatedly trying random numbers until you get one you like. So you should split that code into two parts.
Well, whatever I just changed in mine seems to work just fine.
He's telling you exactly the same thing I did. You might want to pause to think if we might know what we're talking about and maybe have a good reason for suggesting this. Sure, your code works now, but...
It will be harder for your teacher to follow your logic. Making it difficult for your teacher to evaluate your work is not generally in your own best interest. If you find you have to modify it, it's a mess, and it will be harder to modify than if you had well-defined methods that did one well-defined job each, even more so as time passes. If you need to test for primeness in a future project, you won't have a method you can just grab and re-use. You'll have to write it from scratch, or figure out what part of that big blob of code is relevant for the prime test. Most importantly, you're writing off a very bad habit as not a big deal. You'll be likely to continue with that habit in the future. Learning about loops is much less important right now than developing the habit of breaking problems down into small, independent pieces.
Paul Clapham wrote:Yes, it looks like you're on the right track. But in my opinion you've got too much code there, it's doing two different things, and that's leading to confusion. I would have written the loop like this:
The code you have is partly for finding if a number is prime, and partly for repeatedly trying random numbers until you get one you like. So you should split that code into two parts. What I posted there is code for repeatedly trying random numbers until you get one you like. Now you just need code which tells you if a number is prime.
Paul Clapham wrote:
Paul Clapham wrote:It's not that you are missing anything. On the contrary, you have an extra line of code which doesn't match your "fakecode".
Specifically, it's the line of code in your real code which comes after your implementation of the "fakecode".
Not that loop. This one:
Tony Docherty wrote:
And yes, it does in fact only pick primes
Really, when I called the method 20 times I got:
75
73
57
39
119
112
90
55
38
80
72
14
106
93
47
52
127
31
91
76
Paul Clapham wrote:I would have just called the local variable in your findD() method "d", since that's how you are using it. That class-level variable "d"... your findD() method assumes it's initialized to zero. You're better off not having it all, and just letting findD() do its job and find the right value.
And does your pickPrime() method actually return only primes? My impression is that it will always return the first random number it picks, prime or not.
fred rosenberger wrote:Do you know how to calculate PQ?
Do you know how to calculate DE - 1?
Do you know how to calculate phiPQ?
Do you know how to test, in general, if A is evenly divisible by B?