Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

NT1440

macrumors P6
Original poster
May 18, 2008
15,209
22,565
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?
 
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
 
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.
 
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
 
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

And that's what I gave you. Clearly I won't bother next time.
 
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
 
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

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

oh sorry about that, im usually much neater with my coding.
 
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)...

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

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

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
 
lol i have a really hard time when it comes to thinkin up names for my tests.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.