//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) // 例: 7 は 4 番目の素数であるため、nthPrime(4) は 7 を返します int[] getPrimesWithin(int min, int max) //最小値と最大値の間の素数の配列を返します String toString() // データ セット内のすべての素数の文字列バージョンを返します
getUpperBound() // 素数をテストできる最大数 (最大インデックス) を返します
修飾子: private void computePrimes() // 各コンストラクターによってのみ呼び出されるアルゴリズムを使用します void changeUpperBound(int x) // 最高のインデックスが x になるように配列を変更します
コンストラクタ: Primes(int upperBound) Primes() // DEFAULT_UPPER_BOUND 定数を使用
テスターからの出力: 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 4 番目の素数: 7 7 素数?: true 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 23 29 31 37 41 41 43 47 53 59 61 67 71 73 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 197 199 199 199 199 199 199年97:53 59 61 61 67 71 71 71 73 79 83 89 97