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

Please help me with my java programming You are required, but not limited, to tu

ID: 3705749 • Letter: P

Question

Please help me with my java programming

You are required, but not limited, to turn in the following source files:

Assignment11.java (You do not need to modify this file.)
PrimeComputing.java -- to be completed.

Requirements to get full credits in Documentation

The assignment number, your name, StudentID, Lecture number, and a description of each class/file need to be included at the top of each file/class (in this assignment, you might have only one file/class, but in future there will be more than one file.)

A description of each method is also needed.

Some additional comments inside of methods (especially for a "main" method) to explain code that are hard to follow should be written. You can look at Java programs in the text book to see how comments are added to programs.

New Skills to be Applied

In addition to what has been covered in previous assignments, the use of the following items, discussed in class, will probably be needed:

Queues

Program Description

Class Diagram:

Assignment #11 will be the construction of a program that takes an integer as a user input and compute all prime numbers up to the integer using queues. Since the Queue is an interface in Java Library and the LinkedList class is one of the classes that implement it, we will be using an object of LinkedList as a Queue. You may only use Queue methods when dealing with your queue.

Instruction:

Assignment11 class

This class displays a menu for a user. If a user enters "E", then it asks to enter a positive integer, and it needs to compute all prime numbers up to the integer n. This class is given by the instructor.

PrimeComputing class

You need to complete the following method.

public PrimeComputing(int enteredNum)

This is a constructor of the PrimeComputing class. It needs to initialize the instance variable n to the parameter value of enteredNum. It should also instantiate all queues, originalQueue, primeQueue, backupQueue. For the originalQueue, it needs to enqueue all integers from 2 up to n. For the instance variable lastPrime and primeCount, you can initialize them to a value that will work with code that you will write for other methods of the class.

public void computePrimesUpToN()

Please see PrimeComputing.java for instructions.

There are four parts to complete.
1. You will need to remove the first element from original queue and that will be your next prime number.
2. You need to enqueue this next prime number to the prime number queue.
3. You need to check integers in the original queue and see if any of them can be divided by the next prime number. If so, they need to be removed from the original queue. This process needs to be repeated until you reach a prime number in the original queue that is greater than the square root of the entered number n.
You will need to keep track of the last such prime that is used to divide other integers in the original queue. Note that this is not the largest prime number found.
4. After such loop, you will need to move all remaining integers in the original queue to the prime queue. Now the prime queue contains all prime numbers up to n. This will be printed in another method.

Requirements:

You need to implement this method using an object of the LinkedList class in java.util package to represent a Queue.

test case:

input:

output

given java class:

assignment11 class:

PrimeComputing class

Explanation / Answer


Given below is the completed code as per specifications of the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you

PrimeComputing.java

// Assignment #: 11
// Name:
// StudentID:
// Lecture:
// Description: TO BE COMPLETED


//We will be using LinkedList as a Queue
import java.util.LinkedList;

public class PrimeComputing
{
private int n, lastPrime, primeCount;
private LinkedList<Integer> originalQueue;
private LinkedList<Integer> primeQueue;
private LinkedList<Integer> backupQueue;

//Constructor to initialize all queues
public PrimeComputing(int enteredNum)
{
n = enteredNum;
originalQueue = new LinkedList<Integer>();
primeQueue = new LinkedList<Integer>();
backupQueue = new LinkedList<Integer>();
for(int i = 2; i <= enteredNum; i++)
originalQueue.add(i);
}

// computes all prime numbers up to some n
public void computePrimesUpToN()
{
int nextPrime = 2;
lastPrime = 2;

System.out.println(" Processing......");

do {
//1. Remove the first element in the original queue of integers
//and set the next prime "nextPrime" to be the number

nextPrime = originalQueue.removeFirst();

System.out.println("The next prime to divide: " + nextPrime);

//2. Enqueue the next Prime into the primeQueue.

primeQueue.addLast(nextPrime);
//3. Go through the integers in the original queue,
//and eliminate any number that is divisible by the next prime
//HINT: you will need to remove each integer from the original queue
//to examine them, and put back the integers that are NOT
//divisible by the next prime.
//This is where you will need a back up queue.
//Also, you will need to keep track of the last such prime
//that is used to divide other integers in the original queue.

while(!originalQueue.isEmpty())
{
int n = originalQueue.removeFirst();
if(n % nextPrime != 0) //not divisible, save to backup
backupQueue.add(n);
}


lastPrime = nextPrime;

while(!backupQueue.isEmpty())
originalQueue.addLast(backupQueue.removeFirst());

} while (nextPrime <= Math.sqrt(n));
  

//4. Transfer all remaining integers in the original queue
//to the prime queue.

while(!originalQueue.isEmpty())
primeQueue.addLast(originalQueue.removeFirst());
}

//It prints out all primes up to N, by displaying 10 prime numbers
//in each line.
public void printResults()
{
System.out.println(" -----------------------");
System.out.println("The prime(s) up to " + n);
System.out.println("-----------------------");

int count = 0;
primeCount = 0;

while (primeQueue.isEmpty() == false)
{
int primeNum = primeQueue.remove();
System.out.print(primeNum + " ");

count++;
//displaying 10 primes in each line
if (count%10 == 0)
System.out.print(" ");
}
System.out.println(" -----------------------");
primeCount = count;
}

// It returns the largest prime that was used to divide other integers
// in the original queue. Note that this is not the largest prime found.
public int getMaxPrime()
{
return lastPrime;
}
  
// It return the count of prime numbers up to n
public int getCount()
{
return primeCount;
}
}


output
-----
Choice Action
------ ------
E Enter A Positive Integer To Find Primes
Q Quit
? Display Help

What action would you like to perform?
E
Please a positive integer n, to find primes up to n:
243

Processing......
The next prime to divide: 2
The next prime to divide: 3
The next prime to divide: 5
The next prime to divide: 7
The next prime to divide: 11
The next prime to divide: 13
The next prime to divide: 17

-----------------------
The prime(s) up to 243
-----------------------
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
-----------------------
The largest prime number used to divide: 17

The count of prime numbers found: 53

What action would you like to perform?
E
Please a positive integer n, to find primes up to n:
5

Processing......
The next prime to divide: 2
The next prime to divide: 3

-----------------------
The prime(s) up to 5
-----------------------
2 3 5
-----------------------
The largest prime number used to divide: 3

The count of prime numbers found: 3

What action would you like to perform?
E
Please a positive integer n, to find primes up to n:
914

Processing......
The next prime to divide: 2
The next prime to divide: 3
The next prime to divide: 5
The next prime to divide: 7
The next prime to divide: 11
The next prime to divide: 13
The next prime to divide: 17
The next prime to divide: 19
The next prime to divide: 23
The next prime to divide: 29
The next prime to divide: 31

-----------------------
The prime(s) up to 914
-----------------------
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
-----------------------
The largest prime number used to divide: 31

The count of prime numbers found: 156

What action would you like to perform?
Q