Java

Moderators: zibadian
Number of threads: 7818
Number of posts: 18218

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
I need help!!! I need to make a FactorGenerator Posted by mabs on 20 Mar 2006 at 12:16 AM
Hi,
I have a program to write for a chapter on loops and I have no clue where to start. I have to write a program that take in an int and prints the factors of the numbers.

example:

Please enter a number : 12
1
3
4
12

If you guys can help i would really appreciate it. Thanks.
Report
Re: I need help!!! I need to make a FactorGenerator Posted by zibadian on 20 Mar 2006 at 1:58 AM
: Hi,
: I have a program to write for a chapter on loops and I have no clue where to start. I have to write a program that take in an int and prints the factors of the numbers.
:
: example:
:
: Please enter a number : 12
: 1
: 3
: 4
: 12
:
: If you guys can help i would really appreciate it. Thanks.
:
Use a for-loop to loop through the numbers until the loop reaches the number. You can check if a certain number is a factor of the entered int, by using the %-operator. The %-operator gives you the remainder of the division. Thus if a certain number is a factor of the entered int, then the remainder will be equal to 0.
Report
Re: I need help!!! I need to make a FactorGenerator Posted by mabs on 20 Mar 2006 at 2:51 AM
: : Hi,
: : I have a program to write for a chapter on loops and I have no clue where to start. I have to write a program that take in an int and prints the factors of the numbers.
: :
: : example:
: :
: : Please enter a number : 12
: : 1
: : 3
: : 4
: : 12
: :
: : If you guys can help i would really appreciate it. Thanks.
: :
: Use a for-loop to loop through the numbers until the loop reaches the number. You can check if a certain number is a factor of the entered int, by using the %-operator. The %-operator gives you the remainder of the division. Thus if a certain number is a factor of the entered int, then the remainder will be equal to 0.
:
this is what i got so far but it still doesn't work. i can't get the any number to print except for 0;
my book says i have to use the nextFactor method and the hasNextFactor method and that it has to print this way
example:
150
2
3
5
5
if you could help me with this I would really appreciate it. Thank guys.

public class FactorGenerator
{
public FactorGenerator(int aNum)
{

num = aNum;
divBy = 1;
}


public int nextFactor()
{
while(divBy < num)
{
divBy++;
factor = num % divBy;

}
return factor;
}
public void hasNextFactor()
{
for (int i = 1; i < num; i++)
{
factor = num % divBy;

}

}



private int num;
private int divBy;
public int factor;
}

Report
Re: I need help!!! I need to make a FactorGenerator Posted by zibadian on 20 Mar 2006 at 2:53 AM
: : : Hi,
: : : I have a program to write for a chapter on loops and I have no clue where to start. I have to write a program that take in an int and prints the factors of the numbers.
: : :
: : : example:
: : :
: : : Please enter a number : 12
: : : 1
: : : 3
: : : 4
: : : 12
: : :
: : : If you guys can help i would really appreciate it. Thanks.
: : :
: : Use a for-loop to loop through the numbers until the loop reaches the number. You can check if a certain number is a factor of the entered int, by using the %-operator. The %-operator gives you the remainder of the division. Thus if a certain number is a factor of the entered int, then the remainder will be equal to 0.
: :
: this is what i got so far but it still doesn't work. i can't get the any number to print except for 0;
: my book says i have to use the nextFactor method and the hasNextFactor method and that it has to print this way
: example:
: 150
: 2
: 3
: 5
: 5
: if you could help me with this I would really appreciate it. Thank guys.
:
: public class FactorGenerator
: {
: public FactorGenerator(int aNum)
: {
:
: num = aNum;
: divBy = 1;
: }
:
:
: public int nextFactor()
: {
: while(divBy < num)
: {
: divBy++;
: factor = num % divBy;
:
: }
: return factor;
: }
: public void hasNextFactor()
: {
: for (int i = 1; i < num; i++)
: {
: factor = num % divBy;
:
: }
:
: }
:
:
:
: private int num;
: private int divBy;
: public int factor;
: }
:
:
You don't need the divBy. It is enough to divide by the i variable. That variable already holds the divisor.
Report
Re: I need help!!! I need to make a FactorGenerator Posted by codebytes on 20 Mar 2006 at 12:16 PM
hmmmmm. Your code is a little confused. Where is your main() method ? The following code produces the desired output and uses your method and variable names with some modification. When you have compiled it type : java FactorGenerator 12
to produce all the factors of 12. Replace 12 with any other number you wish to obtain the factors of that number.

public class FactorGenerator
{
public FactorGenerator(int aNum)
{

num = aNum;
divBy = 1;

}

public void nextFactor()
{

factor = num % divBy;
divBy++;

}

public void hasNextFactor()
{

for (int i = 1; i <= num; i++)
{

nextFactor();
if (factor == 0 ) System.out.println( i );

}

}

public static void main (String[] args)
{

FactorGenerator fg = new FactorGenerator ( Integer.parseInt ( args[0] ));
System.out.println ( fg.num );
fg.hasNextFactor();

}

private int num;
private int divBy;
public int factor;
}


Report
Re: I need help!!! I need to make a FactorGenerator Posted by mabs on 20 Mar 2006 at 3:19 PM
: hmmmmm. Your code is a little confused. Where is your main() method ? The following code produces the desired output and uses your method and variable names with some modification. When you have compiled it type : java FactorGenerator 12
: to produce all the factors of 12. Replace 12 with any other number you wish to obtain the factors of that number.
:
: public class FactorGenerator
: {
: public FactorGenerator(int aNum)
: {
:
: num = aNum;
: divBy = 1;
:
: }
:
: public void nextFactor()
: {
:
: factor = num % divBy;
: divBy++;
:
: }
:
: public void hasNextFactor()
: {
:
: for (int i = 1; i <= num; i++)
: {
:
: nextFactor();
: if (factor == 0 ) System.out.println( i );
:
: }
:
: }
:
: public static void main (String[] args)
: {
:
: FactorGenerator fg = new FactorGenerator ( Integer.parseInt ( args[0] ));
: System.out.println ( fg.num );
: fg.hasNextFactor();
:
: }
:
: private int num;
: private int divBy;
: public int factor;
: }
:
: Thanks man your a life saver!
:




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.