Arizona State University - CSE205 Assignment #11 Due Date Friday, April 13th, at
ID: 3706517 • Letter: A
Question
Arizona State University - CSE205 Assignment #11 Due Date Friday, April 13th, at 5:30pm Important: This is an individual assignment. Please do not collaborate. No late assignment will be accepted. Make sure that you write every line of your code. Using code written by someone else will be considered a violation of the academic integrity and will result in a report to the Dean's office.
Minimal Submitted Files 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 Stack class) in java.util package.
Input1
E 243 E 5 E 914 Q
Output1 Choice Action ------ ------ E Enter A Positive Integer To Find Primes Q Quit ? Display Help What action would you like to perform? Please a positive integer n, to find primes up to n: 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 prime(s) up to 61 ----------------------- 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 ----------------------- The largest prime number used to divide: 11 The count of prime numbers found: 18 What action would you like to perform? Please a positive integer n, to find primes up to n: 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 prime(s) up to 365 ----------------------- 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 ----------------------- The largest prime number used to divide: 23 The count of prime numbers found: 72 What action would you like to perform? Input2 E 61 E 365 Q Output2 Choice Action ------ ------ E Enter A Positive Integer To Find Primes Q Quit ? Display Help What action would you like to perform? Please a positive integer n, to find primes up to n: 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 prime(s) up to 61 ----------------------- 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 ----------------------- The largest prime number used to divide: 11 The count of prime numbers found: 18 What action would you like to perform? Please a positive integer n, to find primes up to n: 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 prime(s) up to 365 ----------------------- 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 ----------------------- The largest prime number used to divide: 23 The count of prime numbers found: 72 What action would you like to perform? Input3 E 2359 Q Output3 Choice Action ------ ------ E Enter A Positive Integer To Find Primes Q Quit ? Display Help What action would you like to perform? Please a positive integer n, to find primes up to n: 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 next prime to divide: 37 The next prime to divide: 41 The next prime to divide: 43 The next prime to divide: 47 The next prime to divide: 53 ----------------------- The prime(s) up to 2359 ----------------------- 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 919 929 937 941 947 953 967 971 977 983 991 997 1009 1013 1019 1021 1031 1033 1039 1049 1051 1061 1063 1069 1087 1091 1093 1097 1103 1109 1117 1123 1129 1151 1153 1163 1171 1181 1187 1193 1201 1213 1217 1223 1229 1231 1237 1249 1259 1277 1279 1283 1289 1291 1297 1301 1303 1307 1319 1321 1327 1361 1367 1373 1381 1399 1409 1423 1427 1429 1433 1439 1447 1451 1453 1459 1471 1481 1483 1487 1489 1493 1499 1511 1523 1531 1543 1549 1553 1559 1567 1571 1579 1583 1597 1601 1607 1609 1613 1619 1621 1627 1637 1657 1663 1667 1669 1693 1697 1699 1709 1721 1723 1733 1741 1747 1753 1759 1777 1783 1787 1789 1801 1811 1823 1831 1847 1861 1867 1871 1873 1877 1879 1889 1901 1907 1913 1931 1933 1949 1951 1973 1979 1987 1993 1997 1999 2003 2011 2017 2027 2029 2039 2053 2063 2069 2081 2083 2087 2089 2099 2111 2113 2129 2131 2137 2141 2143 2153 2161 2179 2203 2207 2213 2221 2237 2239 2243 2251 2267 2269 2273 2281 2287 2293 2297 2309 2311 2333 2339 2341 2347 2351 2357 ----------------------- The largest prime number used to divide: 53 The count of prime numbers found: 350 What action would you like to perform? Input4 E 546 E 43 E 98 E 734 Q Output4 Choice Action ------ ------ E Enter A Positive Integer To Find Primes Q Quit ? Display Help What action would you like to perform? Please a positive integer n, to find primes up to n: 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 prime(s) up to 546 ----------------------- 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 ----------------------- The largest prime number used to divide: 29 The count of prime numbers found: 100 What action would you like to perform? Please a positive integer n, to find primes up to n: 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 prime(s) up to 43 ----------------------- 2 3 5 7 11 13 17 19 23 29 31 37 41 43 ----------------------- The largest prime number used to divide: 7 The count of prime numbers found: 14 What action would you like to perform? Please a positive integer n, to find primes up to n: 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 prime(s) up to 98 ----------------------- 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 ----------------------- The largest prime number used to divide: 11 The count of prime numbers found: 25 What action would you like to perform? Please a positive integer n, to find primes up to n: 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 prime(s) up to 734 ----------------------- 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 ----------------------- The largest prime number used to divide: 29 The count of prime numbers found: 130 What action would you like to perform?
Assignment11.java
// Assignment #: 11
//Name: Your name
// StudentID:
// Lecture:
// Description: Assignment 11 class displays a menu of choices to a user // and performs the chosen task. It will keep asking a user to
// enter the next choice until the choice of 'Q' (Quit) is entered.
import java.io.*;
public class Assignment11
{
public static void main (String[] args)throws IOException
{
char input1; String line = new String();
printMenu();
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader stdin = new BufferedReader(isr);
PrimeComputing primeComputing = null;
do// will ask for user input
{
System.out.println("What action would you like to perform?");
line = stdin.readLine(); input1 = line.charAt(0);
input1 = Character.toUpperCase(input1);
if (line.length() == 1)
{ // matches one of the case statements switch (input1)
{
case 'E': //Enter Problem parameters
System.out.print("Please a positive integer n, to find primes up to n: ");
int n = Integer.parseInt(stdin.readLine().trim());
if (n < 2)
System.out.print("Please enter an integer greater than 1. ");
else
{
primeComputing = new PrimeComputing(n);
primeComputing.computePrimesUpToN();
primeComputing.printResults();
System.out.print("The largest prime number used to divide: " + primeComputing.getMaxPrime() + " ");
System.out.print("The count of prime numbers found: " + primeComputing.getCount() + " ");
}
break;
case 'Q':
//Quit break;
case '?':
//Display
Menu printMenu();
break;
default: System.out.print("Unknown action "); break;
}
}
else
{
System.out.print("Unknown action ");
}
}
while (input1 != 'Q' || line.length() != 1);
}
/** The method printMenu displays the menu to a user**/
public static void printMenu()
{
System.out.print("Choice Action " + "------ ------ " + "E Enter A Positive Integer To Find Primes " + "Q Quit " + "? Display Help ");
}
}
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 originalQueue;
private LinkedList primeQueue;
private LinkedList backupQueue;
//Constructor to initialize all queues
public PrimeComputing(int enteredNum)
{
//TO BE COMPLETED
}
// 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
//TO BE COMPLETED
System.out.println("The next prime to divide: " + nextPrime);
//2. Enqueue the next Prime into the primeQueue. /
/TO BE COMPLETED
//3. Go through the integers in the original queue,
//and elimienate 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.
//TO BE COMPLETED
}
while (nextPrime <= Math.sqrt(n));
//4. Transfer all remaining integers in the original queue
//to the prime queue.
//TO BE COMPLETED }
//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 laegest 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;
}
}
Please help!
Explanation / Answer
Below is your code: -
Assignment11.java
package primeNumberQueues;
//Assignment #: 11
//Name: Your name
//StudentID:
//Lecture:
//Description: Assignment 11 class displays a menu of choices to a user
//and performs the chosen task. It will keep asking a user to
//enter the next choice until the choice of 'Q' (Quit) is entered.
public class Assignment11 {
public static void main(String[] args) throws IOException {
char input1;
String line = new String();
printMenu();
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader stdin = new BufferedReader(isr);
PrimeComputing primeComputing = null;
do // will ask for user input
{
System.out.println("What action would you like to perform?");
line = stdin.readLine();
input1 = line.charAt(0);
input1 = Character.toUpperCase(input1);
if (line.length() == 1) {
// matches one of the case statements
switch (input1) {
case 'E': // Enter Problem parameters
System.out.print("Please a positive integer n, to find primes up to n: ");
int n = Integer.parseInt(stdin.readLine().trim());
if (n < 2)
System.out.print("Please enter an integer greater than 1. ");
else {
primeComputing = new PrimeComputing(n);
primeComputing.computePrimesUpToN();
primeComputing.printResults();
System.out.print(
"The largest prime number used to divide: " + primeComputing.getMaxPrime() + " ");
System.out.print("The count of prime numbers found: " + primeComputing.getCount() + " ");
}
break;
case 'Q': // Quit
break;
case '?': // Display Menu
printMenu();
break;
default:
System.out.print("Unknown action ");
break;
}
} else {
System.out.print("Unknown action ");
}
} while (input1 != 'Q' || line.length() != 1);
}
/** The method printMenu displays the menu to a user **/
public static void printMenu() {
System.out.print("Choice Action " + "------ ------ " +
"E Enter A Positive Integer To Find Primes "
+ "Q Quit " + "? Display Help ");
}
}
PrimeComputing.java
package primeNumberQueues;
//Assignment #: 11
//Name:
//StudentID:
//Lecture:
//Description: TO BE COMPLETED
//We will be using LinkedList as a Queue
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) {
this.n = enteredNum;
this.originalQueue = new LinkedList<Integer>();
this.primeQueue = new LinkedList<Integer>();
this.backupQueue = new LinkedList<Integer>();
this.lastPrime = 2; // some value
this.primeCount = 0; // some value
for (int i = 2; i <= this.n; i++) {
this.originalQueue.add(i);
}
}
// computes all prime numbers up to some n
public void computePrimesUpToN() {
Integer 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 = this.originalQueue.getFirst();
System.out.println("The next prime to divide: " + nextPrime);
// 2. Enqueue the next Prime into the primeQueue.
this.primeQueue.add(nextPrime);
this.primeCount++;
// Keeping track of last prime
lastPrime = nextPrime;
// Going through all the numbers in originalQueue
while (!this.originalQueue.isEmpty()) {
Integer num = this.originalQueue.removeFirst();
if (num % this.lastPrime != 0) // adding the ones not divisible
// by last prime to the backup
// queue
this.backupQueue.add(num);
}
// copying back to original queue
while (!this.backupQueue.isEmpty()) {
Integer num = this.backupQueue.removeFirst();
this.originalQueue.add(num);
}
} while (nextPrime <= Math.sqrt(n));
// 4. Transfer all remaining integers in the original queue
// to the prime queue.
while (!this.originalQueue.isEmpty()) {
Integer num = this.originalQueue.removeFirst();
this.primeQueue.add(num);
}
}
// 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:
500
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 prime(s) up to 500
-----------------------
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
-----------------------
The largest prime number used to divide: 23
The count of prime numbers found: 95
What action would you like to perform?
?
Choice Action
------ ------
E Enter A Positive Integer To Find Primes
Q Quit
? Display Help
What action would you like to perform?
Q
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.