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: Method returning boolean

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Method returning boolean

    Assignment:
    isPrime Method
    A prime number is a number that is only evenly divisible by itself and 1. For example, the number 5 is prime because it can only be evenly divided by 1 and 5. The number 6, however, is not prime because it can be divided evenly 1, 2, 3, and 6.
    Write a method named isPrime, which takes an integer as an argument and returns true if the argument is a prime number, or false otherwise. Demonstrate the method in a complete program.

    package isprimemethod;
    import javax.swing.JOptionPane;
     
    /**
     *
     * @author uuuuuuu
     */
    public class Main {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            String input;
            int num;
            boolean bool;
     
            input = JOptionPane.showInputDialog("Enter an integer.");
            num = Integer.parseInt(input);
     
            bool = isPrime(num);
            System.out.println(bool);
     
        }
     
        public static boolean isPrime(int num)
        {
            boolean bool;
            {
                if(num%1==0 && num%num==0)
                    bool = true;
                else
                    bool = false;
            }
            return bool;
        }
    }


    I tried a few numbers out but the outcome is always "true". No outcome came out to be "false".
    What is wrong with the code?


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Method returning boolean

    if(num%1==0 && num%num==0)

    num%1 means the remainder of num/1 which is 0.

    num/num = 1 with remainder 0.

    Always true unless perhaps num = 0;

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Method returning boolean

    Quote Originally Posted by javapenguin View Post
    if(num%1==0 && num%num==0)

    num%1 means the remainder of num/1 which is 0.

    num/num = 1 with remainder 0.

    Always true unless perhaps num = 0;
    I know that but, what part of the code do I change in order for it to make certain values to become "false" which is supposed to be false, whereas "true" is true.

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Method returning boolean

    Ohhhh, non-prime numbers also would have that factor.

    Have a for loop that starts at 3

    public static boolean isPrime(int num)
    {
     
    if (num <=1)
    return false;
     
    // if num = 2, it will skip the for loop and return true.
     
    // but if for some reason it doesn't, then just change it to x = 3 and have condition
    // if (num==2) return true;
     
    for (int x =2;  x <  num; x++)
    {
    if (num%x ==0)
    return false;
     
    }
    return true;
     
    }
    Last edited by javapenguin; November 13th, 2010 at 06:57 PM.

Similar Threads

  1. Returning Null
    By Woody619 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: July 22nd, 2010, 01:53 PM
  2. returning a 2D array
    By straw in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 11th, 2010, 04:30 AM
  3. throwing and returning
    By chronoz13 in forum Exceptions
    Replies: 6
    Last Post: October 25th, 2009, 01:00 AM
  4. [SOLVED] Modification of Boolean function in Java program
    By lotus in forum Java SE APIs
    Replies: 4
    Last Post: July 10th, 2009, 11:15 AM
  5. [SOLVED] Java exception "result already defined"
    By rptech in forum Object Oriented Programming
    Replies: 3
    Last Post: May 20th, 2009, 06:48 AM