## Java Program  Tags:
Inheritance
Java
Object-oriented programming
Hi Im trying to answer this question here: Find the 4th prime number Z such that Z=NumberOfDigits(R) where Z is prime AND R is prime AND where R=2p-1 where P is prime. Example the 1st prime number Z is 2, because 2=NumberOfDigits(31) because 2 is prime and because 31=25-1 and because 5 is prime. We are looking for the 2nd,3rd, 4th prime number Z in the Mageua Senoko series as well as its corresponding R and P values. Hints: 1 The 4th Z corresponds to a very large M. Take this into account when doing the calculation. 2 Please programme the solution to this problem in JAVA – We require your code 3 Computational efficiency is important. 4 Object orientation and inheritance MUST be used. i.e. Z is a prime, and a subset (subclass) of R, R is also prime and a subset of P which is also prime. My solution: public class PrimeZ { int firstPrimeNumOfZ=2; int SecondPrimeNumOfZ; int thirdPrimeNumOfZ; int fourthPrimeNumOfZ; long valueP; long valueR; public int getFourthPrimeNumOfZ() { return fourthPrimeNumOfZ; } public void setFourthPrimeNumOfZ(int fourthPrimeNumOfZ) { this.fourthPrimeNumOfZ = fourthPrimeNumOfZ; } public int getSecondPrimeNumOfZ() { int firstPrimeNumOfZ=2; int R=31; int P=5; R =(int) (Math.pow(firstPrimeNumOfZ, P)-1); return SecondPrimeNumOfZ; } public void setSecondPrimeNumOfZ(int secondPrimeNumOfZ) { SecondPrimeNumOfZ = secondPrimeNumOfZ; } public int getThirdPrimeNumOfZ() { int P=0; int R =(int) (Math.pow(firstPrimeNumOfZ, P)-1); return thirdPrimeNumOfZ; } public void setThirdPrimeNumOfZ(int thirdPrimeNumOfZ) { this.thirdPrimeNumOfZ = thirdPrimeNumOfZ; } public long getValueP() { return valueP; } public void setValueP(long valueP) { this.valueP = valueP; } public long getValueR() { int P = 0; int R =(int) (Math.pow(firstPrimeNumOfZ, P)-1); return valueR; } public void setValueR(long valueR) { this.valueR = valueR; } class PrimeR extends PrimeZ { public int getPrimeR(){ int R; int firstPrimeNumOfZ = 2; int P=5; { R =(int) (Math.pow(firstPrimeNumOfZ, P)-1); } return R; } } class PrimeP extends PrimeZ { public int getPrimeP() { int R=10; int firstPrimeNumOfZ = 2; int P = 0; { R =(int) (Math.pow(firstPrimeNumOfZ, P)-1); } return P; } } public int formulsForTheAppl(int value,int powerOf){ int firstPrimeNumOfZ = 2; int R = 31; int P=5; R =(int) (Math.pow(firstPrimeNumOfZ, P)-1); if (Math.pow(value, powerOf)== 0){ /* pow(x, 0) returns 1 */ return 1; } else{ return (int) (Math.pow(value, powerOf - 1) * value); } } public static void main(String[] args){ PrimeZ objPrime = new PrimeZ(); //PrimeR objPrimer = new PrimeR(); System.out.println("The SecondPrimeNumOfZ is:" +objPrime.fourthPrimeNumOfZ); System.out.println("The thirdPrimeNumOfZ is:" +objPrime.fourthPrimeNumOfZ); System.out.println("The fourthPrimeNumOfZ is:" +objPrime.fourthPrimeNumOfZ); //System.out.println("The new value of PrimeR is:" + objPrimer.getPrimeR); // System.out.println("The new value of PrimeP is:"+((PrimeP) objPrime).getPrimeP()); } } am I doing the right thing?

## Answer Wiki

Thanks. We'll let you know when a new response is added.

Here are my comments:

– In the first hint, you say the 4th Z corresponds to a very large M, but M is not defined in the problem description.
– The fourth hint say that Z must be a subclass of R, and R a subclass of P, but you defined Z as the base class, and both R and P as subclasses of Z.

Let’s analyze this:

Z must comply with:
-Z must be prime
-Z =numberOfDigits(R)

R must comply with:
-R must be prime
-R = (2^P) – 1

P must comply with:
-P must be prime.

What is the number with less constraints ?

I think it is P, so the base class should be P (as specified in the fourth hint) which is just a simple prime number.

Then R should extend P, and add a new class member for its P value
Then Z should extend R, and add a new class member for its R value

Then, I would create a new class for the Mageua Senoko serie, being this class who do the actual verifications/calculation to get the first, second, third, and fourth Z numbers.

You need a function to verify if a given number is prime. Here is an example, but you need to be sure that there is no a more efficient way to to it.

<pre>int isPrime(int n) {
for (i=2; i<=(int)(n/2); i++){
int m = n%i;
if (m==0){
break;
}
}
if(i == j)
return 1;
else
return 0;
}</pre>

Hope this helps.

-Carlosdl

## Discuss This Question:

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question. 