Enter your Ending Number and click the Calculate button
import java.awt.*;
import BreezyGUI.*;
public class PrimeNumberApplt extends GBApplet
{
//Define the GUI elements
Label endNumberLabel = addLabel ("Ending Number",1,1,1,1);
IntegerField endNumberField = addIntegerField (0,1,2,1,1);
Label primeTestAnswerLabel = addLabel ("Prime Numbers less than or",2,1,2,1);
Label primeTestAnswer2Label = addLabel ("equal to Ending Number",3,1,2,1);
TextArea primeTestAnswerField = addTextArea ("",4,1,2,7);
Label primeCountLabel = addLabel ("No. of Primes in the List",12,1,1,1);
IntegerField primeCountField = addIntegerField (0,12,2,1,1);
Button calculateButton = addButton ("Calculate",14,1,2,1);
//Declare Variables
int endNum,count;
int i;
int testNumber;
String prime;
public void buttonClicked (Button buttonObj)
{
//Display wait message and Clear the Text Area and reset the count for subsequent runs
count = 0;
prime = "Please Wait" + "\n" + "While the Program Runs";
displayData(prime, count);
//Start the calculations
processPrime();
//Display the results
displayData(prime, count);
//Return focus to the EndNumber Field for additional input
endNumberField.requestFocus();
endNumberField.selectAll();
}
//This module initiates the calculations and calls the loopNumber module
private void processPrime()
{
//This reads the ending number from the user input
endNum = endNumberField.getNumber();
//Test for Negative Numbers through Positive 7 for Ending Number
if (endNum >= 8)
{
//Assign values to variables and start loop to calculate prime numbers
prime = "2" + "\n" + "3" + "\n" + "5" + "\n" + "7" + "\n";
count = 4;
testNumber = 8;
loopNumber();
}
else if (endNum == 7)
{
prime = "2" + "\n" + "3" + "\n" + "5" + "\n" + "7" + "\n";
count = 4;
}
else if ((endNum == 5) || (endNum == 6))
{
prime = "2" + "\n" + "3" + "\n" + "5" + "\n";
count = 3;
}
else if ((endNum == 3) || (endNum == 4))
{
prime = "2" + "\n" + "3" + "\n";
count = 2;
}
else if (endNum == 2)
{
prime = "2" + "\n";
count = 1;
}
else
{
prime = "You must enter a positive" + "\n" + "integer greater than 1.";
count = 0;
}
}
//This module runs a loop to test each number up to Ending Number
private void loopNumber()
{
while (testNumber <= endNum)
{
//This tests for even numbers
if(testNumber % 2 == 0)
{
testNumber += 1;
}
else
//This module does the actual testing for prime
testPrime(testNumber);
}
}
//This module runs a loop to test all the divisors of the TestNumber
private void testPrime(int testNum)
{
i = 3;
while(i < testNum / 3 + 1)
{
//This tests if the number is evenly divisible and if so breaks out of the loop
if (testNum % i == 0)
{
i = testNum / 3 + 1;
}
else
{
i += 2;
if(i >= testNum / 3 + 1)
{
//This adds the number being tested to the display of prime numbers
prime = prime + testNum + "\n";
//This increments the counter
count += 1;
}
}
}
//This increments the test number to test the next number in order
testNumber += 1;
}
// This module displays the Prime numbers and the count
private void displayData(String prime, int count)
{
primeCountField.setNumber (count);
primeTestAnswerField.setText (prime);
}
}