Prime Number Checker
In this lab, you will be tasked with implementing a method in Java named isPrimeNumber
. This method will determine whether a given number is a prime number.
Objectives
- Implement the
isPrimeNumber
Method:- Your method should be named
isPrimeNumber
and be part of theMain
class. - It should take a single integer input.
- The method should return a boolean value:
True
if the number is prime, andFalse
otherwise.
- Your method should be named
Key Concepts
- Prime Number: A prime number is a natural number greater than 1 that is not a product of two smaller natural numbers.
- Edge Cases: Pay special attention to edge cases like
0
,1
, and negative numbers. None of these are prime numbers, so your method should returnFalse
for these inputs.
Examples
-
Example with a Prime Number
- Input:
5
- Expected Output:
True
- Reason:
5
is a prime number as it has no divisors other than1
and5
itself.
- Input:
-
Example with a Non-Prime Number
- Input:
4
- Expected Output:
False
- Reason:
4
is not a prime number as it can be divided evenly by2
(apart from1
and4
).
- Input:
Instructions
-
Create the
isPrimeNumber
Method:- Define the method within the
Main
class. - Ensure the method accepts a single integer parameter and returns a boolean.
- Define the method within the
-
Handle Edge Cases:
- Your method should correctly return
False
for0
,1
, and any negative numbers, as these are not prime.
- Your method should correctly return
-
Testing Your Code:
- You can test your method with various inputs, including prime numbers, non-prime numbers, and edge cases.
Challenges Information
Challenge 1: Method Existence and Correct Return Type
Objective: Ensure that a method named isPrimeNumber
exists in the Main
class and it returns a boolean value.
- Requirement:
- The method must be named exactly
isPrimeNumber
. - The return type of the method should be boolean (
True
orFalse
).
- The method must be named exactly
Challenge 2: Test Case for 0
Objective: Verify that the isPrimeNumber
method returns False
when 0
is passed as an input.
- Requirement:
- Input:
0
- Expected Output:
False
- Input:
- Rationale:
0
is not a prime number, so the method should returnFalse
.
Challenge 3: Test Case for 3
Objective: Test that the isPrimeNumber
method returns True
when 3
is passed as an input.
- Requirement:
- Input:
3
- Expected Output:
True
- Input:
- Rationale:
3
is a prime number and should be correctly identified as such.
Challenge 4: Test Case for 2
Objective: Confirm that the isPrimeNumber
method returns True
when 2
is passed as an input.
- Requirement:
- Input:
2
- Expected Output:
True
- Input:
- Rationale:
2
is the smallest and only even prime number.
Challenge 5: Test Case for 99
Objective: Ensure that the isPrimeNumber
method returns False
when 99
is passed as an input.
- Requirement:
- Input:
99
- Expected Output:
False
- Input:
- Rationale:
99
is not a prime number as it can be divided by numbers other than1
and99
.
Challenge 6: Test Case for Negative Numbers
Objective: Validate that the isPrimeNumber
method returns False
for any negative number input.
- Requirement:
- Input: Any negative number (e.g.,
-1
,-5
,-10
) - Expected Output:
False
- Input: Any negative number (e.g.,
- Rationale: Prime numbers are positive, natural numbers greater than
1
. Therefore, all negative numbers should returnFalse
.
This lab will test your understanding of prime numbers, your ability to implement logical conditions in Java, and your attention to detail, especially regarding edge cases. Good luck!