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

Write a program that displays the first 50 prime numbers in ascending order. Use

ID: 3752102 • Letter: W

Question

Write a program that displays the first 50 prime numbers in ascending order. Use a generic queue given below to store the prime numbers. Also, use the given method isPrime() to determine whether the number is prime or not.


public class GenericQueue<E> {
private java.util.LinkedList<E> list = new java.util.LinkedList<E>();

public void enqueue(E e){
list.addLast(e);
}

public E dequeue(){
return list.removeFirst();
}

public int getSize(){
return list.size();
}

@override

public String toString(){

return "Queue: " + list.toString();
}

----------

public static boolean isPrime(int n){
for (int i=2; i<= n/2; i++){
if (n % i== 0)
return false;
}
return true;
}

Explanation / Answer

Java Code: Save this file as Main.java and keep This file and GenericQueue.java in same folder

class Main {

                public static boolean isPrime(int n){

                                for (int i=2; i<= n/2; i++){

                                                if (n % i== 0)

                                                                return false;

                                }

                                return true;

                }

                public static void main(String[] args) {

                                GenericQueue<Integer> queue = new GenericQueue<Integer>();

                                int n = 0, p = 2;

                                while(n <= 50)

                                {

                                                if(isPrime(p))

                                                {

                                                                queue.enqueue(p);

                                                                n++;

                                                }

                                                p++;

                                }

                                System.out.println(queue);

                }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote