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

NT1440

macrumors Pentium
Original poster
May 18, 2008
15,093
22,159
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?
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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.
 

NT1440

macrumors Pentium
Original poster
May 18, 2008
15,093
22,159
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
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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.
 

iShater

macrumors 604
Aug 13, 2002
7,027
470
Chicagoland
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
 

NT1440

macrumors Pentium
Original poster
May 18, 2008
15,093
22,159
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.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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)...
 

NT1440

macrumors Pentium
Original poster
May 18, 2008
15,093
22,159
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.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 

NT1440

macrumors Pentium
Original poster
May 18, 2008
15,093
22,159
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.
 

iShater

macrumors 604
Aug 13, 2002
7,027
470
Chicagoland
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
 

NT1440

macrumors Pentium
Original poster
May 18, 2008
15,093
22,159
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.