Results 1 to 11 of 11
Thread: please help!! FactorGenerator
- 01-18-2009, 05:04 PM #1
Member
- Join Date
- Nov 2008
- Location
- London
- Posts
- 39
- Rep Power
- 0
please help!! FactorGenerator
I have to write a program that asks the user for an integer and then display all its factors For example, when the user enters 15, the program should display the following:
1
3
5
15
Below is what I have managed to do so far but I am kind of stuck on
Doing I am not sure where to put the JOptionPane.showInputDialog
and the JOptionPane.showMessageDialog
Java Code:import javax.swing.JOptionPane; public class FactorGenerator { private int num; private int nextFactor; public int getNextFactor() { int i = nextFactor - 1 ; while ((num % i) != 0) { i--; } nextFactor = i; return i; } public boolean hasMoreFactors() { if (nextFactor == 1) { return false; } else { return true; } } }Last edited by afrttoh; 01-18-2009 at 05:09 PM. Reason: error
- 01-18-2009, 05:09 PM #2
Senior Member
- Join Date
- Jan 2009
- Posts
- 119
- Rep Power
- 0
* Create Object of JOptionPane
* Using the object , try using the appropriate method for showing the msg box. You can find this in the API.
- 01-18-2009, 05:18 PM #3
Member
- Join Date
- Nov 2008
- Location
- London
- Posts
- 39
- Rep Power
- 0
i know all that thank you but where to put it??
i tried to put in:
but it gives me error!public static void main(String[] args)
{
String num = JOptionPane.showInputDialog("Enter a whole number:");
int number = Integer.parseInt(num);
JOptionPane.showMessageDialog(null, " = " + i);
}
and what do you mean API?Last edited by afrttoh; 01-18-2009 at 05:25 PM.
-
I would first of all give your FactorGenerator class a way to initialize it's num and nextFactor numbers. This can be done by passing an int in its constructor and setting both num and nextFactor equal to this int. Alternatively (or in addition) you can do this via a setNum(int aNumber) method that again will set both num and nextFactor equal to the parameter.
Then you can worry about getting input from the user and showing output to the user in the main method.
- 01-18-2009, 05:34 PM #5
what kind of errors? it worked for me. mc meant to take a look at the java docs.
PHP Code:1. import javax.swing.JOptionPane; 2. 3. public class test 4. { 5. public static void main(String[] args) 6. { 7. // int i = 3; 8. String num = JOptionPane.showInputDialog("Enter a whole number:"); 9. int number = Integer.parseInt(num); 10. JOptionPane.showMessageDialog(null, " = " + number ); // original was "+ i );" 11. } 12. }Last edited by angryboy; 01-18-2009 at 05:50 PM.
USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 01-18-2009, 05:38 PM #6
Member
- Join Date
- Nov 2008
- Location
- London
- Posts
- 39
- Rep Power
- 0
errors:Java Code:import javax.swing.JOptionPane; public class FactorGenerator { private int num; private int nextFactor; public static void main(String[] args) { String num = JOptionPane.showInputDialog("Enter a whole number:"); int number = Integer.parseInt(num); JOptionPane.showMessageDialog(null, " = " + i); } public int getNextFactor() { int i = nextFactor - 1 ; while ((num % i) != 0) { i--; } nextFactor = i; return i; } public boolean hasMoreFactors() { if (nextFactor == 1) { return false; } else { return true; } } }
---------- javac ----------
C:\Documents and Settings\Administrator\Desktop\FactorGenerator.jav a:16: cannot find symbol
symbol : variable i
location: class FactorGenerator
JOptionPane.showMessageDialog(null, " = " + i);
^
1 error
Output completed (0 sec consumed)
- 01-18-2009, 05:39 PM #7
Member
- Join Date
- Nov 2008
- Location
- London
- Posts
- 39
- Rep Power
- 0
- 01-18-2009, 05:42 PM #8
you need to declare i first. like i had. on line 7 in the quotes.
USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 01-18-2009, 05:50 PM #9
Member
- Join Date
- Nov 2008
- Location
- London
- Posts
- 39
- Rep Power
- 0
- 01-18-2009, 06:44 PM #10
Member
- Join Date
- Nov 2008
- Location
- London
- Posts
- 39
- Rep Power
- 0
still cant do it!
-
If I wanted to initialize my class with an int via a constructor, I'd do something as simple as:
then my main method can construct an object of this class and pass an int into it at the same time:Java Code:class MyClass int value; public MyClass(int value) { this.value = value; // here I initialize value via the constructor parameter } public void myMethod() { // .... etc... } }
Java Code:public static void main(String[] args) { MyClass myObject = new MyClass(3); // pass a 3 into the object via a constructor parameter myObject.myMethod(); // ... etc... }


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks