Question

Write a class PrimeSequence that implements the Sequence interface of Worked Example 10.1* and produces a sequence of prime n

need source code for NetBeans

In order to produce arbitrary sequences, we declare an interface type with a single method: public interface Sequence int nex

public class RandomSequence implements Sequence public int nextO return int) (Integer.MAX VALUE Math.randomO) The following c

here is the file

Write a class PrimeSequence that implements the Sequence interface of Worked Example 10.1* and produces a sequence of prime numbers. * See Files section bj6_ch10_we.pdf Hint: Pseudocode to produce next prime: class Prime Sequence implements Sequence instance variable int p function next ()I isPrime-true While (isPrime) increment p; for (d-2; until d*d > p divides p) isPrime-false; d+) if (d if isPrime) return p; isPrime-true Use your PrimeSequence class to generate and print out the first 100 primes.
In order to produce arbitrary sequences, we declare an interface type with a single method: public interface Sequence int nextO The LastDigitDistribution class analyzes sequences. It keeps an array of ten counters. Its process method receives a Sequence object and the number of values to process and updates the counters: public void process(Sequence seq, int valuesToProcess) for (int i 1; i
0 0
Add a comment Improve this question Transcribed image text
Answer #1
JAVA Code:
public class PrimeSequence implements Sequence {

    int p=1;
    @Override
    public int next() {
        boolean isPrime = true;

        while (isPrime){
            p++;
            for(int d=2;d*d<=p;d++) {
                if (p%d == 0)
                    isPrime = false;

            }
            if(isPrime)
                    return p;
                isPrime = true;
            }

return 0;

    }
}

public class PrimeSequence implements Sequence int p-1; @Override public int next) boolean isPrimetrue: while (İSPrine){ р-н;

Main method(only for test):

public class main public static void main(String[] args) Primesequence primesequence = new Primesequence(); 1=1 :1<=100:1++){

Output:

/usr/lib/jvm/java-8-openjdk-amd64/bin/java 6:13 7:17 8:19 9:23 10:29 11:31 12:37 13:41 14:43 15:47 16:53 17:59 18:61 19:67 20

66:317 67:331 68:337 69:347 70:349 71:353 72:359 73:367 74:373 75:379 76:383 77:389 78:397 79:401 80:409 81:419 82:421 83:431

Add a comment
Know the answer?
Add Answer to:
need source code for NetBeans here is the file Write a class PrimeSequence that implements the Sequence inte...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Worked Example 10.1, add a default method default int [] values(int n) that yields an...

    In Worked Example 10.1, add a default method default int [] values(int n) that yields an array of the first n values of the sequence. Please use only the code provided below from the worked example and if possible provide screenshots using all the code provided and the test class provided: Sequence.java public interface Sequence { int next(); } LastDigitDistribution.java public class LastDigitDistribution { public void process(Sequence seq, int valuesToProcess) {       for (int i = 1; i <=...

  • Consider the following class definition class FibSequence implements Iterablexnteger public Iterator<Integer> iterator) return new FibIterator); private class FibIterator implements Iterator&lt...

    Consider the following class definition class FibSequence implements Iterablexnteger public Iterator<Integer> iterator) return new FibIterator); private class FibIterator implements Iterator<Integer> f //complete this You are required to create an inner class in the FibSequence class. The inner class should be called FibIterator. It implements the Iterator<Integer> interface and implements the hasNext() and the next() methods. The iterator iterates through the Fibonacci sequence. The Fibonacci sequence is a series of numbers where a number is the sum of previous two numbers....

  • (JAVA NetBeans) Write programs in java Example 10.21-24 //Animal.java /** Animal class * Anderson. Franceschi */...

    (JAVA NetBeans) Write programs in java Example 10.21-24 //Animal.java /** Animal class * Anderson. Franceschi */ import java.awt.Graphics; public abstract class Animal { private int x; // x position private int y; // y position private String ID; // animal ID /** default constructor * Sets ID to empty String */ public Animal( ) { ID = ""; } /** Constructor * @param rID Animal ID * @param rX x position * @param rY y position */ public Animal( String...

  • In a file named LLBag.java, write a class called LLBag that implements the Bag interface using...

    In a file named LLBag.java, write a class called LLBag that implements the Bag interface using a linked list instead of an array. You may use a linked list with or without a dummy head node. Bag interface code: /* * Bag.java * * Computer Science 112, Boston University */ /* * An interface for a Bag ADT. */ public interface Bag { /*    * adds the specified item to the Bag. Returns true on success    * and...

  • Create a method based program to find if a number is prime and then print all...

    Create a method based program to find if a number is prime and then print all the prime numbers from 1 through 500 Method Name: isPrime(int num) and returns a boolean Use for loop to capture all the prime numbers (1 through 500) Create a file to add the list of prime numbers Working Files: public class IsPrimeMethod {    public static void main(String[] args)    {       String input;        // To hold keyboard input       String message;      // Message...

  • Consider the following class: public class Sequence { private int[] values; public Sequence(int size) { values...

    Consider the following class: public class Sequence { private int[] values; public Sequence(int size) { values = new int[size]; } public void set(int i, int n) { values[i] = n; } public int get(int i) { return values[i]; } public int size() { return values.length; } } Add a method public boolean sameValues(Sequence other) to the Sequence class that checks whether two sequences have the same values in some order, ignoring duplicates. For example, the two sequences 1 4 9...

  • 5. Given the following class definition, write Java code that implements the addFirst method public class...

    5. Given the following class definition, write Java code that implements the addFirst method public class MyLinkedList<E> { private Node<E> head, tail; private int size 0; // Number of elements in the list /** Add an element to the beginning of the list */ public void addFirst (E e) { private static class Node<E> { E element; Node<E> next; public Node (E element) { = element; this.element

  • Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use impl...

    Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use implements Comparable<Book> in the class definition. Now, all book objects are instances of the java.lang.Comparable interface. Write a test program that creates an array of ten books. 1. Use Arrays.sort( Book[]l books) from the java.util package to sort the array. The order of objects in the array is determined using compareTo...) method. 2. Write a method that returns the most expensive book in the...

  • I need a java flowchart for the following code: import java.util.*; public class Main {   ...

    I need a java flowchart for the following code: import java.util.*; public class Main {    public static void main(String[] args) {    Scanner sc=new Scanner(System.in);           System.out.print("Enter the input size: ");        int n=sc.nextInt();        int arr[]=new int[n];        System.out.print("Enter the sequence: ");        for(int i=0;i<n;i++)        arr[i]=sc.nextInt();        if(isConsecutiveFour(arr))        {        System.out.print("yes the array contain consecutive number:");        for(int i=0;i<n;i++)        System.out.print(arr[i]+" ");       ...

  • q2: Write a public class named MythMouseListener that implements the Mouselistener interface. This class will have...

    q2: Write a public class named MythMouseListener that implements the Mouselistener interface. This class will have a public constructor that takes a JTextArea and a JLabel as parameters and stores these in instance variables. Override the mouseEntered method to . display the text from the JTextArea on the JLabel in all upper case letters. Then, override the mouseReleased method to display the text from the JTextArea on the JLabel in all lower case letters. The other three methods from the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT