Java Reference
In-Depth Information
The code generates the same sequence of random int values:
From random1: 734 660 210 581 128 202 549 564 459 961
From random2: 734 660 210 581 128 202 549 564 459 961
Note
The ability to generate the same sequence of random values is useful in software testing
and many other applications. In software testing, often you need to reproduce the test
cases from a fixed sequence of random numbers.
same sequence
9.6.3 The Point2D Class
Java API has a conveninent Point2D class in the javafx.geometry package for represent-
ing a point in a two-dimensional plane. The UML diagram for the class is shown in FigureĀ 9.12.
javafx.geometry.Point2D
+Point2D(x: double, y: double)
+distance(x: double, y: double): double
+distance(p: Point2D): double
+getX(): double
+getY(): double
+toString(): String
Constructs a Point2D object with the specified x - and y -coordinates.
Returns the distance between this point and the specified point ( x , y ).
Returns the distance between this point and the specified point p.
Returns the x -coordinate from this point.
Returns the y -coordinate from this point.
Returns a string representation for the point.
F IGURE 9.12
A Point2D object represents a point with x - and y -coordinates.
You can create a Point2D object for a point with the specified x - and y -coordinates, use
the distance method to compute the distance from this point to another point, and use the
toString() method to return a string representation of the point. Lisitng 9.5 gives an exam-
ple of using this class.
L ISTING 9.5
TestPoint2D.java
1 import java.util.Scanner;
2 import javafx.geometry.Point2D;
3
4 public class TestPoint2D {
5 public static void main(String[] args) {
6 Scanner input = new Scanner(System.in);
7
8 System.out.print( "Enter point1's x-, y-coordinates: " );
9 double x1 = input.nextDouble();
10 double y1 = input.nextDouble();
11 System.out.print( "Enter point2's x-, y-coordinates: " );
12
double x2 = input.nextDouble();
13
double y2 = input.nextDouble();
14
15 Point2D p1 = new Point2D(x1, y1);
16 Point2D p2 = new Point2D(x2, y2);
17 System.out.println( "p1 is " + p1.toString());
18 System.out.println( "p2 is " + p2.toString());
19 System.out.println( "The distance between p1 and p2 is " +
20
create an object
invoke toString()
p1.distance(p2));
get distance
21 }
22 }
 
 
Search WWH ::




Custom Search