6 Replies Latest reply on May 29, 2010 8:05 AM by YoungWinston

# Help with printing prime numbers and their tenth

Hi all,

I am in need of help, I want a program that accepts input from user and checks if that number is a prime, if its a prime then the program should print the Tenth of the number, I have Trouble print this in my main class, i am not good in Java. Here's What i have done so far.
``````package quiz;

import java.io.IOException;

/**
*This class checks if a given number is a Prime.
* @author Tonata
*/
public class Check extends AbstractCheck {
private String tenth;
public Check() {
}

public Check(String tenth, int primeNumber) {
setTenth(tenth);
}

public String getTenth() {
return tenth;
}

public void setTenth(String tenth) {
this.tenth = tenth;
}

}

}

@Override
public boolean checkIfPrime(int number, int half) throws IOException, NumberFormatException, NullPointerException {

half = number / 2; //Store half of the given number
//loop through all the numbers below the half of the given number
for (int i = half; i >= 1; --i) {
if (number % i == 0) { // if the reminder of the given number divided by a number below half is equal to 0
return false;
}
}

return true;
}

/**
* abstract method to print the tenth of the given prime number
* @param tenth - 10th value of the number to be checked if its a prime
* @param int - prime number.
* @return tenth - String representation of the 10 of the number
*/
@Override
public String printTenthOfPrime(int primeNumber, String tenth) {
tenth = this.getTenth();
try {
int divisorInt;
int modulus;
tenth = "Tenth of " + primeNumber + "is 0";
tenth = "Tenth of " + primeNumber + "is 10";
}
} else {
if (modulus < 5) {
tenth = "Tenth of " + primeNumber + "is" + divisorInt * 10 + "";
} else {
tenth = "Tenth of " + primeNumber + "is" + (divisorInt + 1) * 10 + "";
}
}
}
} catch (IOException ex) {
ex.printStackTrace();
} catch (NumberFormatException ex) {
ex.printStackTrace();
} catch (NullPointerException ex) {
ex.printStackTrace();
}
return tenth;
}
}``````
Now I don't know how to print in my main, How do i call the methods from this class.
• ###### 1. Re: Help with printing prime numbers and their tenth
You create an instance of your Check class and then call method on that instance. You can print results using System.out.println(someString);

See [the tutorial|http://java.sun.com/docs/books/tutorial/java/concepts/class.html].

Note that your current printTenthOfPrime() overrides the method arguments, while checkIfPrime() overrides the half arguments. Both shouldn't do that.
• ###### 2. Re: Help with printing prime numbers and their tenth
Tonata wrote:
``````half = number / 2; //Store half of the given number
//loop through all the numbers below the half of the given number
for (int i = half; i >= 1; --i) {
if (number % i == 0) { // if the reminder of the given number divided by a number below half is equal to 0
return false;
}``````
``number/2``
you could start from [Math.sqrt(number)  ^api^|http://java.sun.com/javase/6/docs/api/java/lang/Math.html#sqrt%28double%29].
• ###### 3. Re: Help with printing prime numbers and their tenth
OK thank you guys, and where do i use the getters and setters, in my main class or in my origin class
• ###### 4. Re: Help with printing prime numbers and their tenth
You're implementing functions that don't seem to need state (simplified: things to store in instance variables) - you put in some arguments and get a result, so you would need getters and setters.

I can't understand what printTenthOfPrime is supposed to do based on your code and comments: '10th of prime' makes no sense in English. You call one parameter 'primenumber' but apparently it isn't because you call checkIfPrime on it. It should return some String, but it is also pass in a argument?
• ###### 5. Re: Help with printing prime numbers and their tenth
Tonata wrote:
OK thank you guys, and where do i use the getters and setters, in my main class or in my origin class
WalterLaan wrote:You're implementing functions that don't seem to need state (simplified: things to store in instance variables) - you put in some arguments and get a result, so you would not need getters and setters.

I can't understand what printTenthOfPrime is supposed to do based on your code and comments: '10th of prime' makes no sense in English. You call one parameter 'primenumber' but apparently it isn't because you call checkIfPrime on it.
I would suggest a name like primeCandidate for the variable.

And maybe roundToTens() for the method.
It looks like the algorithm is

| Input | | Result |
| 0 - 4 | | 0 |
| 5 - 10 | | 10 |
| 11 - 14 | | 10 |
| 15 - 19 | | 20 |
| 20 - 24 | | 20 |
| 25 - 29 | | 30 |
| 30 - 34 | | 30 |
|| etc. ||
• ###### 6. Re: Help with printing prime numbers and their tenth
Tonata wrote:
OK thank you guys, and where do i use the getters and setters, in my main class or in my origin class
Answer: getters and setters are for use outside the class. Code inside the class (ie, inside its methods) can always see instance variables, even if they are private (which they should almost always be).

However, you seem to have more problems than that. A couple of tips:

1. You have two separate problems: finding a prime number and rounding a value to the nearest 10. Keep the logic separate. Your checkIfPrime() method seems reasonably decoupled, but your printTenthOfPrime() is trying to do all sorts of things at once. Think about something like a:
``public int roundToNearestTen(int value) {...``
method.

2. Don't mix up Strings and numbers. Both of your basic problems are number-based, so use numbers. Once you've got the result, then you can worry about formatting it for display/printing.

HIH

Winston