Question: JAVA 1. (4 points) Write a class called Primes. The class should have the following: a. A constructor. It is passed 2 parameters x and
JAVA
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
Here is an example of code which uses the Primes class.
public class UsePrimes { public static void main(String[] args) {
int x=10, y=100; // x and y could be any integers as long as x<=y
Primes p = new Primes(x,y); for (Integer i : p)
System.out.print(i + " "); System.out.println();
}
}
Here is the output that should be produced:
11 1317192329313741434753596167717379838997
2.
a. Write a class called SortedSet. A SortedSet object should contain a collection of objects, all of the same type. This means that SortedSet should be generic (i.e., SortedSet
I have provided a constructor for the class. You must write the following:
b. An add method. It is passed an object of type T (the type variable). It returns true if the object is added to the set, or false otherwise. An item is not added to a set if an equivalent item is already in the set, or if the set is full. Equivalence is defined by the compareTo method of the T class. Remember that at all times the set must have its items in ascending order.
c. A remove method. It is passed an object of type T. It returns true if an equivalent object exists in the set, in which case it is removed. The method should return false if no equivalent item exists in the set.
d. A toString method. As always, toString should return a string which represents the object. In the case of SortedSet, we would like the string to reflect which items are in the set, and in what order.
e. A contains method. It is passed an object of type T. It should return true if an equivalent item is found in the set, or false otherwise.
Here is an example program which uses the SortedSet class:
public class UseSortedSet { public static void main(String[] args) {
SortedSet
String data[] = { "a", "c", "a", "b", "z", "o" };
String otherData[] = {"f", "c", "a", "o"}; for (String s : data) {
System.out.println("Add " + s + " " + set.add(s));
System.out.println(set);
} for (String s : otherData)
System.out.println("Does the set contain " + s + "? " + set.contains(s));
for (String s : data) { System.out.println("Remove " + s + " " + set.remove(s));
System.out.println(set);
}
}
}
The output of this code should be:.
Add a true {a} Add c true {a,c} Add a false {a,c} Add b true {a,b,c} Add z true {a,b,c,z} Add o false {a,b,c,z} Does the set contain f? false Does the set contain c? true Does the set contain a? true Does the set contain o? false Remove a true {b,c,z} Remove c true {b,z} Remove a false {b,z} Remove b true {z} Remove z true {} Remove o false {} Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
