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

Create a Java program that performs the following tasks: 1. Prompt the user for

ID: 3674961 • Letter: C

Question

Create a Java program that performs the following tasks:

1. Prompt the user for a number between 0 and 1000 A. Verify the entered number is within the specified range Note: Use the method, public boolean withinRange(int value, int min, int max).

2. If, and only if the entered number is within the specified range A. Count the number of prime numbers that exist between 0 and the entered number.

1. Use the method int countPrimes(int input) to count the primes numbers a. Loop through all the numbers 0 to input (inclusively) b. If a number is determined to be prime, increase the prime count by 1

2. Use the method boolean isPrime(int testValue) to test if a number is prime or not a. Perform the following test to determine if a number is prime 1. No number less than 2 is prime (i.e., 0 and 1 are not prime) 2. No even number except 2 is prime (i.e., 4, 6, 8, etc. are not prime)

3. If a number can be evenly divided by any odd number between 3 and the square root of the number, the number is not prime (i.e., 27 is not prime) b. Verify the results to the List of Valid Prime Numbers Between 0 and 100 B. Display the input value and the prime count result (see Example Output section)

List of Valid Prime Numbers Between 0 and 1000

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997

Explanation / Answer

import java.util.Scanner;

public class PrimeChecker {
  
   public static boolean withinRange(int value, int min, int max){
       if(value>min && value <= 1000)
           return true;
       return false;
   }
  
   public static boolean isPrime(int testValue){
       boolean flag = true;
       for(int i=2; i<Math.sqrt(testValue); i++){
           if(testValue%i == 0){
               flag = false;
               break;
           }
       }
       return flag;
   }
  
   public static int countPrimes(int num){
       int count = 0;
       for(int i=2 ; i<=num; i++){
           if(isPrime(i))
               count++;
       }
       return count;
   }
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       int num, min=0,max=1000;
       System.out.println("Enter a number [0-1000]");
       num = sc.nextInt();
       boolean isRange = withinRange(num, min, max);
       if(isRange){
           System.out.println("Number of prime Number: "+countPrimes(num));
       }
   }
}

/*

Output:

Enter a number [0-1000]
45
Number of prime Number: 17

*/