Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

write a java program that consist of : A prime number is a number that has two factors: 1 and itself. For example, the first

write a java program that consist of :

A prime number is a number that has two factors: 1 and itself. For example, the first 5 prime numbers are: 2,3,5,7,11.

Write a program to read a positive non-zero even number from the keyboard.

Print the last 5 prime numbers before the entered value and 3 prime numbers after the entered value. The length of the generated prime numbers is always 8. For example, the user entered 44 then the output in this case is

29 31 37 41 43 47 53 59

Another example the user entered 20

7 11 13 17 19 23 29 31

There is a possibility if the entered value may have less than 5 prime numbers before that. In this case, then the extra prime numbers are printed after the entered number to ensure the length is always 8.

For example, the user entered 6, there are only 3 prime numbers before 6. There are two missing prime numbers. So, in this case two extra prime numbers are printed after 6 to ensure the length is always 8. The solution in this case is

2 3 5 7 11 13 17 19

Another example the user entered 2, so in this case there is no prime numbers before that, hence, 8 prime numbers will be printed after 2, the output is

3 5 7 11 13 17 19 23

Create two classes to generate the Prime numbers. They are primeNumberBack and primeNumberForward. The class primeNumberBack will generate the prime numbers before the entered number. For example, if the entered number is 42 then in this class the following prime numbers will be generated: (23 29 31 37 41) while the class primeNumberForward, in this case, will generate the remaining three prime numbers (43 47 51) to conform to the length of 8.

In the main method, the user enters the number, and accordingly will call the respective classes to generate the prime numbers, the final output result is printed in the main method.

public class PrimeNumbers

public static void main(String[] args) {

// read a positive non-zero input number

// call the method in the class primeNumberBack

// determine how many prime numbers were generated from the method in class // primeNumberBack

// call the method in the class primeNumberForward

// sort the prime numbers obtained from the methods in class primeNumberBack // and primeNumberForward

// print the sorted prime numbers

}

class primeNumberBack{

// a method will generate the prime numbers that are before the entered input

// value. This method will return an array consisting of prime numbers before the // entered number. For instance, if the entered number was 42 then it returns the // last 5 prime numbers (23 29 31 37 41).

}

class primeNumberForward{

// a method here will generate the prime numbers that are after the entered input // value. This method will return an array consisting of prime numbers after the // entered number. For instance, if the entered number was 42 then it returns only // 3 prime numbers (43 47 51).

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions