Dan Bolens

Greenhorn
+ Follow
since Sep 10, 2012
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
2
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Dan Bolens

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.



Well, thanks to this, I finally figured it out. All the numbers count and list correctly, just having a little trouble with my sortByWordCount, but that shouldn't be too hard to figure out.
11 years ago

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.



Nvm. xD Still working through it.
11 years ago

Steve Luke wrote:Show the new code.



The complete current code for the section is as follows:

11 years ago

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.



Right after I posted that I figured that out, but I'm still having trouble how to get that count to increment correctly. I'm either getting a unique word count of 296 (all the words) or 106 (no clue where that comes from). It should be 169 total unique words. After changing up my code to follow the pusedo-code given exactly I have:



However I'm getting 1 unique word again. I think I'm having the most trouble following the line "// add words[i] to uniqueWords[numUniqueWords] and set wordCounts[numUniqueWords] to 1." What am I coding wrong there?
11 years ago
Alright, so I have changed that method to this:



It works for the most parts, except it's just relisting all the words with a "1" next to them and adding a "null" before each word. How can I get this to add up all instances of said word?
11 years ago

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.



Well, after continuing to rearrage code, you would be correct, it no longer loops at all and I get an output of 0.

EDIT: Would I need to somehow re-add the scanner to this method?
11 years ago
So Im working on a project with simple sorting and for some reason I can't get this array working. The problem is occuring in my countUniqueWordOccurrences method at the bottom. The pseudo-code is written below the method. I'm assuming there is something wrong with one of my for loops, but I'm not sure what it is (I need to review my loops). The output says there is 1 unique word.

11 years ago

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.



  • I do plan on reworking the code, but for time sake (this should be finished tomorrow), I'm just trying to squeeze out what I can. And doing it with a broken hand doesnt help my speed much.

    I think the problem once again is my loop in FindE.

    And I know the problems for FindD is the while loop. I think the combined levels of stress and working at this all day have made me forget basic Java code. I can't think of what loop to change to. T_T

    Well, after working with a CS Major friend for another hour on this, I think Im pretty much stuck taking whatever grade I'll get. I honestly just can't for the life of me figure out what's wrong or how to fix it. There's just too much wrong with each method still (including my encryption) and 16 hours of programming tons of stuff is enough for today. Thanks for all your help everyone. I'll take all your advice and apply it towards future programs. :D
    11 years ago

    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.



    Well, whatever I just changed in mine seems to work just fine. Thanks for the help so far. :D The problem I have with the findE loop is that is goes to whatever PhiPQ equals - 1. For instance, it should be stopping at 11 when P = 109 and Q = 71.

    The code we were given is:

    // Generate an E with the following properties:
    // 1. 1 < E < phiPQ
    // 2. E and phiPQ are relatively prime
    // relatively prime means that gcd(E, phiPQ) == 1
    // 3. There may be several candidates for E, so pick the smallest one
    // (for consistency with the rest of the class -- there is no
    // theoretical reason for this constraint)

    Along with



    11 years ago

    Paul Clapham wrote:



    Well, getting rid of the return just throws the program into an infinite loop, and I obviously cant remove the while... I knew it was one of these lines, but getting rid of one or the other just causes more problems.

    Oh, now I see. I remove it and a conflict occurs between

    while (i < randomNumber - 1)

    and

    if (i >= randomNumber - 1)


    GOT IT!!! n :D Now on to the findE problem...
    11 years ago

    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".



    *shoot self in foot* I'm assuming it ha something to do with the while (true) but I honestly can't see it. I know I have this problem, a problem with findE, and a problem with my decryption. It probably makes the most sense to just start over now... Between my laptop screen breaking (Im working on my TV) and breaking a hand, doing well in class has just been near impossible this last week. T_T
    11 years ago

    Not that loop. This one:



    The "fakecode" we were given is:

    Do until a prime is found
    randomly select a number between 3 and 128
    loop from i equal to 2 to P - 1 and increment i by one each time
    if P mod i is zero
    break; // it isn't prime, no need to continue
    if i is greater than or equal to P - 1 then P is prime

    I'm not quite sure what I have that's missing... (apologies if it's obvious. I've been staring at this screen for 12 hours now working on different projects.)


    11 years ago

    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



    Oh, I think it has something to do with the loop then... This project is turning into a mess. T_T
    11 years ago

    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.



    I actually need to keep d as a class thought because I use it gain in the encryptMsg method. Now e just outputs as zero. Also, For the encrypt and decrypt message sections, Im pretty positive that the encrypt method is correct ( I cant test it yet because of the e problem), but I can tell that my decrypt message is completely wrong starting with . What's the easiest way to reverse the encryption method?

    And yes, it does in fact only pick primes.
    11 years ago

    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?



    Alright, I think I figured it out, but that section with e is giving me problems. Whem assigning p to 109 and q to 71, I should be getting e = 11, but i get e = 7559 and (PhiPQ is 7560, don't know if that's relevant).


    11 years ago