Results 1 to 11 of 11
  1. #1
    afrttoh is offline Member
    Join Date
    Nov 2008
    Location
    London
    Posts
    39
    Rep Power
    0

    Default 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

  2. #2
    MuslimCoder is offline Senior Member
    Join Date
    Jan 2009
    Posts
    119
    Rep Power
    0

    Default

    * Create Object of JOptionPane
    * Using the object , try using the appropriate method for showing the msg box. You can find this in the API.

  3. #3
    afrttoh is offline Member
    Join Date
    Nov 2008
    Location
    London
    Posts
    39
    Rep Power
    0

    Default

    i know all that thank you but where to put it??
    i tried to put in:
    public static void main(String[] args)
    {
    String num = JOptionPane.showInputDialog("Enter a whole number:");
    int number = Integer.parseInt(num);
    JOptionPane.showMessageDialog(null, " = " + i);

    }
    but it gives me error!

    and what do you mean API?
    Last edited by afrttoh; 01-18-2009 at 05:25 PM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  5. #5
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    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)

  6. #6
    afrttoh is offline Member
    Join Date
    Nov 2008
    Location
    London
    Posts
    39
    Rep Power
    0

    Default

    Quote Originally Posted by MuslimCoder View Post
    * Create Object of JOptionPane
    * Using the object , try using the appropriate method for showing the msg box. You can find this in the API.
    Quote Originally Posted by angryboy View Post
    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, " = " + i);
     11. }
     12. }



    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;
    
            }
        }
    }
    errors:

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

  7. #7
    afrttoh is offline Member
    Join Date
    Nov 2008
    Location
    London
    Posts
    39
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    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.

    thank you ill give it a try.

  8. #8
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    you need to declare i first. like i had. on line 7 in the quotes.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  9. #9
    afrttoh is offline Member
    Join Date
    Nov 2008
    Location
    London
    Posts
    39
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    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.

    can you show me an example please i am a bit lost!

  10. #10
    afrttoh is offline Member
    Join Date
    Nov 2008
    Location
    London
    Posts
    39
    Rep Power
    0

    Default

    still cant do it!

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    If I wanted to initialize my class with an int via a constructor, I'd do something as simple as:
    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...
      }
    }
    then my main method can construct an object of this class and pass an int into it at the same time:

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •