Results 1 to 5 of 5
  1. #1
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default Can you help me understand how to use this class.

    I wrote a small program that found the nth prime. Then I decided to make separate classes and see if I can understand methods and objects.

    So I made a class called IsThisAPrime(). I got it working and it would display the information in system out. But then I wanted to make it return a boolean value so another class can use it.

    The problem is probably with the IsThisAPrime() constructor.. but I havnt figured it out yet.

    1) How can I make IsThisAPrime() return a boolean to FindSpecificPrime() ?

    2) Is it ok to create code like this in a loop? Or will this create a new Object each time and use lots of memory? if (new IsThisAPrime())

    Main
    Java Code:
    public class PrimeTest {
        public static void main(String[] args) {
            new FindSpecificPrime(10001);
        }
    }
    IsThisAPrime()
    Java Code:
    public class IsThisAPrime {
    
        private boolean isPrime;
        private int testNum;
    
        [COLOR="Red"]public boolean IsThisAPrime(int isItPrime) {[/COLOR]
    
            testNum = isItPrime;
            if (searchIfPrime()) {
            return true;
            } else {
                return false;
            }
        }
    
        private boolean searchIfPrime(){
    
            for (int i = 2; i < testNum; i++) {
                if (testNum % i == 0) {
                    return false;
                }
            }
            return true;
        }
    }

    FindSpecificPrime()
    Java Code:
    public class FindSpecificPrime {
    
        int testNum, primeAt, n;
        int[] primes;
    
    
        public FindSpecificPrime(int primeNum) {
            primes = new int[primeNum];
            primeAt = 0;
            n = 0;
            while (true) {
                n += 1;
                if ([COLOR="Red"]new IsThisAPrime(n)[/COLOR]) {
                    primes[primeAt] = n;
                    primeAt +=1;
                }
                if (primeAt == primeNum){
                    displayPrimes();
                    break;
                }
            }
        }
    
        private void displayPrimes() {
            for (int i = 0; i < primes.length; i++) {
                System.out.print(primes[i] + " ");
            }
            System.out.println("\nPrime number " + primes.length + " is " + primes[primes.length - 1]);
        }
    }
    Last edited by AcousticBruce; 12-29-2010 at 05:19 PM.

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    You can't do it like that. You've basically overriden the constructor with a method, and that's not allowed. What you have to do instead is something like this:
    Java Code:
    public class IsThisAPrime {
    
        private boolean isPrime;
        private int testNum;
    
        [color=red]public IsThisAPrime(int isItPrime) {
            testNum = isItPrime; // This line comes up here into the constructor, as it makes more sense to initialize the variable here.
        }
    
        // I've axed the entire second function as it's not necessary. We need a separate method ANYway, so we can just use searchIfPrime.[/color]
    
        private boolean searchIfPrime(){
            for (int i = 2; i < testNum; i++) {
                if (testNum % i == 0) {
                    return false;
                }
            }
            return true;
        }
    }
    ...then, in your other class, something like this:
    Java Code:
    if (new IsThisAPrime(n).searchIfPrime()) {

  3. #3
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default

    I guess I do not understand. That search was a private method.

    If I take out the boolean and rewrite it to manualy display the boolean value in system.out.print method and in the main method "new IsThisPrime(45)" It works. I would like to have FindSpecificPrime() call the IsThisPrime class send it a number and IsThisPrime returns a boolean value.

    The point of this was to have code that I can reuse (I was exercising that idea at least)

    So basically FindSpecificPrime will use the IsThisPrime class to test each number.

    Java Code:
    public class IsThisAPrime {
    
        private boolean isPrime;
        private int testNum, pot;
    
        public IsThisAPrime(int isItPrime) {
    
            testNum = isItPrime;
            searchIfPrime();
            System.out.println(isPrime);
    
        }
    
        private void searchIfPrime(){
    
            pot = 0;
            for (int i = 2; i < testNum; i++) {
                if (testNum % i == 0) {
                    pot = 1;
                    break;
    
                }
            }
            if (pot > 0) {
            isPrime = false;
            } else {
                isPrime = true;
            }
        }
        
    }
    Last edited by AcousticBruce; 12-29-2010 at 07:55 PM.

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    You can't have a constructor return a value other than object itself. I apologize for not realizing the method was private; however this is honestly the best way to achieve what you want to do.

    If you use the code you have now, you'll have a call like: if (new IsThisAPrime(n).isPrime) {, except that isPrime is still private.

    So either way, you need a get method, or a method has to be made public to achieve what you want to do.

    If you use the non-private 2nd method idea, you can still reuse the code later.

  5. #5
    Gordy.Liang is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    For this kind of thing, you do not need to construct any object instances to invoke the method of IsThisAPrime. So, this method should be defined as static method, so that it can be invoke without any instance of this class.

    public class IsThisAPrimeTool {

    ...

    public static IsThisAPrime(int isItPrime) {

    ...
    }

    When you want to invoke it, call IsThisAPrimeTool.IsThisAPrime(xxx)

    Many standard java APIs use this kind of definition mechanism.

Similar Threads

  1. GUI help. Don t understand
    By s0meb0dy in forum AWT / Swing
    Replies: 2
    Last Post: 10-27-2010, 09:40 PM
  2. trying to understand this rational numbers class
    By sonny in forum New To Java
    Replies: 9
    Last Post: 03-28-2010, 10:29 PM
  3. I don´t understand
    By Manikyr in forum New To Java
    Replies: 6
    Last Post: 02-22-2009, 11:22 PM
  4. Replies: 3
    Last Post: 10-06-2008, 11:03 PM
  5. Replies: 0
    Last Post: 04-15-2008, 01:32 AM

Posting Permissions

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