Question: Redesign PrimeNumberMethod add one member variable, a constructor, get and set methods for the variable, convert the static methods to instance methods, and overrides the
Redesign PrimeNumberMethod
add one member variable, a constructor, get and set methods for the variable, convert the static methods to instance methods, and overrides the toString methods to the following code, and redesign the main to test.
public class PrimeNumberMethod { public static void main(String[] args) { System.out.println("The first 50 prime numbers are "); printPrimeNumbers(50); }
public static void printPrimeNumbers(int numberOfPrimes) { final int NUMBER_OF_PRIMES_PER_LINE = 10; // Display 10 per line int count = 0; // Count the number of prime numbers int number = 2; // A number to be tested for primeness
// Repeatedly find prime numbers while (count < numberOfPrimes) { // Print the prime number and increase the count if (isPrime(number)) { count++; // Increase the count
if (count % NUMBER_OF_PRIMES_PER_LINE == 0) { // Print the number and advance to the new line System.out.printf("%-5s ", number); } else System.out.printf("%-5s", number); }
// Check if the next number is prime number++; } }
/** Check whether number is prime */ public static boolean isPrime(int number) { for (int divisor = 2; divisor <= number / 2; divisor++) { if (number % divisor == 0) { // If true, number is not prime return false; // number is not a prime } }
return true; // number is prime } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
