Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA(Fill in *replacethis*) 1. (4 points) Write a class called Primes. The class should have the following: a. A constructor. It is passed 2 parameters

JAVA(Fill in *replacethis*)

1. (4 points) Write a class called Primes. The class should have the following:

a. A constructor. It is passed 2 parameters x and y, both of which are integers. You should write the class so that it can return a sequence of prime numbers between x and y (inclusive). For example, the prime numbers between 10 and 23 are 11, 13, 17, 19, and 23.

b. An isPrime method. It is passed a parameter p, and returns true if p is a prime number or false otherwise.

c. The class should implement the Iterable interface. This means that the Primes class should have a method called iterator, which returns an object of type Iterator. I have already written the iterator method; you must write hasNext and next for the Iterator object returned by the iterator method.

Here is the output that should be produced:

11 1317192329313741434753596167717379838997

package hw2;

import java.util.Iterator;

// generate a sequence of prime numbers that are greater than or equal to // smallest, and less than or equal to larges.

public class Primes implements Iterable { private int smallest, largest;

// fill this in public Primes(int x, int y) {

} // fill this in; to be used in one of more of the iterator methods below public static boolean isPrime(int p) { // replace this with your code return true; } // I have supplied the iterator method, which is required by the Iterable // interface. It returns an object which implements Iterator. You // must write hasNext and next methods for this object. public Iterator iterator() { // thie is an "anonymous inner class" return new Iterator() { private int p = smallest; public boolean hasNext() { // replace this return false; } public Integer next() { // replace this return p; } }; }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Visual Basic6 Database Programming

Authors: John W. Fronckowiak, David J. Helda

1st Edition

0764532545, 978-0764532542

More Books

Students also viewed these Databases questions

Question

2. What do you believe is at the root of the problem?

Answered: 1 week ago