Beginner JAVA Read the following program description entirely before deciding to
ID: 3589971 • Letter: B
Question
Beginner JAVA
Read the following program description entirely before deciding to panic.
You are to write a program to find all of the prime numbers between randomly chosen minimum and maximum values.
You should write a class PrimesInInterval.
This class should have a method
public static boolean isPrime(int possiblePrime)
This method returns true if possiblePrime is a prime number, and false otherwise. This method does not have a println call.
This class should have a method
public static int floor()
that returns a randomly chosen positive int value less than 1000.
This class should have a method
public static int ceiling(int lowBound)
that returns a randomly chosen int value that is greater than lowBound (but note, to make sure our program finishes before the course is over, let's make the returned value random, but less than lowBound * 10).
This class should have a main method that
uses floor and ceiling to establish a range of numbers to check for being prime.
prints the floor and ceiling values.
loops through the potential prime numbers, using isPrime to determine whether each is a prime number.
prints the prime numbers
prints the number of prime numbers found
Hint: isPrime need only loop from 2 to (possiblePrime/2).
This program is based on Listing 6.7.
Explanation / Answer
PrimesInInterval.java
import java.util.Random;
public class PrimesInInterval {
static Random r = new Random();
public static void main(String[] args) {
//Declaring variables
boolean bool;
int countPrimes=0;
//Finding the lower bounds and upper bounds
int lowbound=floor();
int upperbound=ceiling(lowbound);
//Displaying the lower bound and upper bound
System.out.println("The Lower Bound is :"+lowbound);
System.out.println("The Upper Bound is :"+upperbound);
//This for loop will display will iterate over numbers
for(int i=lowbound;i<=upperbound;i++)
{
//calling the method
if(isprime(i))
{
System.out.println(i+" is a Prime Number .");
countPrimes++;
}
}
System.out.println("The Number of Prime Numbers :"+countPrimes);
}
private static int ceiling(int lowbound) {
return r.nextInt(((lowbound*10-1) - lowbound) + 1) + lowbound;
}
private static int floor() {
return r.nextInt((999 - 1) + 1) +1;
}
/* This method will check whether
* the number is prime or not
*/
private static boolean isprime(int num)
{
// If the user entered number is '2' return true
if (num == 2)
return true;
for (int i = 2; i * i <= num; i++)
{
if (num % i == 0)
return false;
}
return true;
}
}
___________________
Output:
The Lower Bound is :57
The Upper Bound is :519
59 is a Prime Number .
61 is a Prime Number .
67 is a Prime Number .
71 is a Prime Number .
73 is a Prime Number .
79 is a Prime Number .
83 is a Prime Number .
89 is a Prime Number .
97 is a Prime Number .
101 is a Prime Number .
103 is a Prime Number .
107 is a Prime Number .
109 is a Prime Number .
113 is a Prime Number .
127 is a Prime Number .
131 is a Prime Number .
137 is a Prime Number .
139 is a Prime Number .
149 is a Prime Number .
151 is a Prime Number .
157 is a Prime Number .
163 is a Prime Number .
167 is a Prime Number .
173 is a Prime Number .
179 is a Prime Number .
181 is a Prime Number .
191 is a Prime Number .
193 is a Prime Number .
197 is a Prime Number .
199 is a Prime Number .
211 is a Prime Number .
223 is a Prime Number .
227 is a Prime Number .
229 is a Prime Number .
233 is a Prime Number .
239 is a Prime Number .
241 is a Prime Number .
251 is a Prime Number .
257 is a Prime Number .
263 is a Prime Number .
269 is a Prime Number .
271 is a Prime Number .
277 is a Prime Number .
281 is a Prime Number .
283 is a Prime Number .
293 is a Prime Number .
307 is a Prime Number .
311 is a Prime Number .
313 is a Prime Number .
317 is a Prime Number .
331 is a Prime Number .
337 is a Prime Number .
347 is a Prime Number .
349 is a Prime Number .
353 is a Prime Number .
359 is a Prime Number .
367 is a Prime Number .
373 is a Prime Number .
379 is a Prime Number .
383 is a Prime Number .
389 is a Prime Number .
397 is a Prime Number .
401 is a Prime Number .
409 is a Prime Number .
419 is a Prime Number .
421 is a Prime Number .
431 is a Prime Number .
433 is a Prime Number .
439 is a Prime Number .
443 is a Prime Number .
449 is a Prime Number .
457 is a Prime Number .
461 is a Prime Number .
463 is a Prime Number .
467 is a Prime Number .
479 is a Prime Number .
487 is a Prime Number .
491 is a Prime Number .
499 is a Prime Number .
503 is a Prime Number .
509 is a Prime Number .
The Number of Prime Numbers :81
_____________Could you rate me well.Plz .Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.