//i have this so far
public class Primes {
private boolean[] nums;
private int upperbound;
public Primes(int n) {
nums = new boolean[n + 1];
for (int i = 2; i <= n; i++)
nums[i] = true;
}
public static final int DEFAULT_UPPER_BOUND = 100 + 1;
public boolean isPrime(int x) {
if (nums[x] == true) {
return true;
} else {
return false;
}
}
public boolean isComposite(int x) {
if (nums[x] == true) {
return false;
} else {
return true;
}
}
public int getPrimesWithin(int min, int max) {
for (int n = min; n <= max; n++) {
if (nums[n] == true) {
return n;
}
}
return max;
}
public String toString() {
String a = "";
a += (nums) + " ";
return a;
}
public int getUpperBound() {
return nums.length;
}
public int nthPrime(int n) {
int count = 0;
int index = 2;
while (count < n) {
if (nums[index] = true) {
count++;
}
}
return index;
}
public void computePrimes(int x) {
for (int i = 2; i * i <= x; i++) {
if (nums[i]) {
for (int j = i; i * j <= x; j++) {
nums[i * j] = false;
}
}
}
}
void changeUpperBound(int x) {
upperbound = x;
}
}
//it needs to fit this
public class Prime {
public static void main(String[] args) {
Primes somePrimes = new Primes();
System.out.println("Default Prime object");
System.out.println(somePrimes);
System.out.println("Upper Bound: " + somePrimes.getUpperBound());
System.out.println("4th prime: " + somePrimes.nthPrime(4));
System.out.println("7 prime?: " + somePrimes.isPrime(7));
System.out.println("7 composite?: " + somePrimes.isComposite(7));
somePrimes.changeUpperBound(50);
System.out.println(somePrimes);
int[] primes = somePrimes.getPrimesWithin(40, 50);
System.out.print("Primes between 40 and 50: ");
for (int p : primes)
System.out.print(p + " ");
System.out.println();
System.out.println("*******************");
Primes myPrimes = new Primes(53);
System.out.println(myPrimes);
System.out.println("Upper Bound: " + myPrimes.getUpperBound());
System.out.println("10th prime: " + myPrimes.nthPrime(10));
System.out.println("15 prime?: " + myPrimes.isPrime(15));
System.out.println("15 composite?: " + myPrimes.isComposite(15));
myPrimes.changeUpperBound(200);
System.out.println(myPrimes);
int[] primes2 = myPrimes.getPrimesWithin(50, 97);
System.out.print("Primes between 50 and 97: ");
for (int p : primes2)
System.out.print(p + " ");
System.out.println();
}
}
// i am not sure how to make the primeswithin work and if you notice any other errors there are probably several
Primes 类的规格:
实例字段:
private boolean[] nums;// 你可以选择另一个逻辑名称 // 是必要大小的变量,或者我们可以只使用 .length?
类常量:public static final int DEFAULT_UPPER_BOUND = ?; // 选择一个值并在默认构造函数中使用 // 构造函数。如果上限(可以测试素数的最大数)是 10, // 数组的大小是多少?
访问器: boolean isPrime(int x) boolean isComposite(int x) int nthPrime(int n) // 示例:nthPrime(4) 返回 7,因为 7 是第 4 个素数 int[] getPrimesWithin(int min, int max) //返回一个介于 min 和 max 之间的素数数组 String toString() // 返回数据集中所有素数的 String 版本
getUpperBound() // 返回可以测试素数的最高数(最高索引)
修饰符: private void computePrimes() // 使用算法,仅由每个构造函数调用 void changeUpperBound(int x) // 更改数组,使最高索引为 x
构造函数: Primes(int upperBound) Primes() // 使用 DEFAULT_UPPER_BOUND 常量
测试仪的输出:默认 Prime 对象素数到 100:2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 上限:100 第四个素数:7 7 素数?:真 7复合?:通过 50 的假素数:2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 40 到 50 之间的素数:41 43 47
素数到 53:2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 上限:53 第 10 个素数:29 15 素数?:假 15 复合?:真素数到 200:2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 71 73 79 83 89 89 97 101 101 103 107 109 113 113 127 131 137 137 139 139 139 139 149 151 157 157 163 167 167 179 179 179 181 191 193 199 199 199 79 83 89 97