Design a class prime that has one int private member that is prime at all times.
ID: 662659 • Letter: D
Question
Design a class prime that has one int private member that is prime at all times.
//Provide a default constructor that sets the member to 2//Provide a constructor that accepts an intger and sets p to it if it is a prime member,
//otherwise, it sets it to the closest prime number.//provide accessor and mutator for p.
//Here is a Function that allows you to check if a number is prime or not
inline bool isPrime(int a) {
if (a < 2 || (a > 2 && a%2 == 0))
return false;
if (a == 2)
return true;
for (int i = 3; i <= sqrt(a); i++){
if (a%i == 0)
return false;
}
return true;
}
Explanation / Answer
class prime
{
private int primeno;
prime()
{
primeno = 2;
}
prime(int h)
{
if(isPrimeNumber(h))
{
h=primeno;
}
}
public boolean isPrimeNumber(int a){
if(a < 2 || (a > 2 && a%2 == 0))
return false;
if (a == 2)
return true;
for (int i = 3; i <= sqrt(a); i++){
if (a%i == 0)
return false;
}
return true;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.