Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote