Results 1 to 5 of 5
- 12-29-2010, 05:15 PM #1
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
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
IsThisAPrime()Java Code:public class PrimeTest { public static void main(String[] args) { new FindSpecificPrime(10001); } }
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.
- 12-29-2010, 06:47 PM #2
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:
...then, in your other class, 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; } }
Java Code:if (new IsThisAPrime(n).searchIfPrime()) {
- 12-29-2010, 07:50 PM #3
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
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.
- 12-30-2010, 02:51 AM #4
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.
- 12-30-2010, 03:57 AM #5
Member
- Join Date
- Dec 2010
- Posts
- 10
- Rep Power
- 0
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
-
GUI help. Don t understand
By s0meb0dy in forum AWT / SwingReplies: 2Last Post: 10-27-2010, 09:40 PM -
trying to understand this rational numbers class
By sonny in forum New To JavaReplies: 9Last Post: 03-28-2010, 10:29 PM -
I don´t understand
By Manikyr in forum New To JavaReplies: 6Last Post: 02-22-2009, 11:22 PM -
Understand my logic errors and better understanding method and class creation
By freethinker89 in forum New To JavaReplies: 3Last Post: 10-06-2008, 11:03 PM -
pls somebody help me ; need to understand SQL queries from DAOImpl class
By hossainsadd in forum Web FrameworksReplies: 0Last Post: 04-15-2008, 01:32 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks