Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: printing factors with 2 classes

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default printing factors with 2 classes

    hi, i have a school project that asks for 2 classes to print factors of a number using 2 methods nextFactor and hasMoreFactors it does not specify if they are supposed to be void or not so... so far I'm at this-
    public class chapter7number10
    {
        public chapter7number10(int a)
        {
            factor = a;
        }
        public void hasMoreFactors()
        {
            do
            {
                nextfactor();
                System.out.println("one of the factor's is " + afactor);
            }
            while(afactor < factor);
        }
        public void nextfactor()
        {
            for(countingnumber = currentfactor; factor > countingnumber; countingnumber++)
            {
                currentfactor = currentfactor + countingnumber;
                if(factor % currentfactor == 0)
                {
                    afactor = currentfactor;
                }
            }
        }
        int factor;
        int afactor = 0;
        int currentfactor = 1;
        int countingnumber = 1;
    }
    and the tester -
    import java.util.*;
    public class chapter7number10tester
    {
        public static void main(String args[])
        {
            Scanner in = new Scanner(System.in);
            System.out.println("What is your factor");
            int a = in.nextInt();
            chapter7number10 tester = new chapter7number10(a);
            tester.hasMoreFactors();
        }
    }
    this is printing an infinite loop of the first factor and i need it to print all the factors excluding 1 and itself then stop, please help.


  2. #2
    Member
    Join Date
    Oct 2011
    Posts
    36
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: printing factors with 2 classes

    public class chapter7number10 {
     
        public chapter7number10(int a)
        {
            factor = a;
        }
        public void hasMoreFactors()
        {
            System.out.println("one of the factor's is " + afactor);
            do
            {
                nextfactor();
                System.out.println("one of the factor's is " + afactor);
     
            }
            while(afactor < factor);
        }
        public void nextfactor()
        {
            for(countingnumber = currentfactor +1; factor >= countingnumber; countingnumber++)
            {
                currentfactor =  countingnumber;
                if(factor % currentfactor == 0)
                {
                    afactor = currentfactor;
                    break;
                }
            }
        }
        int factor;
        int afactor = 1;
        int currentfactor = 1;
        int countingnumber = 1;
     
    }

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: printing factors with 2 classes

    thanks for the code but i do not want it to print 1 and itself can you change that or no?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,975
    My Mood
    Hungover
    Thanks
    133
    Thanked 555 Times in 476 Posts

    Default Re: printing factors with 2 classes

    luck999, please read this carefully before you post again: The Problem with Spoon-feeding

    If you continue spoonfeeding, let alone spoonfeeding incorrect answers, I'm going to ban you.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Prime Factorization Method: Won't reproduce all Factors every time
    By Staticity in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 30th, 2011, 05:16 AM
  2. Replies: 1
    Last Post: September 28th, 2011, 07:29 AM
  3. Printing Job
    By qwerty53 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 19th, 2011, 08:34 AM
  4. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 02:41 AM
  5. How to printing a Jtable
    By hundu in forum AWT / Java Swing
    Replies: 0
    Last Post: June 29th, 2009, 06:57 AM