Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 07dea4d

Browse files
Merge pull request HarryDulaney#37 from HarryDulaney/New_Chapters_New_Solutions
Added 22_08 Exercise Solution + 22 09 and 22 10 Exercise Questions
2 parents dc0debe + e05629f commit 07dea4d

File tree

9 files changed

+288
-54
lines changed

9 files changed

+288
-54
lines changed

‎ch_13/exercise13_01/Triangle.java‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
//__________________________UML DIAGRAM_____________________________*
66
/* |
77
* /GeometricObject/ |
8-
*-------------------------------------------------------------------|
8+
*------------------------------------------------------------------|
99
* -color: String |
1010
* -filled: boolean |
11-
* -dateCreated : java.util.Date |
11+
* -dateCreated : java.util.Date |
1212
* |
13-
*-------------------------------------------------------------------|
13+
*------------------------------------------------------------------|
1414
* #GeometricObject() |
1515
* #GeometricObject(color:String,filled:boolean) |
1616
* |
@@ -20,11 +20,11 @@
2020
* +isFilled(): boolean |
2121
* +setFilled(filled: boolean): void |
2222
* +getDateCreated(): java.util.Date |
23-
* |
24-
* +toString(): String |
23+
* |
24+
* +toString(): String |
2525
* /+getArea()/: double/ |
2626
* /+getPerimeter(): double/ |
27-
* __________________________________________________________________| */
27+
* _________________________________________________________________| */
2828
/* ^
2929
^
3030
^
@@ -38,8 +38,8 @@
3838
* -side3: double |
3939
* _________________________________________________________________|
4040
* |
41-
* +Triangle2D() |
42-
* +Triangle2D(side1:double,side2:double,side3:double) |
41+
* +Triangle2D() |
42+
* +Triangle2D(side1:double,side2:double,side3:double) |
4343
* +getSide1(): double |
4444
* +setSide1(side1: double): void |
4545
* +getSide2(): double |

‎ch_22/Exercise22_09.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package ch_22;
2+
3+
/**
4+
* **22.9 (Geometry: gift-wrapping algorithm for finding a convex hull) Section 22.10.1
5+
* introduced the gift-wrapping algorithm for finding a convex hull for a set of
6+
* points. Assume that the Java’s coordinate system is used for the points. Implement the algorithm using the following method:
7+
* // Return the points that form a convex hull
8+
* public static ArrayList<Point2D> getConvexHull(double[][]s)
9+
* Point2D is defined in Section 9.6.
10+
* Write a test program that prompts the user to enter the set size and the points
11+
* and displays the points that form a convex hull.Here is a sample run:
12+
*/
13+
public class Exercise22_09 {
14+
}

