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

Write algorithms and programs to create a class PrimeSequence that implements th

ID: 3803088 • Letter: W

Question

Write algorithms and programs to create a class PrimeSequence that implements the Sequence interface.

Provide a Demo class that will produce an arbitrary sequence of n prime numbers in table format and will perform an analysis of those n prime numbers.

Output: Output will provide a table of n prime numbers. The table will be as close to “square” as possible - i.e. same number of rows and columns - with at most 10 entries per row. All entries will be right-aligned. Additionally, output will include a “histogram” of the occurrences of the last digit [0-9] of each prime number. The histogram will be horizontal, may be scaled, and each entry must fit on a single row. All output should be handled by the Demo class. Additional details of the histogram will be discussed in class.

Input: Input will be provided by the command-line. Two input constants will be placed on the command-line upon execution. For example: java SequenceDemo 6 50 ; where the prime sequence will start with the next prime after the first number, and the second number (n) is the number of prime numbers to be sequenced. Both numbers will be integers and both numbers must be 1 or greater. Instructions for compilation/execution should include command-line input details.

REQUIREMENTS:

No switch or breaks statements allowed.

You must write at least THREE programs: one will be the Sequence interface with a next() method; one will be the PrimeSequence class that will implement the interface; and one will be the SequenceDemo class that will perform the demonstration/analysis.

The Sequence interface will provide for the next() method. Interface will use class documentation standards.

The PrimeSequence class will implement the next() method, a method to determine if a number is prime, and any other methods/instance variables required for the class.

The SequenceDemo class will accept and validate the command-line input, request the specified number (n) of primes, starting were specified, collecting data for analysis, and preparing the table for output. The analysis will entail counting how many instances of each digit [0-9] occur on the last digit of each prime.

MUST BE DONE IN JAVA

Explanation / Answer

public class LastDigitDistribution

{

   private int[] counters;

   // Constructs a distribution whose counters are set to zero.

   public LastDigitDistribution()

   {

     counters = new int[10];

   }

/**

     Processes values from this sequence.

     @param seq the sequence from which to obtain the values

     @param valuesToProcess the number of values to process

*/

public void process(Sequence seq, int valuesToProcess)

{

    for (int i = 1; i <= valuesToProcess; i++)

     {

        int value = seq.next();

        int lastDigit = value % 10;

        counters[lastDigit]++;

     }

}

// Displays the counter values of this distribution.

public void display()

{

    for (int i = 0; i < counters.length; i++)

    {

        System.out.println(i + ": " + counters[i]);

    }

}

}

The Sequence Interface

public interface Sequence
{
    int next();
}

SquareSequence Class

public class SquareSequence implements Sequence
{
    private int n;

    public int next()
    {
        n++;
       return n*n;
    }

The Random Sequence Class

public class RandomSequence implements Sequence
{
    public int next()
    {
        return (int) (Integer.MAX_VALUE * Math.random());
    }
}

The Demo/Tester Class for The Sequence

public class SequenceDemo {

    public static void main(String[] args)
    {
          LastDigitDistribution dist1 = new LastDigitDistribution();
          dist1.process(new SquareSequence(), 100);
          dist1.display();
          System.out.println();

          LastDigitDistribution dist2 = new LastDigitDistribution();
          dist2.process(new RandomSequence(), 1000);
          dist2.display();
       }
}

Now I have to introduce a primesequence class this is what I've come up with so far the prime number algorithm is fine I just don't know how to implement it and relate it with this sequence.

public class SquareSequence implements Sequence
{
   private int n;

   public int next()
   {{         
        for (int i = 1; i < n; i++ ){
           int j;
           for (j=2; j<i; j++){
           int k = i%j;
           if (k==0){
           break;
           }
           }
           if(i == j){
           System.out.print(" "+i);            
           }              
       }
        return n;      
  }   
  }
}

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