6 Replies - 1332 Views - Last Post: 13 April 2012 - 05:13 PM Rate Topic: -----

#1 =DD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 13-April 12

Testing a perfect number in Java

Posted 13 April 2012 - 04:01 PM

Hi all, my first post :)

I've been working thru a programming book and I am having issues. I am supposed to check if an inputed integer is perfect or not, then display the factors following the number.

I have 4 methods total including the main method. The problem is I am supposed to use an array to store the factors of the number as I go along and I'm stuck. I can't fill in the last method (the one that is supposed to print out the number and its factors to the screen) without figuring out how I am supposed to store the factors into an array. It would be probably best to create the array in the main method, but I'm pretty new to programming and don't fully understand how to use them.

If you could point me in the right direction, that would be great.

Thanks :D

  import java.util.Scanner;

        public class check_perfect
        {
                public static void main ( String args[] )
                {
                        Scanner input = new Scanner ( System.in );

                        int numbers;
                        int pp = 0; // possible perfect
                        int counter = 0;

                        do
                        {
                        System.out.print ( "How many numbers would you like to test?" );
                        numbers = input.nextInt();
                        }
                        while ( validateNum ( numbers ) = false );


                        do
                        {
                        System.out.print ( "Please enter a possible perfect number: " );
                        pp = input.nextInt();

                        testPerfect ( pp );
                        printFactors ( pp );

                        counter ++;
                        }
                        while ( counter < numbers );

                }

                public boolean validateNum ( int numbers )
                {
                                if ( numbers < 1 )
                                {
                                        return false;
                                }
                                else
                                {
                                        return true;
                                }
                }

                public boolean testPerfect( int pp )
                {
                        int sum = 0;
                        int x = 0;

                        for ( int i = 1; i < pp; i++ )
                        {
                                if ( ( pp % i ) == 0 )
                                {
                                        x = i;
                                        sum += x;
                                }
                                if ( x == pp )
                                {
                                        return true;
                                }
                                else
                                {
                                        return false;
                                }
                        }
                }

                public static printFactors()
                {

                }

        } 


Is This A Good Question/Topic? 0
  • +

Replies To: Testing a perfect number in Java

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Testing a perfect number in Java

Posted 13 April 2012 - 04:13 PM

Most books I've seen have a chapter dedicated to arrays. Did you skip that chapter?

And, yes, you'll probably want to have the array available to store the values as you get them from the user, so you'll need it in your main() method. An array is a variable with multiple entries or elements, so instead of storing the user's input into a variable that can only store one element, as in:

numbers = input.nextInt(); // line 16

you'll store them in an array:

numbers[index] = input.nextInt();

There are important concepts about using arrays that you need to understand, and I doubt anyone will try to explain them all to you in a post. Here's the Java tutorial on arrays. Start there and let us know when you need help.
Was This Post Helpful? 1
  • +
  • -

#3 =DD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 13-April 12

Re: Testing a perfect number in Java

Posted 13 April 2012 - 04:23 PM

I didn't skip the chapter on arrays I'm just not sure how to fully implement one into this program specifically. My problem is that I can't figure out how to make the array store the factors of the number that the user inputs, and how to return that to the printFactors method for printing out.
Was This Post Helpful? 0
  • +
  • -

#4 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,534
  • Joined: 05-May 05

Re: Testing a perfect number in Java

Posted 13 April 2012 - 04:26 PM

When you find a divisor store it into the array.

if ( ( pp % i ) == 0 )
{
     x = i;
     sum += x;
     factors[index++] = x;
}



Since you don't know how many factors there are up front, you'll need to define an upper limit. In the worst case, you could set to the size of the number in question.

int[] factors = new int[pp];



There are optimal ways to determine the number of divisors and the factors themselves as well.

It's probably best to make factors a class member that's visible to all other members, so that printFactors can access it directly.
Was This Post Helpful? 2
  • +
  • -

#5 =DD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 13-April 12

Re: Testing a perfect number in Java

Posted 13 April 2012 - 04:46 PM

If I'm not able to use factors as a class member (which I'm not allowed to), how can I store the factors and return them to the other methods in a boolean method? I'm sorry but this is a little over complicated.
Was This Post Helpful? 0
  • +
  • -

#6 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,534
  • Joined: 05-May 05

Re: Testing a perfect number in Java

Posted 13 April 2012 - 04:57 PM

I don't know how your classes need to be structured, but here are few ways to do it.

private int[] testPerfect(int p) {
    int[] factors = new int[p];
    //...
    return factors;
}

private void printFactors(int[] factors){}



or

private boolean testPerfect(int p) {
    int[] factors = new int[p];
    //...
    printFactors(factors);
    return false;
}

private void printFactors(int[] factors){}


Was This Post Helpful? 1
  • +
  • -

#7 =DD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 13-April 12

Re: Testing a perfect number in Java

Posted 13 April 2012 - 05:13 PM

Thanks for all the help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1