‎ch_22/Exercise22_10.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package ch_22;
2+
3+
/**
4+
* 22.10 (Number of prime numbers) Programming Exercise 22.8 stores the prime numbers in a file named PrimeNumbers.dat.
5+
* Write a program that finds the number of prime numbers that are less than or equal to 10, 100, 1,000, 10,000,
6+
* 100,000, 1,000,000, 10,000,000, 100,000,000, 1,000,000,000, and
7+
* 10,000,000,000. Your program should read the data from PrimeNumbers.dat
8+
*/
9+
public class Exercise22_10 {
10+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package ch_22.exercise22_08;
2+
3+
import java.io.*;
4+
5+
/**
6+
* *22.8 (All prime numbers up to 10,000,000,000) Write a program that finds
7+
* all prime numbers up to 10,000,000,000. There are approximately
8+
* 455,052,511 such prime numbers. Your program should meet the following
9+
* requirements:
10+
* しかく Your program should store the prime numbers in a binary data file, named
11+
* PrimeNumbers.dat. When a new prime number is found, the number is
12+
* appended to the file.
13+
* しかく To find whether a new number is prime, your program should load the
14+
* prime numbers from the file to an array of the long type of size 10000.
15+
* If no number in the array is a divisor for the new number, continue to read
16+
* the next 10000 prime numbers from the data file, until a divisor is found
17+
* or all numbers in the file are read. If no divisor is found, the new number
18+
* is prime.
19+
* しかく Since this program takes a long time to finish, you should run it as a batch
20+
* job from a UNIX machine. If the machine is shut down and rebooted, your
21+
* program should resume by using the prime numbers stored in the binary data
22+
* file rather than start over from scratch.
23+
*/
24+
public class Exercise22_08 {
25+
private static PrimeStorage primeStorage;
26+
private static final String[] packageParts = Exercise22_08.class.getPackage().getName().split("\\.");
27+
private static final String PATH = packageParts[0] + File.separator + packageParts[1] + File.separator + "PrimeNumbers.dat";
28+
private static long[] primeReadBuffer = new long[10000];
29+
private static final long UPPER_BOUND = 10_000_000_000L;
30+
private static boolean reachedEndOfBuffer = false;
31+
32+
public static void main(String[] args) {
33+
try {
34+
primeStorage = new PrimeStorage(PATH);
35+
long checkNum = primeStorage.getNextNumberToCheck();
36+
// Check primes until reaching upper bound
37+
while (checkNum < UPPER_BOUND) {
38+
boolean isPrime = checkPrime(checkNum, primeStorage);
39+
if (isPrime) {
40+
primeStorage.appendPrime(checkNum);
41+
}
42+
checkNum++;
43+
}
44+
45+
} catch (Exception e) {
46+
System.err.println("Could not create and/or read/write file needed to store prime numbers on this device. " +
47+
"Please confirm Security Manager will allow create/read/write file storage.");
48+
System.exit(0);
49+
}
50+
51+
}
52+
53+
private static boolean checkPrime(long checkNum, PrimeStorage primeStorage) throws IOException {
54+
boolean loop = true;
55+
boolean isPrime = true;
56+
primeStorage.setPosition(0);
57+
58+
while (loop) {
59+
primeReadBuffer = primeStorage.readNextPrimes();
60+
boolean isDivisible = isDivisible(primeReadBuffer, checkNum);
61+
if (isDivisible) {
62+
// checkNum is not prime
63+
loop = false;
64+
isPrime = false;
65+
break;
66+
} else if (reachedEndOfBuffer) {
67+
loop = false;
68+
break;
69+
}
70+
}
71+
72+
return isPrime;
73+
}
74+
75+
public static boolean isDivisible(long[] primes, long checkNum) {
76+
for (long prime : primes) {
77+
if (prime == 0) {
78+
reachedEndOfBuffer = true;
79+
return false;
80+
}
81+
if (checkNum % prime == 0) {
82+
return true;
83+
}
84+
}
85+
return false;
86+
}
87+
}

‎ch_22/exercise22_08/PrimeNumbers.dat‎

7.9 KB
Binary file not shown.
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package ch_22.exercise22_08;
2+
3+
import java.io.EOFException;
4+
import java.io.File;
5+
import java.io.IOException;
6+
import java.io.RandomAccessFile;
7+
8+
public class PrimeStorage {
9+
protected File dataFile;
10+
private boolean firstRun;
11+
private long nextNumberToCheck = 0;
12+
private RandomAccessFile randomAccessFile;
13+
14+
15+
public PrimeStorage(String storageFilePath) throws Exception {
16+
dataFile = new File(storageFilePath);
17+
boolean createdOrExists = dataFile.exists();
18+
/* If file does not exist, this is the first run, starting from first prime number */
19+
if (!createdOrExists) {
20+
this.firstRun = true;
21+
try {
22+
createdOrExists = dataFile.createNewFile();
23+
} catch (IOException ioe) {
24+
throw new Exception("Error while trying to create storage file: " + ioe.getLocalizedMessage());
25+
}
26+
}
27+
28+
if (createdOrExists) {
29+
/* Need file channel to be: Closable, seekable, readable, writable */
30+
try {
31+
randomAccessFile = new RandomAccessFile(dataFile, "rws");
32+
// Handle different startup flows
33+
if (firstRun) {
34+
nextNumberToCheck = initializePrimes();
35+
} else {
36+
nextNumberToCheck = restartPrimes();
37+
}
38+
} catch (IOException ioException) {
39+
throw new Exception("IOException while creating in and out file stream: \n" + ioException.getMessage());
40+
}
41+
}
42+
43+
}
44+
45+
private long restartPrimes() throws IOException {
46+
long totalBytes = randomAccessFile.length();
47+
randomAccessFile.seek(totalBytes - 8); // Seek to read the last 8 bytes in the file
48+
long lastPrime = randomAccessFile.readLong();
49+
return nextNumberToCheck = lastPrime + 1;
50+
}
51+
52+
53+
private long initializePrimes() throws IOException {
54+
/* First time running program, prep the file by writing first two prime numbers */
55+
randomAccessFile.writeLong(2L);
56+
randomAccessFile.writeLong(3L);
57+
return 4;
58+
}
59+
60+
public long[] readNextPrimes() {
61+
long[] primes = new long[10000];
62+
try {
63+
for (int i = 0; i < primes.length && (randomAccessFile.getFilePointer() < randomAccessFile.length()); i++) {
64+
primes[i] = randomAccessFile.readLong();
65+
}
66+
} catch (EOFException eof) {
67+
System.out.println("End of File Reached");
68+
return primes;
69+
} catch (IOException e) {
70+
e.printStackTrace();
71+
}
72+
return primes;
73+
}
74+
75+
public void appendPrime(long primeNumber) throws IOException {
76+
long totalBytes = randomAccessFile.length();
77+
randomAccessFile.seek(totalBytes);
78+
randomAccessFile.writeLong(primeNumber);
79+
}
80+
81+
public boolean isFirstRun() {
82+
return firstRun;
83+
}
84+
85+
public long getNextNumberToCheck() {
86+
return nextNumberToCheck;
87+
}
88+
89+
public void setNextNumberToCheck(long nextNumberToCheck) {
90+
this.nextNumberToCheck = nextNumberToCheck;
91+
}
92+
93+
public void setPosition(long position) throws IOException {
94+
randomAccessFile.seek(position);
95+
}
96+
}

‎ch_22/exercise22_08/Test.java‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package ch_22.exercise22_08;
2+
3+
import java.io.EOFException;
4+
import java.io.File;
5+
import java.io.IOException;
6+
import java.io.RandomAccessFile;
7+
8+
/**
9+
* Integration Test will print out the prime numbers from PrimeNumbers.dat
10+
*/
11+
public class Test {
12+
private static final String[] packageParts = Exercise22_08.class.getPackage().getName().split("\\.");
13+
private static final String PATH = packageParts[0] + File.separator + packageParts[1] + File.separator + "PrimeNumbers.dat";
14+
15+
public static void main(String[] args) {
16+
try {
17+
RandomAccessFile randomAccessFile = new RandomAccessFile(new File(PATH), "rw");
18+
while (true) {
19+
long nextLong = randomAccessFile.readLong();
20+
System.out.println("Next long: " + nextLong);
21+
}
22+
} catch (EOFException eof) {
23+
System.out.println("EOF reached");
24+
} catch (IOException ioException) {
25+
System.out.println("IOException while creating in and out file stream: \n" + ioException.getMessage());
26+
}
27+
}
28+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /