really need help..please
well im a high school student who has a question
i have a problem with this java program
im trying to write a program that will print all the factors of a number that is entered in an input box. If there are no factors other than 1 and itself it will printout that the number is a prime number.
im not very good at this stuff sometimes, if any one could help me out with this problem it would be alot of my shoulders becuase im having trouble in this course
Message was edited by:
billjones
So, what's your question? What part are you having trouble with? (If the answer is "Everything," then you need to speak to your instructor or find a private tutor. These forums are not a good venue for that kind of teaching.
Do you know how to display an "input box"?
Do you know how to take text that's been entered into an "input box" and turn it into a number?
Do you know how to find the factors of a number without respect to Java?
Do you know how to turn the above factoring process into Java instructions?
Do you know how to structure a general Java program, with classes, methods, a main() method, etc.?
Do you know how to break a problem down into pieces, and how to map those pieces to Java methods?
i pretty much need help with most of the whol java program..not teh flowchart...i know about the public class and plulic startic void main(string [ ] args and what not..but im not to sure about the rest, not sure of what loop to use and how its used .
sorry but i really suck at this stuff
Use a for loop that loops through all the numbers up to the given number. You do know how to use % right? It calculates the remainder. For example:int a = 15%4;In this example, a would be 3.
> Use a for loop that loops through all the numbers up> to the given number. Up to the square root, actually.
> i pretty much need help with most of the whol java> program..Then you should probably seek out more direct, one-on-one help, from your teacher or a tutor or something. Somebody here may walk through it with you, but most probably won't. Good luck.
ok well i kida got the body of it down.....but im not sure what the calculations are for the for loop for how to find the factors
> Up to the square root, actually.Wouldnt you go up to half the number?
> > Up to the square root, actually.
>
> Wouldnt you go up to half the number?
100:
1 * 100
2 * 50
4 * 25
5 * 20
10 * 10 <- sqrt. Everything below repeats what's above
20 * 5
25 * 4
50 * 2 < Half. Already had a few redundancies.
100 * 1
The point being, by the time we hit sqrt, we've already covered all the factors. If we get there and haven't found any factors, there will be none. If there were a factor > sqrt (but less than half), it would be paired with one less than sqrt, which we'd have hit by now.
/*
*
* factors
*
* Arthor: Bill Jones
*
* Purpose: to find any factors of a number and if its a prime number
*
* Date: October, 28,2006
*
*/
public class factors
{
public static void main(String [] args)
{
for (int ?)
{
System.out.print("your factors are " ?);
where i put the ? is where im lost ..these are the problems i can never figure out the calcuations ...
Gotcha, didnt think of doing it that way.
> where i put the ? is where im lost ..these are the> problems i can never figure out the calcuations ...Do you even know how to write a for loop?
You also need to look into JOptionPane to read a value from an input box.
no i dont...this is the first probalem on my own with teh for loop...this is why im asking how to do it
> > where i put the ? is where im lost ..these are
> the
> > problems i can never figure out the calcuations
> ...
>
> Do you even know how to write a for loop?
You want to go from 2 up to the square root of the number ([url http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Math.html#sqrt(double)]http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Math.html#sqrt(double)[/url]
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/for.html
ok well i still cant figure this out ive been trying it for abour 3 hours now i just cant get theses for loops
if somone would maybe write the loop for me of my program...i would hopefully learn from it,
i wouldnt copy and paste it, i would try to figure out whats going on in it and why, then i would trype it out but change what ever needs to be changed.
> ok well i still cant figure this out ive been trying
> it for abour 3 hours now i just cant get theses for
> loops
>
What are you not understanding?
Do you understand where you want to start?
Do you understand where you want to end?
Do you understand how to specify start and end in a for loop?
Did you read that tutorial carefully?
> if somone would maybe write the loop for me of my
> program...
How's about you try it based on the examples in the tutorial and what we told you about where to start and end, and then post your best effort here along with details about what's still giving you trouble?
jverda at 2007-7-21 12:28:34 >

> if somone would maybe write the loop for me of my
> program...i would hopefully learn from it,
> i wouldnt copy and paste it, i would try to figure
> out whats going on in it and why, then i would trype
> it out but change what ever needs to be changed.
Nice try, but no. Study this and see how it works instead.
//loop from 1 to a certain number
int number = 27;
for(int i=1; i<=number; i++)
{
//do something
}
What are you not understanding?
Do you understand where you want to start?
Do you understand where you want to end?
Do you understand how to specify start and end in a for loop?
Did you read that tutorial carefully?
ok what im not understaning is just the calculations to put in the for(...)
like im not sure how to find the factors of it.....i understand the body of the loop now
ill try again and see what ahppend...thanks for helping me so farMessage was edited by: billjones
> ok what im not understaning is just the calculations
> to put in the for(...)
> like im not sure how to find the factors of it.....i
> understand the body of the loop now
The body of the loop is the calculations.
for (...) {
// body
}
Okay, so you don't know how to test a given number to see if it's a factor? (You do realize that that's what's going on in the body, right? You're testing the current number to see if it's a factor.)
So, how would you do this manually? That is if you're not using Java and you're just doing it on paper, or explaining to somebody, what would you do to determine if, say, 20 or 23 is a factor of 100? What does it mean for one number to be a factor of another?
jverda at 2007-7-21 12:28:34 >

a factor is a number that can be multiplyed by another number to reach your answer..so if i wanted to find the factors of 25 i would do 1 *252 * 3 *4 *5 * 5and keep on goingso how would i write thet in java code?
> a factor is a number that can be multiplyed by> another number to reach your answerTry thinking of it in a different way. How can you tell that 20 is a factor of 100?
100 divided by 20 = 5 so you would take your answer and dived it by a number to see if u can get an even number?is that what you mean?
Yes, so the remainder would be zero.
> 100 divided by 20 = 5
>
> o you would take your answer and dived it by a number
> to see if u can get an even number?
Not an even number, but there has to be no remainder.
As somebody said earlier, look into the % operator.
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/op1.html
jverda at 2007-7-21 12:28:34 >

im not too sure how i would write this in java code but is it somthign like this
int number
for( int i % number, % =0, %<0)
System.out.println ( ' you have a factor ' );
System.out.println ( ' you have a prime number ' );
im pretty sure it isnt right but am i on the right track?
how would it know if there was a remainder or not?
and what would i do to make it print out all the factors
Message was edited by:
billjones
> im pretty sure it isnt right but am i on the right
> track?
Uhhh... No. You shouldnt be calculating the remainder there. All you want the for loop to do is increment from 2 to the square root of the number. Inside the for loop, calculate the remainder to see if its a factor.
> im not too sure how i would write this in java code
> but is it somthign like this
>
>
> int number
> for( int i % number, % =0, %<0)
No.
You're trying to do too much at once.
There's the loop, that just walks through every number up to the square root: for (int ix = 2; ix < the square root of the number ; ix++)
and then there's the loop body that does the test to see if the current nubmer is a factor: for (int ix = 2; ix < the square root of the number ; ix++) {
if number_we_are_testing divided by ix has no remainder {
ix is a factor
}
}
> how would it know if there was a remainder or not?
Use the % operator. You have to actually read that page I linked, or your textbook, or something. Don't just throw it in there any old place.
10 / 3 is 3
10 % 3 is 1
> and what would i do to make it print out all the
> factors
Print out the current numbe inside the if block.
jverda at 2007-7-21 12:28:34 >

oooo ok...wow i really forgot about the if statement...i think i can do his now...thanks you guys so much for helping me hopefully i wont need to ask another question
> oooo ok...wow i really forgot about the if
> statement...
Small pieces. :-)
One piece is for testing a given number to see if it's a factor, and then acting appropriately based on the result.
Another piece is for repeating that above piece for each potential factor you need to test.
>i think i can do his now...thanks you
> guys so much for helping me hopefully i wont need to
> ask another question
You're welcome. Ask again if you need to, but try to work through it--in small piece--by yourself first.
jverda at 2007-7-21 12:28:39 >
