Can someone tell me whats wrong with my code?

Discussion in 'Mac Programming' started by NT1440, Oct 30, 2008.

  1. NT1440 macrumors G4

    NT1440

    Joined:
    May 18, 2008
    Location:
    Hartford, CT
    #1
    Hey guys, I'm in an AP Java class that unfortuneatly ive fallen behind in. I'm trying to answer a question:

    write a method (method1) that uses a for loop to print out all the numbers between "a" and "b" (inclusive) that are divisible by "c". It returns nothing.

    The beginning of my for loop is"

    for (int i = a; i<= b; i++)

    I cant really figure out what I have to do though. I realize that a/c has to equal an int.

    Any tips?
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    to see if x is evenly divisible by y, you can see if x modulus y is 0. I think in java % is the modulus operator. The rest should be pretty self explanatory.

    -Lee
     
  3. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #3
    You haven't written any code. That would be the root problem. I would suggest looking at the modulus operator (%), but I'm not about to write your homework for you.
     
  4. NT1440 thread starter macrumors G4

    NT1440

    Joined:
    May 18, 2008
    Location:
    Hartford, CT
    #4
    robbie, who the hell was asking for anyone to write code? I asked for TIPS, one such being the modulus operator.

    Please dont assume that every person asking for advice is asking people to write code for them.

    edit: thanks lee
     
  5. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #5
    And that's what I gave you. Clearly I won't bother next time.
     
  6. iShater macrumors 604

    iShater

    Joined:
    Aug 13, 2002
    Location:
    Chicagoland
    #6
    So you have your loops, now you need a condition inside it that will check that the remainder for i/c is a whole number. Use the % modulus operator to get a 0.

    Come on, spend less time in the PRSI and focus on class. THAT is more important!

    GO! now! :p
     
  7. NT1440 thread starter macrumors G4

    NT1440

    Joined:
    May 18, 2008
    Location:
    Hartford, CT
    #7
    Lol yes, PRSI is a big reason why I've fallen so far behind.

    Thanks for the advice!

    Ok, heres a little test i just ran through BlueJ, it appears everything works as intended. Can anyone see any problems I might run into using that code as my answer? (meaning any reasons it would crash/fail)

    public class Modulus
    { int c = 2;
    public void test()
    { for (int i = 1; i <= 8; i++)
    { if (i%c == 0)
    {System.out.println(i);
    }
    }
    }



    }

    note that I had to substitute real numbers in to test if it would work. A starts off as 1, B is 8, and C is 2.
     
  8. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #8
    Code:
    public class Modulus {   
        int c = 2;
        public void test() {   
             for (int i = 1; i <= 8; i++) {   
                if (i%c == 0) {
                   System.out.println(i);
                }
            }
        }
    }
    
    Formatted so i can read it, and in CODE tags, that looks fine to me. Whoever told you that
    Code:
    block start
    { first line;
      second line;
    
    is the right way to format code should be punished.

    -Lee
     
  9. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #9
    My understanding of the question was that your method would take 3 parameters: a,b and c. I could be wrong though (although the fact that the starting loop they give you contains a and b seems to indicate this may be the case)...
     
  10. NT1440 thread starter macrumors G4

    NT1440

    Joined:
    May 18, 2008
    Location:
    Hartford, CT
    #10
    oh sorry about that, im usually much neater with my coding.
     
  11. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #11
    I believe this to be true; the OP was just posting a test case with fixed values to see if they had the basics down.

    -Lee
     
  12. NT1440 thread starter macrumors G4

    NT1440

    Joined:
    May 18, 2008
    Location:
    Hartford, CT
    #12
    Yea thats what I was trying to do.

    And robbie, im srry that we've rustled feathers, it was not my intention to ask anyone to write code for me, which is why I was rather flustered when you decided to include the "but I'm not about to write code for you" comment. I feel it was rather un-needed. Thanks for the modulus suggestion tho.

    Thanks everyone.
     
  13. iShater macrumors 604

    iShater

    Joined:
    Aug 13, 2002
    Location:
    Chicagoland
    #13
    I agree, the method needs to take 3 inputs, a, b, and c.

    I thought he came up with the loops, not them.

    Also, the method name, I think you are looking for multiples of the number in a range, not the modulus. :D
     
  14. NT1440 thread starter macrumors G4

    NT1440

    Joined:
    May 18, 2008
    Location:
    Hartford, CT
    #14
    lol i have a really hard time when it comes to thinkin up names for my tests.
     
  15. iShater macrumors 604

    iShater

    Joined:
    Aug 13, 2002
    Location:
    Chicagoland
    #15
    Is this HW, or a take-home test? :eek:
     

Share This Page