Skip to content

Commit c91cd28

Browse files
committed
Exercise 22 12
1 parent a4bdbc7 commit c91cd28

File tree

4 files changed

+78
-8
lines changed

4 files changed

+78
-8
lines changed

ch_22/Exercise22_09.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ public static void main(String[] args) {
4040
}
4141
}
4242
scanner.close();
43-
System.out.println("The convex hull is " + getConvexHull(points));
43+
List<Point2D> convexHull = getConvexHull(points);
44+
System.out.println("The convex hull is: ");
45+
for (Point2D point : convexHull) {
46+
System.out.print("(" + point.getX() + ", " + point.getY() + ") ");
47+
}
48+
4449

4550
}
4651

ch_22/Exercise22_11.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@ public static void main(String[] args) {
5555

5656
double[][] points = new double[numPoints][2];
5757
System.out.print("Enter " + numPoints + " points: ");
58+
5859
for (int i = 0; i < points.length; i++) {
59-
points[i][0] = scanner.nextDouble();
60-
points[i][1] = scanner.nextDouble();
60+
for (int j = 0; j < 2; j++) {
61+
points[i][j] = scanner.nextDouble();
62+
}
6163
}
6264
List<MyPoint> result = getConvexHull(points);
6365
System.out.println("The convex hull is: ");
@@ -69,11 +71,11 @@ public static ArrayList<MyPoint> getConvexHull(double[][] s) {
6971
double yMin = s[0][1];
7072
int minPos = 0;
7173
// Find the bottom most point
72-
for (int i = 1; i < s[0].length; i++) {
74+
for (int i = 1; i < s.length; i++) {
7375
double y = s[i][1];
7476
// Pick the bottom-most or chose the right
7577
// most point in case of tie
76-
if ((y < yMin) || (yMin == y && s[i][0] < s[minPos][0])) {
78+
if ((y < yMin) || (yMin == y && s[i][0] > s[minPos][0])) {
7779
yMin = s[i][1];
7880
minPos = i;
7981

@@ -161,8 +163,7 @@ static double distSq(MyPoint p1, MyPoint p2) {
161163
* A utility function to find next to top in a stack
162164
*/
163165
static MyPoint nextToTop(Stack<MyPoint> s) {
164-
MyPoint p = s.peek();
165-
s.pop();
166+
MyPoint p = s.pop();
166167
MyPoint res = s.peek();
167168
s.push(p);
168169
return res;
@@ -194,7 +195,7 @@ public int compareTo(MyPoint o) {
194195
// Find orientation
195196
int orientation = orientation(rightMostLowestPoint, this, o);
196197
if (orientation == 0) {
197-
return (distSq(rightMostLowestPoint, o) >= distSq(rightMostLowestPoint, this)) ? -1 : 1;
198+
return distSq(rightMostLowestPoint, o) >= distSq(rightMostLowestPoint, this) ? -1 : 1;
198199
}
199200
return (orientation == 2) ? -1 : 1;
200201
}

ch_22/Exercise22_12.java

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package ch_22;
2+
3+
import ch_22.exercise22_08.Exercise22_08;
4+
5+
import java.io.File;
6+
import java.io.IOException;
7+
import java.io.RandomAccessFile;
8+
import java.util.Arrays;
9+
10+
/**
11+
* 22.12 (Last 100 prime numbers) Programming Exercise 22.8 stores the prime numbers
12+
* in a file named PrimeNumbers.dat.
13+
* Write an efficient program that reads
14+
* the last 100 numbers in the file. (Hint: Don’t read all numbers from the file.
15+
* Skip all numbers before the last 100 numbers in the file.)
16+
* <p>
17+
*/
18+
public class Exercise22_12 {
19+
private static final String[] packageParts = Exercise22_08.class.getPackage().getName().split("\\.");
20+
private static final String PATH = packageParts[0] + File.separator + packageParts[1] + File.separator + "PrimeNumbers.dat";
21+
private static final long UPPER_BOUND = 10_000_000_000L;
22+
private static final long BYTE_PER_LONG = 8;
23+
24+
public static void main(String[] args) throws Exception {
25+
File dataFile = new File(PATH);
26+
boolean createdOrExists = dataFile.exists();
27+
/* If file does not exist, this is the first run, starting from first prime number */
28+
if (!createdOrExists) {
29+
System.out.println("Prime Storage File from Exercise 22 08 does not exist. Please run Exercise 22 08 first.");
30+
System.exit(0);
31+
} else {
32+
/* Need file channel to be: Closable, seekable, readable, writable */
33+
try (RandomAccessFile randomAccessFile = new RandomAccessFile(dataFile, "rws")) {
34+
long[] last100Primes = new long[100];
35+
long endOfFilePointer = randomAccessFile.length(); // Get pointer to end of the last byte in the file
36+
/* Calc pointer to start point for reading the last 100 numbers in the file */
37+
long bytePointer = endOfFilePointer - (BYTE_PER_LONG * 100); // bytes per long * 100 numbers from end
38+
randomAccessFile.seek(bytePointer);
39+
int readCount = 0;
40+
long nextPrime = 0;
41+
while (nextPrime < UPPER_BOUND && readCount < 100) {
42+
nextPrime = randomAccessFile.readLong();
43+
last100Primes[readCount] = nextPrime;
44+
readCount++;
45+
}
46+
System.out.println("Last 100 primes in the file: ");
47+
System.out.println("================================");
48+
System.out.println(Arrays.toString(last100Primes));
49+
50+
} catch (IOException ioException) {
51+
throw new Exception("IOException while creating in and out file stream: \n" + ioException.getMessage());
52+
}
53+
54+
55+
}
56+
}
57+
}

ch_22/exercise22_08/PrimeStorage.java

+7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
package ch_22.exercise22_08;
22

3+
import ch_22.Exercise22_12;
4+
35
import java.io.EOFException;
46
import java.io.File;
57
import java.io.IOException;
68
import java.io.RandomAccessFile;
79

10+
/**
11+
* PrimeStorage class is used to store prime numbers in a file.
12+
* Used in {@link Exercise22_08} and {@link Exercise22_12 } to read/write/evaluate prime numbers
13+
* from the filesystem.
14+
*/
815
public class PrimeStorage {
916
protected File dataFile;
1017
private boolean firstRun;

0 commit comments

Comments
 (0)