Java Reference
In-Depth Information
Student 0's correct count is 7
Student 1's correct count is 6
Student 2's correct count is 5
Student 3's correct count is 4
Student 4's correct count is 8
Student 5's correct count is 7
Student 6's correct count is 7
Student 7's correct count is 7
The statement in lines 5-13 declares, creates, and initializes a two-dimensional array of
characters and assigns the reference to
answers
of the
char[][]
type.
The statement in line 16 declares, creates, and initializes an array of
char
values and
assigns the reference to
keys
of the
char[]
type.
Each row in the array
answers
stores a student's answer, which is graded by comparing
it with the key in the array
keys
. The result is displayed immediately after a student's answer
is graded.
This section presents a geometric problem for finding the closest pair of points.
Key
Point
Given a set of points, the closest-pair problem is to find the two points that are nearest to
each other. In FigureĀ 8.3, for example, points
(1, 1)
and
(2, 0.5)
are closest to each
other. There are several ways to solve this problem. An intuitive approach is to compute the
distances between all pairs of points and find the one with the minimum distance, as imple-
mented in Listing 8.3.
closest-pair animation on the
Companion Website
(-1, 3)
(3, 3)
(4, 2)
x
y
(1, 1)
0
1
2
3
4
5
6
7
-1
3
-1 -1
1 1
2 0.5
2 1
3
(2, 0.5)
(4, -0.5)
(-1, -1)
(2, -1)
3
4
2
4
-0.5
F
IGURE
8.3
Points can be represented in a two-dimensional array.
L
ISTING
8.3
FindNearestPoints.java
1
import
java.util.Scanner;
2
3
public class
FindNearestPoints {
4
public static void
main(String[] args) {
5 Scanner input =
new
Scanner(System.in);
6 System.out.print(
"Enter the number of points: "
);
7
int
numberOfPoints = input.nextInt();
number of points
8
9
// Create an array to store points
Search WWH ::
Custom Search