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 b82f27f

Browse files
Clean up and add ch 22 exercises
1 parent 259ed95 commit b82f27f

File tree

5 files changed

+97
-3
lines changed

5 files changed

+97
-3
lines changed

‎ch_22/Exercise22_09.java‎

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,85 @@
11
package ch_22;
22

3+
import javafx.geometry.Point2D;
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.List;
8+
import java.util.Scanner;
9+
import java.util.stream.Collectors;
10+
311
/**
412
* **22.9 (Geometry: gift-wrapping algorithm for finding a convex hull) Section 22.10.1
513
* 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:
14+
* points. Assume that the Java’s coordinate system is used for the points.
15+
* Implement the algorithm using the following method:
716
* // Return the points that form a convex hull
817
* public static ArrayList<Point2D> getConvexHull(double[][]s)
918
* Point2D is defined in Section 9.6.
1019
* 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:
20+
* and displays the points that form a convex hull.
21+
* Here is a sample run:
22+
* <p>
23+
* How many points are in the set? 6
24+
* Enter 6 points: 1 2.4 2.5 2 1.5 34.5 5.5 6 6 2.4 5.5 9
25+
* The convex hull is
26+
* (1.5, 34.5) (5.5, 9.0) (6.0, 2.4) (2.5, 2.0) (1.0, 2.4)
1227
*/
1328
public class Exercise22_09 {
29+
public static void main(String[] args) {
30+
Scanner scanner = new Scanner(System.in);
31+
System.out.print("How many points are in the set? ");
32+
int numPoints = scanner.nextInt();
33+
System.out.print("Enter " + numPoints + " points: ");
34+
35+
double[][] points = new double[numPoints][2];
36+
37+
for (int i = 0; i < points.length; i++) {
38+
for (int j = 0; j < 2; j++) {
39+
points[i][j] = scanner.nextDouble();
40+
}
41+
}
42+
scanner.close();
43+
System.out.println("The convex hull is " + getConvexHull(points));
44+
45+
}
46+
47+
48+
public static ArrayList<Point2D> getConvexHull(double[][] s) {
49+
int n = s.length;
50+
// map matrix into List of Point2D's
51+
List<Point2D> S = Arrays.stream(s).map(points -> new Point2D(points[0], points[1])).collect(Collectors.toList());
52+
// Created list to hold the points in the convex hull
53+
ArrayList<Point2D> H = new ArrayList<>();
54+
// Find the leftmost point
55+
int l = 0;
56+
for (int i = 1; i < n; i++)
57+
if (S.get(i).getX() < S.get(l).getX()) {
58+
l = i;
59+
}
60+
61+
int p = l, q;
62+
do {
63+
H.add(S.get(p));
64+
65+
q = (p + 1) % n;
66+
67+
for (int i = 0; i < n; i++) {
68+
if (getOrientation(S.get(p), S.get(i), S.get(q)) == -1) {
69+
q = i;
70+
}
71+
}
72+
p = q;
73+
74+
} while (p != l);
75+
76+
return H;
77+
}
78+
79+
public static int getOrientation(Point2D p, Point2D q, Point2D r) {
80+
double val = (q.getY() - p.getY()) * (r.getX() - q.getX()) - (q.getX() - p.getX()) * (r.getY() - q.getY());
81+
82+
if (val == 0) return 0;
83+
return (val > 0) ? 1 : -1;
84+
}
1485
}

‎ch_22/Exercise22_10.java‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,31 @@
11
package ch_22;
22

3+
import ch_22.exercise22_08.Exercise22_08;
4+
5+
import java.io.File;
6+
import java.io.RandomAccessFile;
7+
38
/**
49
* 22.10 (Number of prime numbers) Programming Exercise 22.8 stores the prime numbers in a file named PrimeNumbers.dat.
510
* Write a program that finds the number of prime numbers that are less than or equal to 10, 100, 1,000, 10,000,
611
* 100,000, 1,000,000, 10,000,000, 100,000,000, 1,000,000,000, and
712
* 10,000,000,000. Your program should read the data from PrimeNumbers.dat
813
*/
914
public class Exercise22_10 {
15+
private static final String[] packageParts = Exercise22_08.class.getPackage().getName().split("\\.");
16+
private static final String PATH = packageParts[0] + File.separator + packageParts[1] + File.separator + "PrimeNumbers.dat";
17+
18+
public static void main(String[] args) {
19+
try {
20+
File dataFile = new File(PATH);
21+
RandomAccessFile randomAccessFile = new RandomAccessFile(dataFile, "r");
22+
long count = 0;
23+
for (long i = 10; i <= 10_000_000_000L; i *= 10) {
24+
25+
}
26+
27+
} catch (Exception e) {
28+
System.out.println("Exception occurred: " + e.getLocalizedMessage());
29+
}
30+
}
1031
}

‎ch_22/exercise22_08/PrimeNumbers.dat‎

17.3 KB
Binary file not shown.

‎ch_22/exercise22_08/Test.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import java.io.RandomAccessFile;
77

88
/**
9+
* The prime number for Exercise 22 08 are stored in a file as bytes, so in order to
10+
* evaluate the contents of the file we use this Test program to read the file and print the results.
11+
* <p>
912
* Integration Test will print out the prime numbers from PrimeNumbers.dat
1013
*/
1114
public class Test {

‎ch_24/exercise24_01/Exercise24_01.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public static void main(String[] args) {
3939
System.out.println("list1: " + list1);
4040
System.out.println("list2: " + list2);
4141

42-
4342
System.out.println("list1.addAll(list2) result: ");
4443
list1.addAll(list2);
4544
System.out.println("list1: " + list1);

0 commit comments

Comments
(0)

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