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

[Java] Stacking and Sorting This is from Building Java Programs ED3* Chapter 14,

ID: 3557433 • Letter: #

Question

[Java] Stacking and Sorting

This is from Building Java Programs ED3* Chapter 14, Programing Project 1. The question is #1, it's continuied on the 2nd page, ignore problem #2. Here's where the formula is from, http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

These are combined in an alternating fashion to form a sequence of pairs the first values from each half (2 and 7), then the second values from each half (8 and 3), and so on. Your method should draw an Il legal Argument Exception if the queue does not have an even size. Use one stack as storage. Write a Primes program that finds prime numbers using the Sieve of Eratosthenes, an algorithm devised by a Greek mathematician of the same name who lived in the third century BC. The algorithm finds all prime numbers up to some maximum value n, as described by the following pseudocode: create a queue of numbers to process. fill the queue with the integers 2 through n inclusive. Create an empty result queue to store primes. repeat the following steps: obtain the next prime p by removing the first value from the queue of numbers. put p into the result queue of primes. loop through the queue of numbers, eliminating all numbers that are divisible by p, while (p is less than the square root of n). all remaining values in the numbers queue are prime, so transfer them to the result primes queue. Write an HTML. Validator program that reads files of HTML. data and uses in the file are properly matched. A tag consists of a named element between apply to a range of text, in which case a pair of tags is and a closing tag with a indicating the end of the For

Explanation / Answer

import java.lang.Integer;
import java.lang.Math;

public class Prime
{
     public static void main(String[] args)
     {
          int input = 0;
          System.out.print("Enter a max value for find its primes>> ");
          Scanner in = new Scanner(System.in);
          input = in.nextInt();
          printAllPrimes(input);
     }

     public static void printAllPrimes(int max)
     {
          LinkedQueue qNum = new LinkedQueue(); //num list
          LinkedQueue qPrime = new LinkedQueue(); //prime list
          Integer object = new Integer(2);
          Integer temp;

          for(int i = 2; i<=max; i++)
          {
               Integer num = new Integer(i);
               qNum.enqueue(num);

          }
          
          while(object.intValue() <= Math.sqrt(max))
          {
               object = (Integer)(qNum.dequeue());     
               qPrime.enqueue(object);
               temp = (Integer)(qNum.first());
               while(!qnum.isEmpty())
               {
                    if((temp.intValue() % object.intValue() == 0))
                         qNum.dequeue();

          }
}
//transfer all values of qNum to qPrime
//print qPrime


********************Here is the LinkedQueue implementation that I use with my Prime class*********************
//********************************************************************
// LinkedQueue.java Authors: Lewis/Chase
//
// Represents a linked implementation of a queue.
//********************************************************************

//package jss2;

import jss2.exceptions.*;

public class LinkedQueue implements QueueADT
{
private int count;
private LinearNode front, rear;

//-----------------------------------------------------------------
// Creates an empty queue.
//-----------------------------------------------------------------
public LinkedQueue()
{
count = 0;
front = rear = null;
}

//-----------------------------------------------------------------
// Adds the specified element to the rear of the queue.
//-----------------------------------------------------------------
public void enqueue (Object element)
{
LinearNode node = new LinearNode(element);

if (isEmpty())
front = node;
else
rear.setNext (node);

rear = node;
count++;
}

//-----------------------------------------------------------------
// Removes the element at the front of the queue and returns a
// reference to it. Throws an EmptyCollectionException if the
// queue is empty.
//-----------------------------------------------------------------
public Object dequeue() throws EmptyCollectionException
{
if (isEmpty())
throw new EmptyCollectionException ("queue");

Object result = front.getElement();
front = front.getNext();
count--;

if (isEmpty())
rear = null;

return result;
}

//-----------------------------------------------------------------
// Returns a reference to the element at the front of the queue.
// The element is not removed from the queue. Throws an
// EmptyCollectionException if the queue is empty.
//-----------------------------------------------------------------
public Object first() throws EmptyCollectionException
{
if (isEmpty())
throw new EmptyCollectionException ("queue");

return front.getElement();
}

//-----------------------------------------------------------------
// Returns true if this queue is empty and false otherwise.
//-----------------------------------------------------------------
public boolean isEmpty()
{
return (count == 0);
}

//-----------------------------------------------------------------
// Returns the number of elements currently in this queue.
//-----------------------------------------------------------------
public int size()
{
return count;
}

//-----------------------------------------------------------------
// Returns a string representation of this queue.
//-----------------------------------------------------------------
public String toString()
{
String result = "";
LinearNode current = front;

while (current != null)
{
result = result + (current.getElement()).toString() + " ";
current = current.getNext();
}

return result;
}
}

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